forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicauth.go
50 lines (39 loc) · 1.34 KB
/
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
48
49
50
package basicauthrequest
import (
"encoding/base64"
"errors"
"net/http"
"strings"
authapi "github.com/openshift/origin/pkg/auth/api"
"github.com/openshift/origin/pkg/auth/authenticator"
)
type basicAuthRequestHandler struct {
passwordAuthenticator authenticator.Password
}
func NewBasicAuthAuthentication(passwordAuthenticator authenticator.Password) authenticator.Request {
return &basicAuthRequestHandler{passwordAuthenticator}
}
func (authHandler *basicAuthRequestHandler) AuthenticateRequest(req *http.Request) (authapi.UserInfo, bool, error) {
username, password, err := getBasicAuthInfo(req)
if err != nil {
return nil, false, err
}
return authHandler.passwordAuthenticator.AuthenticatePassword(username, password)
}
func getBasicAuthInfo(r *http.Request) (string, string, error) {
// Retrieve the Authorization header and check whether it contains basic auth information
const basicScheme string = "Basic "
auth := r.Header.Get("Authorization")
if !strings.HasPrefix(auth, basicScheme) {
return "", "", nil
}
str, err := base64.StdEncoding.DecodeString(auth[len(basicScheme):])
if err != nil {
return "", "", errors.New("No valid base64 data in basic auth scheme found")
}
cred := strings.Split(string(str), ":")
if len(cred) != 2 {
return "", "", errors.New("Invalid Authorization header")
}
return cred[0], cred[1], nil
}