-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof.go
117 lines (98 loc) · 4.13 KB
/
proof.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
package dpop
import (
"errors"
"github.com/golang-jwt/jwt/v5"
)
// These claims contains the standard fields of a DPoP proof claim.
//
// If there is a need for custom claims this can be embedded
// in custom claims to ensure that claims are still possible to validate with the Validate function.
type ProofTokenClaims struct {
*jwt.RegisteredClaims
// the `htm` (HTTP Method) claim. See https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.2
Method HTTPVerb `json:"htm"`
// the `htu` (HTTP URL) claim. See https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.2
URL string `json:"htu"`
// the `ath` (Authorization Token Hash) claim. See https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.2
AccessTokenHash string `json:"ath,omitempty"`
// the `nonce` claim. See https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.2
Nonce string `json:"nonce,omitempty"`
}
// Implement the ProofClaims interface.
func (p ProofTokenClaims) GetAccessTokenHash() (string, error) {
return p.AccessTokenHash, nil
}
// This interface allows for custom claims to be used in proof tokens.
//
// As long as any custom claims extends the 'ProofTokenClaims' they will implement this interface
// and 'Validate' should handle them correctly
type ProofClaims interface {
jwt.Claims
GetAccessTokenHash() (string, error)
}
// Represents a DPoP proof, if acquired through the Parse function it should be a valid DPoP proof.
//
// However if a bound access token was recieved with the proof the Validate function needs be used to verify that the proof is valid for the access token.
type Proof struct {
*jwt.Token
HashedPublicKey string
}
// Validate takes a bound access token and validate that the token is bound correctly to this DPoP proof.
// This satisfies point 12 in https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.3
//
// Validate needs to be called by a protected resource after parsing the DPoP proof.
// A bound access token needs to be introspected by the resource server in order for claims to be availabe for validation.
//
// The bound access token should be validated before calling this function
// and the claims of the introspected bound access token needs to be of the BoundAccessTokenClaims type.
//
// The access token hash needs to be a URL encoded SHA256 hash of the access token.
//
// If no error is returned the proof is valid for the supplied bound token.
func (t *Proof) Validate(accessTokenHash []byte, boundAccessTokenJWT *jwt.Token) error {
// Make sure the proof token claims are of the correct type.
claims, ok := t.Claims.(ProofClaims)
if !ok {
return errors.Join(ErrInvalidProof, ErrIncorrectClaimsType)
}
proofAccessTokenHash, err := claims.GetAccessTokenHash()
if err != nil {
return errors.Join(ErrInvalidProof, err)
}
// Check that proof has a bound access token
if proofAccessTokenHash == "" {
return errors.Join(ErrInvalidProof, ErrMissingAth)
}
// Control that bound token in proof matches supplied token
if proofAccessTokenHash != string(accessTokenHash) {
return errors.Join(ErrInvalidProof, ErrAthMismatch)
}
// Check that proof has a key
b64URLjwkHash := t.PublicKey()
if b64URLjwkHash == "" {
return errors.Join(ErrInvalidProof, ErrMissingJWK)
}
// Make sure bound access token claims are of the correct type.
boundTokenClaims, ok := (boundAccessTokenJWT.Claims).(BoundClaims)
if !ok {
return ErrIncorrectAccessTokenClaimsType
}
// Check that key in proof matches bound token key
jkt, err := boundTokenClaims.GetJWKThumbprint()
if err != nil {
return errors.Join(ErrIncorrectAccessTokenClaimsType, err)
}
if jkt != b64URLjwkHash {
return errors.Join(ErrInvalidProof, ErrJWKMismatch)
}
return nil
}
// Get the public key from the proof.
//
// The public key string is base64 and url encoded according to https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-6.1
// in order to help comparison of proof public key and 'jkt' claim of a bound access token.
//
// An authorization server can use this to get the 'jkt' value it should encode in the bound access token.
func (t *Proof) PublicKey() string {
return t.HashedPublicKey
}