forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bearertoken.go
38 lines (32 loc) · 985 Bytes
/
bearertoken.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
package bearertoken
import (
"net/http"
"strings"
"github.com/openshift/origin/pkg/auth/authenticator"
"k8s.io/kubernetes/pkg/auth/user"
)
type Authenticator struct {
// auth is the token authenticator to use to validate the token
auth authenticator.Token
// removeHeader indicates whether the Authorization header should be removeHeaderd on successful auth
removeHeader bool
}
func New(auth authenticator.Token, removeHeader bool) *Authenticator {
return &Authenticator{auth, removeHeader}
}
func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool, error) {
auth := strings.TrimSpace(req.Header.Get("Authorization"))
if auth == "" {
return nil, false, nil
}
parts := strings.Split(auth, " ")
if len(parts) < 2 || strings.ToLower(parts[0]) != "bearer" {
return nil, false, nil
}
token := parts[1]
user, ok, err := a.auth.AuthenticateToken(token)
if ok && a.removeHeader {
req.Header.Del("Authorization")
}
return user, ok, err
}