-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
203 lines (161 loc) · 3.91 KB
/
jwt.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package jwtlib
import (
"context"
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/pestanko/gouthy/app/shared"
log "github.com/sirupsen/logrus"
"strings"
"time"
)
func NewJwt(token *jwt.Token) Jwt {
impl := jwtImpl{
token: token,
}
impl.jtiParts, _ = ParseJtiParts(impl.ID())
return &impl
}
type Jwt interface {
JwkID() string
ClientId() string
Audience() string
Subject() string
ID() string
Issuer() string
IssuedAt() time.Time
ExpiresAt() time.Time
UserId() string
AppId() string
Scopes() shared.Scopes
Jti() JtiParts
Raw() string
// Kind of internal
Claims() map[string]interface{}
RawHeader() map[string]interface{}
}
type jwtImpl struct {
token *jwt.Token
jtiParts JtiParts
}
func (j *jwtImpl) RawHeader() map[string]interface{} {
return j.token.Header
}
func (j *jwtImpl) Claims() map[string]interface{} {
return j.token.Claims.(jwt.MapClaims)
}
func (j *jwtImpl) Raw() string {
return j.token.Raw
}
func (j *jwtImpl) Jti() JtiParts {
return j.jtiParts
}
func (j *jwtImpl) Scopes() shared.Scopes {
scopes := j.mapClaims()["scope"].([]interface{})
result := make(shared.Scopes, len(scopes))
for i, item := range scopes {
result[i] = item.(string)
}
return result
}
func (j *jwtImpl) AppId() string {
return j.Audience()
}
func (j *jwtImpl) UserId() string {
return j.Subject()
}
func (j *jwtImpl) Issuer() string {
return j.stringClaim("iss")
}
func (j *jwtImpl) IssuedAt() time.Time {
return j.timeClaim("iat")
}
func (j *jwtImpl) ExpiresAt() time.Time {
return j.timeClaim("exp")
}
func (j *jwtImpl) Subject() string {
return j.stringClaim("sub")
}
func (j *jwtImpl) ID() string {
return j.stringClaim("jti")
}
func (j *jwtImpl) ClientId() string {
return j.Audience()
}
func (j *jwtImpl) JwkID() string {
return j.token.Header["kid"].(string)
}
func (j *jwtImpl) Audience() string {
return j.stringClaim("aud")
}
func (j *jwtImpl) mapClaims() jwt.MapClaims {
return j.token.Claims.(jwt.MapClaims)
}
func (j *jwtImpl) stringClaim(claim string) string {
return j.mapClaims()[claim].(string)
}
func (j *jwtImpl) timeClaim(claim string) time.Time {
switch iat := j.mapClaims()[claim].(type) {
case float64:
return time.Unix(int64(iat), 0)
case json.Number:
v, _ := iat.Int64()
return time.Unix(v, 0)
}
return time.Time{}
}
type JwtSigningService interface {
Sign(ctx context.Context, token Jwt) (*SignedJwt, error)
Create(ctx context.Context, claims Claims) (Jwt, error)
}
type jwtSigningServiceImpl struct {
keys JwkRepository
}
type SignedJwt struct {
jwtImpl
Signature string
}
func (service *jwtSigningServiceImpl) Sign(ctx context.Context, token Jwt) (*SignedJwt, error) {
impl := token.(*jwtImpl)
key, err := service.keys.GetLatest(ctx)
if err != nil {
return nil, err
}
impl.token.Header["kid"] = key.KeyId()
signed, err := impl.token.SignedString(key.PrivateKey())
if err != nil {
return nil, err
}
return &SignedJwt{*impl, signed}, nil
}
func (service *jwtSigningServiceImpl) Create(ctx context.Context, claims Claims) (Jwt, error) {
serialize := claims.Serialize()
token := jwt.NewWithClaims(jwt.GetSigningMethod("RS256"), serialize)
shared.GetLogger(ctx).WithFields(log.Fields(serialize)).Info("Create a new TokenClaims")
return NewJwt(token), nil
}
func NewJwtSigningService(repo JwkRepository) JwtSigningService {
return &jwtSigningServiceImpl{keys: repo}
}
type JtiParts struct {
CorrelationId string
Type string
Offset string
}
func ParseJtiParts(str string) (JtiParts, error) {
if str == "" {
return JtiParts{}, fmt.Errorf("jti string is empty")
}
parts := strings.Split(str, ".")
if len(parts) != 3 {
return JtiParts{}, fmt.Errorf("expected jti parts are 3, provided: %d", len(parts))
}
return JtiParts{
Type: parts[0],
CorrelationId: parts[1],
Offset: parts[2],
}, nil
}
func JtiPartsToString(parts JtiParts) string {
return fmt.Sprintf("%s.%s.%s", parts.Type, parts.CorrelationId, parts.Offset)
}