/
verifier_default.go
166 lines (143 loc) · 4.3 KB
/
verifier_default.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
package credentials
import (
"context"
"crypto/ecdsa"
"crypto/rsa"
"fmt"
"strings"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"github.com/ory/fosite"
"github.com/ory/herodot"
"github.com/ory/oathkeeper/helper"
"github.com/ory/x/jwtx"
"github.com/ory/x/stringslice"
"github.com/ory/x/stringsx"
)
var _ Verifier = new(VerifierDefault)
type VerifierDefault struct {
r FetcherRegistry
}
func NewVerifierDefault(f FetcherRegistry) *VerifierDefault {
return &VerifierDefault{
r: f,
}
}
func (v *VerifierDefault) Verify(
ctx context.Context,
token string,
r *ValidationContext,
) (*jwt.Token, error) {
// Parse the token.
t, err := jwt.ParseWithClaims(token, jwt.MapClaims{}, func(token *jwt.Token) (interface{}, error) {
if !stringslice.Has(r.Algorithms, fmt.Sprintf("%s", token.Header["alg"])) {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReason(fmt.Sprintf(`JSON Web Token used signing method "%s" which is not allowed.`, token.Header["alg"])))
}
kid, ok := token.Header["kid"].(string)
if !ok || kid == "" {
return nil, errors.WithStack(herodot.ErrBadRequest.WithReason("The JSON Web Token must contain a kid header value but did not."))
}
key, err := v.r.CredentialsFetcher().ResolveKey(ctx, r.KeyURLs, kid, "sig")
if err != nil {
return nil, err
}
// Mutate to public key
if _, ok := key.Key.([]byte); !ok && !key.IsPublic() {
k := key.Public()
key = &k
}
switch token.Method.(type) {
case *jwt.SigningMethodRSA:
if k, ok := key.Key.(*rsa.PublicKey); ok {
return k, nil
}
case *jwt.SigningMethodECDSA:
if k, ok := key.Key.(*ecdsa.PublicKey); ok {
return k, nil
}
case *jwt.SigningMethodRSAPSS:
if k, ok := key.Key.(*rsa.PublicKey); ok {
return k, nil
}
case *jwt.SigningMethodHMAC:
if k, ok := key.Key.([]byte); ok {
return k, nil
}
default:
return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf(`This request object uses unsupported signing algorithm "%s".`, token.Header["alg"]))
}
return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf(`The signing key algorithm does not match the algorithm from the token header.`))
})
if err != nil {
if e, ok := errors.Cause(err).(*jwt.ValidationError); ok {
if _, ok := errors.Cause(e.Inner).(*herodot.DefaultError); !ok {
return nil, herodot.ErrInternalServerError.WithErrorf(e.Error()).WithTrace(err)
}
return nil, e.Inner
}
return nil, err
} else if !t.Valid {
return nil, errors.WithStack(fosite.ErrInactiveToken)
}
claims, ok := t.Claims.(jwt.MapClaims)
if !ok {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to type assert jwt claims to jwt.MapClaims."))
}
parsedClaims := jwtx.ParseMapStringInterfaceClaims(claims)
for _, audience := range r.Audiences {
if !stringslice.Has(parsedClaims.Audience, audience) {
return nil, herodot.ErrUnauthorized.WithReasonf("Token audience %v is not intended for target audience %s.", parsedClaims.Audience, audience)
}
}
if len(r.Issuers) > 0 {
if !stringslice.Has(r.Issuers, parsedClaims.Issuer) {
return nil, herodot.ErrUnauthorized.WithReasonf("Token issuer does not match any trusted issuer %s.", parsedClaims.Issuer).
WithDetail("received issuers", strings.Join(r.Issuers, ", "))
}
}
s, k := scope(claims)
delete(claims, k)
claims["scp"] = s
if r.ScopeStrategy != nil {
for _, sc := range r.Scope {
if !r.ScopeStrategy(s, sc) {
return nil, herodot.ErrUnauthorized.WithReasonf(`JSON Web Token is missing required scope "%s".`, sc)
}
}
} else {
if len(r.Scope) > 0 {
return nil, errors.WithStack(helper.ErrRuleFeatureDisabled.WithReason("Scope validation was requested but scope strategy is set to \"none\"."))
}
}
return t, nil
}
func scope(claims map[string]interface{}) ([]string, string) {
var ok bool
var interim interface{}
var key string
for _, k := range []string{"scp", "scope", "scopes"} {
if interim, ok = claims[k]; ok {
key = k
break
}
}
if !ok {
return []string{}, key
}
switch i := interim.(type) {
case []string:
return i, key
case []interface{}:
vs := make([]string, len(i))
for k, v := range i {
if vv, ok := v.(string); ok {
vs[k] = vv
}
}
return vs, key
case string:
return stringsx.Splitx(i, " "), key
default:
return []string{}, key
}
}