-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtoken.go
315 lines (268 loc) · 8.61 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package token
import (
"crypto"
"crypto/x509"
"errors"
"fmt"
"time"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
log "github.com/sirupsen/logrus"
"github.com/distribution/distribution/v3/registry/auth"
)
const (
// TokenSeparator is the value which separates the header, claims, and
// signature in the compact serialization of a JSON Web Token.
TokenSeparator = "."
// Leeway is the Duration that will be added to NBF and EXP claim
// checks to account for clock skew as per https://tools.ietf.org/html/rfc7519#section-4.1.5
Leeway = 60 * time.Second
)
var signingAlgorithms = map[string]jose.SignatureAlgorithm{
"EdDSA": jose.EdDSA,
"HS256": jose.HS256,
"HS384": jose.HS384,
"HS512": jose.HS512,
"RS256": jose.RS256,
"RS384": jose.RS384,
"RS512": jose.RS512,
"ES256": jose.ES256,
"ES384": jose.ES384,
"ES512": jose.ES512,
"PS256": jose.PS256,
"PS384": jose.PS384,
"PS512": jose.PS512,
}
var defaultSigningAlgorithms = []jose.SignatureAlgorithm{
jose.EdDSA,
jose.HS256,
jose.HS384,
jose.HS512,
jose.RS256,
jose.RS384,
jose.RS512,
jose.ES256,
jose.ES384,
jose.ES512,
jose.PS256,
jose.PS384,
jose.PS512,
}
// Errors used by token parsing and verification.
var (
ErrMalformedToken = errors.New("malformed token")
ErrInvalidToken = errors.New("invalid token")
)
// ResourceActions stores allowed actions on a named and typed resource.
type ResourceActions struct {
Type string `json:"type"`
Class string `json:"class,omitempty"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
// ClaimSet describes the main section of a JSON Web Token.
type ClaimSet struct {
// Public claims
Issuer string `json:"iss"`
Subject string `json:"sub"`
Audience AudienceList `json:"aud"`
Expiration int64 `json:"exp"`
NotBefore int64 `json:"nbf"`
IssuedAt int64 `json:"iat"`
JWTID string `json:"jti"`
// Private claims
Access []*ResourceActions `json:"access"`
}
// Token is a JSON Web Token.
type Token struct {
Raw string
JWT *jwt.JSONWebToken
}
// VerifyOptions is used to specify
// options when verifying a JSON Web Token.
type VerifyOptions struct {
TrustedIssuers []string
AcceptedAudiences []string
Roots *x509.CertPool
TrustedKeys map[string]crypto.PublicKey
}
// NewToken parses the given raw token string
// and constructs an unverified JSON Web Token.
func NewToken(rawToken string, signingAlgs []jose.SignatureAlgorithm) (*Token, error) {
token, err := jwt.ParseSigned(rawToken, signingAlgs)
if err != nil {
return nil, ErrMalformedToken
}
return &Token{
Raw: rawToken,
JWT: token,
}, nil
}
// Verify attempts to verify this token using the given options.
// Returns a nil error if the token is valid.
func (t *Token) Verify(verifyOpts VerifyOptions) (*ClaimSet, error) {
// Verify that the signing key is trusted.
signingKey, err := t.VerifySigningKey(verifyOpts)
if err != nil {
log.Infof("failed to verify token: %v", err)
return nil, ErrInvalidToken
}
// NOTE(milosgajdos): Claims both verifies the signature
// and returns the claims within the payload
var claims ClaimSet
err = t.JWT.Claims(signingKey, &claims)
if err != nil {
return nil, err
}
// Verify that the Issuer claim is a trusted authority.
if !contains(verifyOpts.TrustedIssuers, claims.Issuer) {
log.Infof("token from untrusted issuer: %q", claims.Issuer)
return nil, ErrInvalidToken
}
// Verify that the Audience claim is allowed.
if !containsAny(verifyOpts.AcceptedAudiences, claims.Audience) {
log.Infof("token intended for another audience: %v", claims.Audience)
return nil, ErrInvalidToken
}
// Verify that the token is currently usable and not expired.
currentTime := time.Now()
ExpWithLeeway := time.Unix(claims.Expiration, 0).Add(Leeway)
if currentTime.After(ExpWithLeeway) {
log.Infof("token not to be used after %s - currently %s", ExpWithLeeway, currentTime)
return nil, ErrInvalidToken
}
NotBeforeWithLeeway := time.Unix(claims.NotBefore, 0).Add(-Leeway)
if currentTime.Before(NotBeforeWithLeeway) {
log.Infof("token not to be used before %s - currently %s", NotBeforeWithLeeway, currentTime)
return nil, ErrInvalidToken
}
return &claims, nil
}
// VerifySigningKey attempts to verify and return the signing key which was used to sign the token.
func (t *Token) VerifySigningKey(verifyOpts VerifyOptions) (crypto.PublicKey, error) {
if len(t.JWT.Headers) == 0 {
return nil, ErrInvalidToken
}
// NOTE(milosgajdos): docker auth spec does not seem to
// support tokens signed by multiple signatures so we are
// verifying the first one in the list only at the moment.
header := t.JWT.Headers[0]
signingKey, err := verifyCertChain(header, verifyOpts.Roots)
if err != nil {
// NOTE(milosgajdos): if the x5c header is missing
// the token may have been signed by a JWKS.
if errors.Is(err, jose.ErrMissingX5cHeader) {
switch {
case header.JSONWebKey != nil:
return verifyJWK(header, verifyOpts)
case header.KeyID != "":
if signingKey, ok := verifyOpts.TrustedKeys[header.KeyID]; ok {
return signingKey, nil
}
return nil, fmt.Errorf("token signed by untrusted key with ID: %q", header.KeyID)
default:
return nil, ErrInvalidToken
}
}
return nil, err
}
return signingKey, nil
}
func verifyCertChain(header jose.Header, roots *x509.CertPool) (signingKey crypto.PublicKey, err error) {
verifyOpts := x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
// TODO: this call returns certificate chains which we ignore for now, but
// we should check them for revocations if we have the ability later.
chains, err := header.Certificates(verifyOpts)
if err != nil {
return nil, err
}
signingKey = getCertPubKey(chains)
return
}
func verifyJWK(header jose.Header, verifyOpts VerifyOptions) (crypto.PublicKey, error) {
jwk := header.JSONWebKey
// Check to see if the key includes a certificate chain.
if len(jwk.Certificates) == 0 {
// The JWK should be one of the trusted root keys.
key, trusted := verifyOpts.TrustedKeys[jwk.KeyID]
if !trusted {
return nil, errors.New("untrusted JWK with no certificate chain")
}
// The JWK is one of the trusted keys.
return key, nil
}
opts := x509.VerifyOptions{
Roots: verifyOpts.Roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
leaf := jwk.Certificates[0]
if opts.Intermediates == nil {
opts.Intermediates = x509.NewCertPool()
for _, intermediate := range jwk.Certificates[1:] {
opts.Intermediates.AddCert(intermediate)
}
}
// TODO: this call returns certificate chains which we ignore for now, but
// we should check them for revocations if we have the ability later.
chains, err := leaf.Verify(opts)
if err != nil {
return nil, err
}
return getCertPubKey(chains), nil
}
func getCertPubKey(chains [][]*x509.Certificate) crypto.PublicKey {
// NOTE(milosgajdos): if there are no certificates
// header.Certificates call above returns error, so we are
// guaranteed to get at least one certificate chain.
// We pick the leaf certificate chain.
chain := chains[0]
// NOTE(milosgajdos): header.Certificates call returns the result
// of leafCert.Verify which is a call to x509.Certificate.Verify.
// If successful, it returns one or more chains where the first
// element of the chain is x5c and the last element is from opts.Roots.
// See: https://pkg.go.dev/crypto/x509#Certificate.Verify
cert := chain[0]
// NOTE: we dont have to verify that the public key in the leaf cert
// *is* the signing key: if it's not the signing then token claims
// verification with this key fails
return cert.PublicKey.(crypto.PublicKey)
}
// accessSet returns a set of actions available for the resource
// actions listed in the `access` section of this token.
func (c *ClaimSet) accessSet() accessSet {
accessSet := make(accessSet, len(c.Access))
for _, resourceActions := range c.Access {
resource := auth.Resource{
Type: resourceActions.Type,
Name: resourceActions.Name,
}
set, exists := accessSet[resource]
if !exists {
set = newActionSet()
accessSet[resource] = set
}
for _, action := range resourceActions.Actions {
set.add(action)
}
}
return accessSet
}
func (c *ClaimSet) resources() []auth.Resource {
resourceSet := map[auth.Resource]struct{}{}
for _, resourceActions := range c.Access {
resource := auth.Resource{
Type: resourceActions.Type,
Class: resourceActions.Class,
Name: resourceActions.Name,
}
resourceSet[resource] = struct{}{}
}
resources := make([]auth.Resource, 0, len(resourceSet))
for resource := range resourceSet {
resources = append(resources, resource)
}
return resources
}