This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
forked from arrikto/oidc-authservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticator_idtoken.go
69 lines (57 loc) · 1.83 KB
/
authenticator_idtoken.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"net/http"
oidc "github.com/coreos/go-oidc"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/user"
)
type idTokenAuthenticator struct {
header string // header name where id token is stored
caBundle []byte
provider *oidc.Provider
clientID string // need client id to verify the id token
userIDClaim string // retrieve the userid if the claim exists
groupsClaim string
}
func (s *idTokenAuthenticator) AuthenticateRequest(r *http.Request) (*authenticator.Response, bool, error) {
logger := loggerForRequest(r, "idtoken authenticator")
// get id-token from header
bearer := getBearerToken(r.Header.Get(s.header))
if len(bearer) == 0 {
logger.Info("No bearer token found")
return nil, false, nil
}
ctx := setTLSContext(r.Context(), s.caBundle)
// Verifying received ID token
verifier := s.provider.Verifier(&oidc.Config{ClientID: s.clientID})
token, err := verifier.Verify(ctx, bearer)
if err != nil {
logger.Errorf("id-token verification failed: %v", err)
return nil, false, nil
}
var claims map[string]interface{}
if err := token.Claims(&claims); err != nil {
logger.Errorf("retrieving user claims failed: %v", err)
return nil, false, nil
}
if claims[s.userIDClaim] == nil {
// No USERID_CLAIM, pass this authenticator
logger.Error("USERID_CLAIM doesn't exist in the id token")
return nil, false, nil
}
groups := []string{}
groupsClaim := claims[s.groupsClaim]
if groupsClaim != nil {
groups = interfaceSliceToStringSlice(groupsClaim.([]interface{}))
}
// Authentication using header successfully completed
extra := map[string][]string{"auth-method": {"header"}}
resp := &authenticator.Response{
User: &user.DefaultInfo{
Name: claims[s.userIDClaim].(string),
Groups: groups,
Extra: extra,
},
}
return resp, true, nil
}