| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"time" |
| 7 |
|
| 8 |
jose "github.com/go-jose/go-jose/v4" |
| 9 |
|
| 10 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 11 |
) |
| 12 |
|
| 13 |
// JWTProfileVerifier extends oidc.Verifier with |
| 14 |
// a jwtProfileKeyStorage and a function to check |
| 15 |
// the subject in a token. |
| 16 |
type JWTProfileVerifier struct { |
| 17 |
oidc.Verifier |
| 18 |
Storage JWTProfileKeyStorage |
| 19 |
keySet oidc.KeySet |
| 20 |
CheckSubject func(request *oidc.JWTTokenRequest) error |
| 21 |
} |
| 22 |
|
| 23 |
// NewJWTProfileVerifier creates an oidc.Verifier for JWT Profile assertions (authorization grant and client authentication) |
| 24 |
func NewJWTProfileVerifier(storage JWTProfileKeyStorage, issuer string, maxAgeIAT, offset time.Duration, opts ...JWTProfileVerifierOption) *JWTProfileVerifier { |
| 25 |
return newJWTProfileVerifier(storage, nil, issuer, maxAgeIAT, offset, opts...) |
| 26 |
} |
| 27 |
|
| 28 |
// NewJWTProfileVerifierKeySet creates an oidc.Verifier for JWT Profile assertions (authorization grant and client authentication) |
| 29 |
func NewJWTProfileVerifierKeySet(keySet oidc.KeySet, issuer string, maxAgeIAT, offset time.Duration, opts ...JWTProfileVerifierOption) *JWTProfileVerifier { |
| 30 |
return newJWTProfileVerifier(nil, keySet, issuer, maxAgeIAT, offset, opts...) |
| 31 |
} |
| 32 |
|
| 33 |
func newJWTProfileVerifier(storage JWTProfileKeyStorage, keySet oidc.KeySet, issuer string, maxAgeIAT, offset time.Duration, opts ...JWTProfileVerifierOption) *JWTProfileVerifier { |
| 34 |
j := &JWTProfileVerifier{ |
| 35 |
Verifier: oidc.Verifier{ |
| 36 |
Issuer: issuer, |
| 37 |
MaxAgeIAT: maxAgeIAT, |
| 38 |
Offset: offset, |
| 39 |
}, |
| 40 |
Storage: storage, |
| 41 |
keySet: keySet, |
| 42 |
CheckSubject: SubjectIsIssuer, |
| 43 |
} |
| 44 |
|
| 45 |
for _, opt := range opts { |
| 46 |
opt(j) |
| 47 |
} |
| 48 |
|
| 49 |
return j |
| 50 |
} |
| 51 |
|
| 52 |
type JWTProfileVerifierOption func(*JWTProfileVerifier) |
| 53 |
|
| 54 |
// SubjectCheck sets a custom function to check the subject. |
| 55 |
// Defaults to SubjectIsIssuer() |
| 56 |
func SubjectCheck(check func(request *oidc.JWTTokenRequest) error) JWTProfileVerifierOption { |
| 57 |
return func(verifier *JWTProfileVerifier) { |
| 58 |
verifier.CheckSubject = check |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// VerifyJWTAssertion verifies the assertion string from JWT Profile (authorization grant and client authentication) |
| 63 |
// |
| 64 |
// checks audience, exp, iat, signature and that issuer and sub are the same |
| 65 |
func VerifyJWTAssertion(ctx context.Context, assertion string, v *JWTProfileVerifier) (*oidc.JWTTokenRequest, error) { |
| 66 |
ctx, span := Tracer.Start(ctx, "VerifyJWTAssertion") |
| 67 |
defer span.End() |
| 68 |
|
| 69 |
request := new(oidc.JWTTokenRequest) |
| 70 |
payload, err := oidc.ParseToken(assertion, request) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
if err = oidc.CheckAudience(request, v.Issuer); err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
if err = oidc.CheckExpiration(request, v.Offset); err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
if err = oidc.CheckIssuedAt(request, v.MaxAgeIAT, v.Offset); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
if err = v.CheckSubject(request); err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
|
| 91 |
keySet := v.keySet |
| 92 |
if keySet == nil { |
| 93 |
keySet = &jwtProfileKeySet{storage: v.Storage, clientID: request.Issuer} |
| 94 |
} |
| 95 |
if err = oidc.CheckSignature(ctx, assertion, payload, request, nil, keySet); err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
return request, nil |
| 99 |
} |
| 100 |
|
| 101 |
type JWTProfileKeyStorage interface { |
| 102 |
GetKeyByIDAndClientID(ctx context.Context, keyID, clientID string) (*jose.JSONWebKey, error) |
| 103 |
} |
| 104 |
|
| 105 |
// SubjectIsIssuer |
| 106 |
func SubjectIsIssuer(request *oidc.JWTTokenRequest) error { |
| 107 |
if request.Issuer != request.Subject { |
| 108 |
return oidc.ErrSubjectInvalid |
| 109 |
} |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
type jwtProfileKeySet struct { |
| 114 |
storage JWTProfileKeyStorage |
| 115 |
clientID string |
| 116 |
} |
| 117 |
|
| 118 |
// VerifySignature implements oidc.KeySet by getting the public key from Storage implementation |
| 119 |
func (k *jwtProfileKeySet) VerifySignature(ctx context.Context, jws *jose.JSONWebSignature) (payload []byte, err error) { |
| 120 |
ctx, span := Tracer.Start(ctx, "VerifySignature") |
| 121 |
defer span.End() |
| 122 |
|
| 123 |
keyID, _ := oidc.GetKeyIDAndAlg(jws) |
| 124 |
key, err := k.storage.GetKeyByIDAndClientID(ctx, keyID, k.clientID) |
| 125 |
if err != nil { |
| 126 |
return nil, fmt.Errorf("error fetching keys: %w", err) |
| 127 |
} |
| 128 |
return jws.Verify(key) |
| 129 |
} |