-
Notifications
You must be signed in to change notification settings - Fork 9
/
token.go
183 lines (161 loc) · 3.98 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package model
import (
"time"
jwt "github.com/golang-jwt/jwt/v4"
)
const (
TokenTypeInvite = "invite" // TokenTypeInvite is an invite token type value.
TokenTypeReset = "reset" // TokenTypeReset is an reset token type value.
TokenTypeWebCookie = "web-cookie" // TokenTypeWebCookie is a web-cookie token type value.
TokenTypeAccess = "access" // TokenTypeAccess is an access token type.
TokenTypeRefresh = "refresh" // TokenTypeRefresh is a refresh token type.
TokenTypeTFAPreauth = "2fa-preauth" // TokenTypeTFAPreauth is an 2fa preauth token type.
)
// StandardTokenClaims structured version of Claims Section, as referenced at
// https://tools.ietf.org/html/rfc7519#section-4.1
type StandardTokenClaims interface {
Audience() string
ExpiresAt() time.Time
ID() string
IssuedAt() time.Time
Issuer() string
NotBefore() time.Time
Subject() string
}
// Token is an abstract application token.
type Token interface {
StandardTokenClaims
Validate() error
UserID() string
Type() string
Scopes() string
Payload() map[string]interface{}
}
// NewTokenWithClaims generates new JWT token with claims and keyID.
func NewTokenWithClaims(method jwt.SigningMethod, kid string, claims jwt.Claims) *jwt.Token {
return &jwt.Token{
Header: map[string]interface{}{
"typ": "JWT",
"alg": method.Alg(),
"kid": kid,
},
Claims: claims,
Method: method,
}
}
// JWToken represents JWT token.
type JWToken struct {
JWT *jwt.Token
New bool
}
// Validate validates token data. Returns nil if all data is valid.
func (t *JWToken) Validate() error {
if t.JWT == nil {
return ErrEmptyToken
}
if !t.New && !t.JWT.Valid {
return ErrTokenInvalid
}
return nil
}
// UserID returns user ID.
func (t *JWToken) UserID() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Subject
}
// Payload returns token payload.
func (t *JWToken) Payload() map[string]interface{} {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return make(map[string]interface{})
}
return claims.Payload
}
// Type returns token type.
func (t *JWToken) Type() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Type
}
// Audience standard token claim
func (t *JWToken) Audience() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Audience
}
// ExpiresAt standard token claim
func (t *JWToken) ExpiresAt() time.Time {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return time.Time{}
}
return time.Unix(claims.ExpiresAt, 0)
}
// ID standard token claim
func (t *JWToken) ID() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Id
}
func (t *JWToken) Claims() *Claims {
return t.JWT.Claims.(*Claims)
}
// IssuedAt standard token claim
func (t *JWToken) IssuedAt() time.Time {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return time.Time{}
}
return time.Unix(claims.IssuedAt, 0)
}
// Issuer standard token claim
func (t *JWToken) Issuer() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Issuer
}
// NotBefore standard token claim
func (t *JWToken) NotBefore() time.Time {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return time.Time{}
}
return time.Unix(claims.NotBefore, 0)
}
// Subject standard token claim
func (t *JWToken) Subject() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Subject
}
// Scopes standard token claim
func (t *JWToken) Scopes() string {
claims, ok := t.JWT.Claims.(*Claims)
if !ok {
return ""
}
return claims.Scopes
}
// Claims is an extended claims structure.
type Claims struct {
Payload map[string]interface{} `json:"payload,omitempty"`
Scopes string `json:"scopes,omitempty"`
Type string `json:"type,omitempty"`
KeyID string `json:"kid,omitempty"` // optional keyID
jwt.StandardClaims
}
// Full example of how to use JWT tokens:
// https://github.com/form3tech-oss/jwt-go/blob/master/cmd/jwt/app.go