-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt_service.go
130 lines (108 loc) · 3.87 KB
/
jwt_service.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
package jwtlib
import (
"context"
)
const (
TokenTypeAccess = "a"
TokenTypeRefresh = "r"
TokenTypeId = "i"
TokenTypeSession = "s"
TokenTypeUndefined = "U"
)
type JwtService interface {
CreateAccessToken(ctx context.Context, params TokenCreateParams) (Jwt, error)
CreateRefreshToken(ctx context.Context, params TokenCreateParams) (Jwt, error)
CreateIdToken(ctx context.Context, params TokenCreateParams) (Jwt, error)
CreateSessionToken(ctx context.Context, params TokenCreateParams) (Jwt, error)
CreateSignedAccessToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error)
CreateSignedRefreshToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error)
CreateSignedIdToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error)
CreateSignedSessionToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error)
}
func NewJwtService(keys JwkRepository) JwtService {
jwtSigning := NewJwtSigningService(keys)
return &jwtServiceImpl{
keys: keys,
jwtSigning: jwtSigning,
}
}
type jwtServiceImpl struct {
keys JwkRepository
jwtSigning JwtSigningService
}
func (j *jwtServiceImpl) CreateSessionToken(ctx context.Context, params TokenCreateParams) (Jwt, error) {
const ExpTime = SessionTokenExpiration
return j.createInternal(ctx, params, ExpTime, TokenTypeRefresh)
}
func (j *jwtServiceImpl) CreateSignedSessionToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error) {
token, err := j.CreateSessionToken(ctx, params)
if err != nil {
return nil, err
}
return j.signToken(ctx, token)
}
func (j *jwtServiceImpl) CreateSignedAccessToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error) {
token, err := j.CreateAccessToken(ctx, params)
if err != nil {
return nil, err
}
return j.signToken(ctx, token)
}
func (j *jwtServiceImpl) CreateSignedRefreshToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error) {
token, err := j.CreateRefreshToken(ctx, params)
if err != nil {
return nil, err
}
return j.signToken(ctx, token)
}
func (j *jwtServiceImpl) CreateSignedIdToken(ctx context.Context, params TokenCreateParams) (*SignedJwt, error) {
token, err := j.CreateIdToken(ctx, params)
if err != nil {
return nil, err
}
return j.signToken(ctx, token)
}
func (j *jwtServiceImpl) CreateAccessToken(ctx context.Context, params TokenCreateParams) (Jwt, error) {
const ExpTime = AccessTokenExpiration
return j.createInternal(ctx, params, ExpTime, TokenTypeAccess)
}
func (j *jwtServiceImpl) CreateRefreshToken(ctx context.Context, params TokenCreateParams) (Jwt, error) {
const ExpTime = RefreshTokenExpiration
return j.createInternal(ctx, params, ExpTime, TokenTypeRefresh)
}
func (j *jwtServiceImpl) CreateIdToken(ctx context.Context, params TokenCreateParams) (Jwt, error) {
const ExpTime = IdTokenExpiration
return j.createInternal(ctx, params, ExpTime, TokenTypeId)
}
func (j *jwtServiceImpl) createInternal(ctx context.Context, params TokenCreateParams, exp int64, tokenType string) (Jwt, error) {
claims, err := j.makeClaims(ctx, params, exp, tokenType)
if err != nil {
return nil, err
}
return j.createToken(ctx, claims)
}
func (j *jwtServiceImpl) createToken(ctx context.Context, claims Claims) (Jwt, error) {
jwt, err := j.jwtSigning.Create(ctx, claims)
if err != nil {
return nil, err
}
return jwt, nil
}
func (j *jwtServiceImpl) signToken(ctx context.Context, jwt Jwt) (*SignedJwt, error) {
signed, err := j.jwtSigning.Sign(ctx, jwt)
if err != nil {
return nil, err
}
return signed, nil
}
func (j *jwtServiceImpl) makeClaims(ctx context.Context, params TokenCreateParams, expTime int64, tokenType string) (Claims, error) {
claims := makeClaims(ctx, claimsParams{
User: params.User,
Application: params.App,
Scopes: params.Scopes,
ExpirationAdd: expTime,
TokenType: tokenType,
Issuer: "Gouthy", // TODO
})
return claims, nil
}