-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
108 lines (90 loc) · 2.75 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package lib
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"golang.org/x/net/context"
)
// The key type is unexported to prevent collisions with context keys defined in
// other packages.
type key int
// idtKey is the context key for an identity. Its value of zero is
// arbitrary. If this package defined other context keys, they would have
// different integer values.
const idtKey key = 0
const tokenKey = 1
// NewContext returns a new Context carrying an Identity pat.
func NewContext(ctx context.Context, idt *Identity) context.Context {
return context.WithValue(ctx, idtKey, idt)
}
// FromContext extracts the Identity pat from ctx, if present.
func FromContext(ctx context.Context) (*Identity, bool) {
// ctx.Value returns nil if ctx has no value for the key;
p, ok := ctx.Value(idtKey).(*Identity)
return p, ok
}
// MustFromContext extracts the identity from ctx.
// If not present it panics.
func MustFromContext(ctx context.Context) *Identity {
idt, ok := ctx.Value(idtKey).(*Identity)
if !ok {
panic("identity is not registered")
}
return idt
}
func NewTokenContext(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, tokenKey, token)
}
func FromTokenContext(ctx context.Context) (string, bool) {
t, ok := ctx.Value(tokenKey).(string)
return t, ok
}
func MustFromTokenContext(ctx context.Context) string {
t, ok := ctx.Value(tokenKey).(string)
if !ok {
panic("token is not registered")
}
return t
}
func ParseToken(t, secret string) (*Identity, error) {
token, err := jwt.Parse(t, func(token *jwt.Token) (key interface{}, err error) {
return []byte(secret), nil
})
if err != nil {
return nil, err
}
identity := &Identity{}
pidString, ok := token.Claims["pid"].(string)
if !ok {
return nil, fmt.Errorf("failed cast to string of pid:%s",
fmt.Sprintln(token.Claims["pid"]))
}
idpString, ok := token.Claims["idp"].(string)
if !ok {
return nil, fmt.Errorf("failed cast to string of idp:%s",
fmt.Sprintln(token.Claims["idp"]))
}
displaynameString, ok := token.Claims["display_name"].(string)
if !ok {
return nil, fmt.Errorf("failed cast to string of display_ame:%s",
fmt.Sprintln(token.Claims["display_ame"]))
}
emailString, ok := token.Claims["email"].(string)
if !ok {
return nil, fmt.Errorf("failed cast to string of email:%s",
fmt.Sprintln(token.Claims["email"]))
}
identity.Pid = pidString
identity.Idp = idpString
identity.DisplayName = displaynameString
identity.Email = emailString
return identity, nil
}
type Identity struct {
Pid string `json:"pid"`
Idp string `json:"idp"`
Email string `json:"email"`
DisplayName string `json:"email"`
}
func (i *Identity) String() string {
return fmt.Sprintf("identity(pid:%s idp:%s email:%s)", i.Pid, i.Idp, i.Email)
}