-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
45 lines (36 loc) · 997 Bytes
/
user.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
package main
import (
"encoding/json"
"github.com/coreos/go-oidc"
)
type UserContext struct {
UserName string
Groups []string
}
func ParseUserContext(opts *Options, token *oidc.IDToken) (*UserContext, error) {
var claims map[string]json.RawMessage
if err := token.Claims(&claims); err != nil {
return nil, err
}
var userContext UserContext
if val, ok := claims[opts.UserClaim]; ok {
var userName string
if err := json.Unmarshal(val, &userName); err == nil {
userContext.UserName = opts.UserPrefix + userName
}
}
if val, ok := claims[opts.GroupsClaim]; ok {
var group string
if err := json.Unmarshal(val, &group); err == nil {
userContext.Groups = []string{opts.GroupsPrefix + group}
}
var groups []string
if err := json.Unmarshal(val, &groups); err == nil {
userContext.Groups = make([]string, len(groups), len(groups))
for i, group := range groups {
userContext.Groups[i] = opts.GroupsPrefix + group
}
}
}
return &userContext, nil
}