-
Notifications
You must be signed in to change notification settings - Fork 158
/
token.go
193 lines (150 loc) · 5.18 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
184
185
186
187
188
189
190
191
192
193
package token
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/tink-crypto/tink-go/jwt"
"github.com/hatchet-dev/hatchet/internal/encryption"
"github.com/hatchet-dev/hatchet/internal/repository"
)
type JWTManager interface {
GenerateTenantToken(ctx context.Context, tenantId, name string) (string, error)
ValidateTenantToken(ctx context.Context, token string) (string, error)
}
type TokenOpts struct {
Issuer string
Audience string
ServerURL string
GRPCBroadcastAddress string
}
type jwtManagerImpl struct {
encryption encryption.EncryptionService
opts *TokenOpts
tokenRepo repository.EngineTokenRepository
verifier jwt.Verifier
}
func NewJWTManager(encryptionSvc encryption.EncryptionService, tokenRepo repository.EngineTokenRepository, opts *TokenOpts) (JWTManager, error) {
verifier, err := jwt.NewVerifier(encryptionSvc.GetPublicJWTHandle())
if err != nil {
return nil, fmt.Errorf("failed to create JWT Verifier: %v", err)
}
return &jwtManagerImpl{
encryption: encryptionSvc,
opts: opts,
tokenRepo: tokenRepo,
verifier: verifier,
}, nil
}
func (j *jwtManagerImpl) GenerateTenantToken(ctx context.Context, tenantId, name string) (string, error) {
// Retrieve the JWT Signer primitive from privateKeysetHandle.
signer, err := jwt.NewSigner(j.encryption.GetPrivateJWTHandle())
if err != nil {
return "", fmt.Errorf("failed to create JWT Signer: %v", err)
}
tokenId, expiresAt, opts := j.getJWTOptionsForTenant(tenantId)
rawJWT, err := jwt.NewRawJWT(opts)
if err != nil {
return "", fmt.Errorf("failed to create raw JWT: %v", err)
}
token, err := signer.SignAndEncode(rawJWT)
if err != nil {
return "", fmt.Errorf("failed to sign and encode JWT: %v", err)
}
// write the token to the database
_, err = j.tokenRepo.CreateAPIToken(ctx, &repository.CreateAPITokenOpts{
ID: tokenId,
ExpiresAt: expiresAt,
TenantId: &tenantId,
Name: &name,
})
if err != nil {
return "", fmt.Errorf("failed to write token to database: %v", err)
}
return token, nil
}
func (j *jwtManagerImpl) ValidateTenantToken(ctx context.Context, token string) (tenantId string, err error) {
// Verify the signed token.
audience := j.opts.Audience
validator, err := jwt.NewValidator(&jwt.ValidatorOpts{
ExpectedAudience: &audience,
ExpectedIssuer: &j.opts.Issuer,
FixedNow: time.Now(),
ExpectIssuedInThePast: true,
})
if err != nil {
return "", fmt.Errorf("failed to create JWT Validator: %v", err)
}
verifiedJwt, err := j.verifier.VerifyAndDecode(token, validator)
if err != nil {
return "", fmt.Errorf("failed to verify and decode JWT: %v", err)
}
// Read the token from the database and make sure it's not revoked
if hasTokenId := verifiedJwt.HasStringClaim("token_id"); !hasTokenId {
return "", fmt.Errorf("token does not have token_id claim")
}
tokenId, err := verifiedJwt.StringClaim("token_id")
if err != nil {
return "", fmt.Errorf("failed to read token_id claim: %v", err)
}
// ensure the current server url and grpc broadcast address match the token, if present
if hasServerURL := verifiedJwt.HasStringClaim("server_url"); hasServerURL {
serverURL, err := verifiedJwt.StringClaim("server_url")
if err != nil {
return "", fmt.Errorf("failed to read server_url claim: %v", err)
}
if serverURL != j.opts.ServerURL {
return "", fmt.Errorf("server_url claim does not match")
}
}
if hasGRPCBroadcastAddress := verifiedJwt.HasStringClaim("grpc_broadcast_address"); hasGRPCBroadcastAddress {
grpcBroadcastAddress, err := verifiedJwt.StringClaim("grpc_broadcast_address")
if err != nil {
return "", fmt.Errorf("failed to read grpc_broadcast_address claim: %v", err)
}
if grpcBroadcastAddress != j.opts.GRPCBroadcastAddress {
return "", fmt.Errorf("grpc_broadcast_address claim does not match")
}
}
// read the token from the database
dbToken, err := j.tokenRepo.GetAPITokenById(ctx, tokenId)
if err != nil {
return "", fmt.Errorf("failed to read token from database: %v", err)
}
if dbToken.Revoked {
return "", fmt.Errorf("token has been revoked")
}
if expiresAt := dbToken.ExpiresAt.Time; expiresAt.Before(time.Now()) {
return "", fmt.Errorf("token has expired")
}
// ensure the subject of the token matches the tenantId
if hasSubject := verifiedJwt.HasSubject(); !hasSubject {
return "", fmt.Errorf("token does not have subject claim")
}
subject, err := verifiedJwt.Subject()
if err != nil {
return "", fmt.Errorf("failed to read subject claim: %v", err)
}
return subject, nil
}
func (j *jwtManagerImpl) getJWTOptionsForTenant(tenantId string) (tokenId string, expiresAt time.Time, opts *jwt.RawJWTOptions) {
expiresAt = time.Now().Add(90 * 24 * time.Hour)
iAt := time.Now()
audience := j.opts.Audience
subject := tenantId
issuer := j.opts.Issuer
tokenId = uuid.New().String()
opts = &jwt.RawJWTOptions{
IssuedAt: &iAt,
Audience: &audience,
Subject: &subject,
ExpiresAt: &expiresAt,
Issuer: &issuer,
CustomClaims: map[string]interface{}{
"token_id": tokenId,
"server_url": j.opts.ServerURL,
"grpc_broadcast_address": j.opts.GRPCBroadcastAddress,
},
}
return
}