-
Notifications
You must be signed in to change notification settings - Fork 23
/
token.go
169 lines (154 loc) · 4.09 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package pwsso
import (
"fmt"
time "time"
jwt "github.com/dgrijalva/jwt-go"
"go.uber.org/zap"
"pathwar.land/pathwar/v2/go/pkg/errcode"
)
func (c *client) TokenWithClaims(bearer string) (*jwt.Token, jwt.MapClaims, error) {
return TokenWithClaims(bearer, c.publicKey, c.opts.AllowUnsafe)
}
func TokenWithClaims(bearer string, pubkey interface{}, allowUnsafe bool) (*jwt.Token, jwt.MapClaims, error) {
claims := jwt.MapClaims{}
// FIXME: add an option to automatically fetch the public key from
// https://id.pathwar.land/auth/realms/Pathwar-Dev/protocol/openid-connect/certs
// or
// https://id.pathwar.land/auth/realms/Pathwar-Dev
kf := func(token *jwt.Token) (interface{}, error) {
return pubkey, nil
}
token, err := jwt.ParseWithClaims(bearer, claims, kf)
if err != nil {
if allowUnsafe {
zap.L().Warn(
"invalid token",
zap.Error(err),
zap.Bool("client-unsafe", true),
)
parser := new(jwt.Parser)
token, _, err := parser.ParseUnverified(bearer, claims)
if err != nil {
return nil, nil, errcode.ErrSSOInvalidBearer.Wrap(err)
}
return token, claims, nil
}
e, ok := err.(*jwt.ValidationError)
if !ok || (ok && e.Errors&jwt.ValidationErrorIssuedAt == 0) { // don't report error that token used before issued.
return nil, nil, errcode.ErrSSOInvalidBearer.Wrap(err)
}
}
return token, claims, nil
}
func TokenHasRole(token *jwt.Token, expectedRole string) error {
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return errcode.TODO // Unable to get claims out of JWT token
}
resources, ok := claims["resource_access"]
if !ok {
return errcode.TODO // Unable to get resource_access of JWT token claims
}
clients, ok := resources.(map[string]interface{})
if !ok {
return errcode.TODO // Unable to get resources out of JWT token claims
}
for _, clientMap := range clients {
client, ok := clientMap.(map[string]interface{})
if !ok {
continue
}
roles, ok := client["roles"].([]interface{})
if !ok {
continue
}
for _, role := range roles {
if role == expectedRole {
return nil
}
}
}
return errcode.TODO // Unable to get expected role from JWT token claims
}
func SubjectFromToken(token *jwt.Token) string {
mc := token.Claims.(jwt.MapClaims)
if v := mc["sub"]; v != nil {
return v.(string)
}
return ""
}
func ClaimsFromToken(token *jwt.Token) *Claims {
mc := token.Claims.(jwt.MapClaims)
claims := &Claims{
ActionToken: &ActionToken{},
}
// keycloak
if v := mc["typ"]; v != nil {
claims.ActionToken.Typ = v.(string)
}
if v := mc["sub"]; v != nil {
claims.ActionToken.Sub = v.(string)
}
if v := mc["azp"]; v != nil {
claims.ActionToken.Azp = v.(string)
}
if v := mc["iss"]; v != nil {
claims.ActionToken.Iss = v.(string)
}
if v := mc["aud"]; v != nil {
switch typed := v.(type) {
case string:
claims.ActionToken.Aud = typed
default:
claims.ActionToken.Aud = fmt.Sprintf("%v", typed)
}
}
if v := mc["asid"]; v != nil {
claims.ActionToken.Asid = v.(string)
}
if v := mc["nonce"]; v != nil {
claims.ActionToken.Nonce = v.(string)
}
if v := mc["session_state"]; v != nil {
claims.ActionToken.SessionState = v.(string)
}
if v := mc["scope"]; v != nil {
claims.ActionToken.Scope = v.(string)
}
if v := mc["jti"]; v != nil {
claims.ActionToken.Jti = v.(string)
}
if v := mc["nbf"]; v != nil {
claims.ActionToken.Nbf = float32(v.(float64))
}
if v := mc["iat"]; v != nil {
t := time.Unix(int64(v.(float64)), 0)
claims.ActionToken.Iat = &t
}
if v := mc["exp"]; v != nil {
t := time.Unix(int64(v.(float64)), 0)
claims.ActionToken.Exp = &t
}
if v := mc["auth_time"]; v != nil {
t := time.Unix(int64(v.(float64)), 0)
claims.ActionToken.AuthTime = &t
}
// pathwar specific
if v := mc["preferred_username"]; v != nil {
claims.PreferredUsername = v.(string)
}
if v := mc["email"]; v != nil {
claims.Email = v.(string)
}
if v := mc["email_verified"]; v != nil {
claims.EmailVerified = v.(bool)
}
if v := mc["given_name"]; v != nil {
claims.GivenName = v.(string)
}
if v := mc["family_name"]; v != nil {
claims.FamilyName = v.(string)
}
// FIXME: add more infos
return claims
}