smithy

ref: 80b32ff990c2ef131867cf5c3351fe0e3dd88113

pkg/go-git-http/auth/basicauth.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package auth

import (
	"encoding/base64"
	"fmt"
	"regexp"
	"strings"
)

// Parse http basic header
type BasicAuth struct {
	Name string
	Pass string
}

var (
	basicAuthRegex = regexp.MustCompile("^([^:]*):(.*)$")
)

func parseAuthHeader(header string) (*BasicAuth, error) {
	parts := strings.SplitN(header, " ", 2)
	if len(parts) < 2 {
		return nil, fmt.Errorf("Invalid authorization header, not enought parts")
	}

	authType := parts[0]
	authData := parts[1]

	if strings.ToLower(authType) != "basic" {
		return nil, fmt.Errorf("Authentication '%s' was not of 'Basic' type", authType)
	}

	data, err := base64.StdEncoding.DecodeString(authData)
	if err != nil {
		return nil, err
	}

	matches := basicAuthRegex.FindStringSubmatch(string(data))
	if matches == nil {
		return nil, fmt.Errorf("Authorization data '%s' did not match auth regexp", data)
	}

	return &BasicAuth{
		Name: matches[1],
		Pass: matches[2],
	}, nil
}