-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
62 lines (52 loc) · 1.37 KB
/
token.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
// based on https://github.com/42wim/crewjam-saml
package samlplugin
import (
"context"
"strings"
jwt "github.com/dgrijalva/jwt-go"
)
// AuthorizationToken represents the data stored in the authorization cookie.
type AuthorizationToken struct {
jwt.StandardClaims
Attributes Attributes `json:"attr"`
}
// Attributes is a map of attributes provided in the SAML assertion
type Attributes map[string][]string
// Get returns the first attribute named `key` or an empty string if
// no such attributes is present.
func (a Attributes) Get(key string) string {
if a == nil {
return ""
}
v := a[key]
if len(v) == 0 {
return ""
}
return v[0]
}
// Get returns the first attribute named `key` or an empty string if
// no such attributes is present.
func (a Attributes) GetAll(key string) []string {
if a == nil {
return []string{}
}
v := a[strings.ToLower(key)]
if len(v) == 0 {
return []string{}
}
return v
}
type indexType int
const tokenIndex indexType = iota
// Token returns the token associated with ctx, or nil if no token are associated
func Token(ctx context.Context) *AuthorizationToken {
v := ctx.Value(tokenIndex)
if v == nil {
return nil
}
return v.(*AuthorizationToken)
}
// WithToken returns a new context with token associated
func WithToken(ctx context.Context, token *AuthorizationToken) context.Context {
return context.WithValue(ctx, tokenIndex, token)
}