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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package oauth
import (
"cmp"
"context"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/ttlcache"
)
const (
HeaderDPoP = "DPoP"
HeaderDPoPNonce = "DPoP-Nonce"
AuthSchemeDPoP = "DPoP"
DefaultDPoPLeeway = 30 * time.Second
DefaultDPoPReplayWindow = 5 * time.Minute
DefaultNonceLifetime = 5 * time.Minute
DefaultNonceGrace = 30 * time.Second
)
type DPoPProof struct {
JKT string
JTI string
Nonce string
}
type DPoPVerifier struct {
Nonces NonceStore
Leeway time.Duration
ReplayWindow time.Duration
RequireNonce bool
}
func NewDPoPVerifier(nonces NonceStore) *DPoPVerifier {
return &DPoPVerifier{
Nonces: nonces,
Leeway: DefaultDPoPLeeway,
ReplayWindow: DefaultDPoPReplayWindow,
RequireNonce: true,
}
}
type dpopHeader struct {
Typ string `json:"typ"`
Alg string `json:"alg"`
JWK *dpopJWK `json:"jwk"`
}
type dpopJWK struct {
Kty string `json:"kty"`
Crv string `json:"crv"`
X string `json:"x"`
Y string `json:"y"`
D json.RawMessage `json:"d"`
}
type dpopPayload struct {
HTM string `json:"htm"`
HTU string `json:"htu"`
IAT json.Number `json:"iat"`
JTI string `json:"jti"`
ATH string `json:"ath"`
Nonce string `json:"nonce"`
Exp json.Number `json:"exp"`
}
func (v *DPoPVerifier) Verify(ctx context.Context, proof, method, requestURL, accessToken string) (*DPoPProof, error) {
if proof == "" {
return nil, fmt.Errorf("%w: missing DPoP proof", ErrUseDPoPNonce)
}
leeway := v.Leeway
leeway = opt.Positive(leeway, DefaultDPoPLeeway)
replayWindow := v.ReplayWindow
replayWindow = opt.Positive(replayWindow, DefaultDPoPReplayWindow)
headerPart, payloadPart, signaturePart, ok := splitJWT(proof)
if !ok {
return nil, fmt.Errorf("%w: malformed JWT", ErrInvalidDPoPProof)
}
var header dpopHeader
if err := decodeSegment(headerPart, &header); err != nil {
return nil, fmt.Errorf("%w: malformed header", ErrInvalidDPoPProof)
}
if header.Typ != "dpop+jwt" {
return nil, fmt.Errorf("%w: unexpected typ", ErrInvalidDPoPProof)
}
if header.Alg != "ES256" {
return nil, fmt.Errorf("%w: unexpected alg", ErrInvalidDPoPProof)
}
if header.JWK == nil {
return nil, fmt.Errorf("%w: missing jwk", ErrInvalidDPoPProof)
}
if header.JWK.Kty != "EC" || header.JWK.Crv != "P-256" {
return nil, fmt.Errorf("%w: unsupported jwk", ErrInvalidDPoPProof)
}
if len(header.JWK.D) > 0 && string(header.JWK.D) != "null" {
return nil, fmt.Errorf("%w: private jwk", ErrInvalidDPoPProof)
}
if header.JWK.X == "" || header.JWK.Y == "" {
return nil, fmt.Errorf("%w: incomplete jwk", ErrInvalidDPoPProof)
}
publicKey, err := atcrypto.ParsePublicJWK(atcrypto.JWK{
KeyType: header.JWK.Kty,
Curve: header.JWK.Crv,
X: header.JWK.X,
Y: header.JWK.Y,
})
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidDPoPProof, err)
}
signature, err := base64.RawURLEncoding.DecodeString(signaturePart)
if err != nil {
return nil, fmt.Errorf("%w: malformed signature", ErrInvalidDPoPProof)
}
if err := publicKey.HashAndVerifyLenient([]byte(headerPart+"."+payloadPart), signature); err != nil {
return nil, fmt.Errorf("%w: bad signature", ErrInvalidDPoPProof)
}
var payload dpopPayload
if err := decodeSegment(payloadPart, &payload); err != nil {
return nil, fmt.Errorf("%w: malformed payload", ErrInvalidDPoPProof)
}
if payload.HTM != strings.ToUpper(method) {
return nil, fmt.Errorf("%w: htm mismatch", ErrInvalidDPoPProof)
}
proofTarget, ok := normalizeHTU(payload.HTU)
if !ok {
return nil, fmt.Errorf("%w: invalid htu", ErrInvalidDPoPProof)
}
requestTarget, ok := normalizeHTU(requestURL)
if !ok {
return nil, fmt.Errorf("%w: invalid request URL", ErrInvalidDPoPProof)
}
if proofTarget != requestTarget {
return nil, fmt.Errorf("%w: htu mismatch", ErrInvalidDPoPProof)
}
if payload.JTI == "" {
return nil, fmt.Errorf("%w: missing jti", ErrInvalidDPoPProof)
}
iat, err := strconv.ParseInt(payload.IAT.String(), 10, 64)
if err != nil {
return nil, fmt.Errorf("%w: missing iat", ErrInvalidDPoPProof)
}
now := time.Now()
skew := now.Unix() - iat
if skew < 0 {
skew = -skew
}
if time.Duration(skew)*time.Second > leeway {
return nil, fmt.Errorf("%w: stale iat", ErrInvalidDPoPProof)
}
if payload.Exp != "" {
exp, err := strconv.ParseInt(payload.Exp.String(), 10, 64)
if err != nil {
return nil, fmt.Errorf("%w: invalid exp", ErrInvalidDPoPProof)
}
if now.Unix() >= exp {
return nil, fmt.Errorf("%w: expired proof", ErrInvalidDPoPProof)
}
}
if accessToken != "" {
if payload.ATH == "" {
return nil, fmt.Errorf("%w: missing ath", ErrInvalidDPoPProof)
}
sum := sha256.Sum256([]byte(accessToken))
expected := base64.RawURLEncoding.EncodeToString(sum[:])
if subtle.ConstantTimeCompare([]byte(payload.ATH), []byte(expected)) != 1 {
return nil, fmt.Errorf("%w: ath mismatch", ErrInvalidDPoPProof)
}
}
if payload.Nonce != "" || v.RequireNonce {
if payload.Nonce == "" {
return nil, fmt.Errorf("%w: missing nonce", ErrUseDPoPNonce)
}
if v.Nonces == nil {
return nil, fmt.Errorf("%w: no nonce store", ErrUseDPoPNonce)
}
origin, ok := dpopOrigin(requestURL)
if !ok {
return nil, fmt.Errorf("%w: invalid request URL", ErrInvalidDPoPProof)
}
valid, err := v.Nonces.VerifyNonce(ctx, origin, payload.Nonce)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrUseDPoPNonce, err)
}
if !valid {
return nil, fmt.Errorf("%w: unknown nonce", ErrUseDPoPNonce)
}
}
if v.Nonces != nil {
fresh, err := v.Nonces.RecordJTI(ctx, "dpop:"+payload.JTI, now.Add(replayWindow))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidDPoPProof, err)
}
if !fresh {
return nil, fmt.Errorf("%w: replayed jti", ErrInvalidDPoPProof)
}
}
return &DPoPProof{
JKT: jktFromJWK(header.JWK),
JTI: payload.JTI,
Nonce: payload.Nonce,
}, nil
}
func jktFromJWK(jwk *dpopJWK) string {
canonical := struct {
Crv string `json:"crv"`
Kty string `json:"kty"`
X string `json:"x"`
Y string `json:"y"`
}{Crv: jwk.Crv, Kty: jwk.Kty, X: jwk.X, Y: jwk.Y}
raw, err := json.Marshal(canonical)
if err != nil {
return ""
}
sum := sha256.Sum256(raw)
return base64.RawURLEncoding.EncodeToString(sum[:])
}
func normalizeHTU(raw string) (string, bool) {
parsed, err := url.Parse(raw)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return "", false
}
scheme := strings.ToLower(parsed.Scheme)
host := strings.ToLower(parsed.Hostname())
if host == "" {
return "", false
}
port := parsed.Port()
if (scheme == "https" && port == "443") || (scheme == "http" && port == "80") {
port = ""
}
if strings.Contains(host, ":") {
host = "[" + host + "]"
}
if port != "" {
host = host + ":" + port
}
path := parsed.EscapedPath()
path = cmp.Or(path, "/")
return scheme + "://" + host + path, true
}
func dpopOrigin(raw string) (string, bool) {
parsed, err := url.Parse(raw)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return "", false
}
scheme := strings.ToLower(parsed.Scheme)
host := strings.ToLower(parsed.Hostname())
if host == "" {
return "", false
}
port := parsed.Port()
if (scheme == "https" && port == "443") || (scheme == "http" && port == "80") {
port = ""
}
if strings.Contains(host, ":") {
host = "[" + host + "]"
}
if port != "" {
host = host + ":" + port
}
return scheme + "://" + host, true
}
type memoryNonceEntry struct {
current string
previous string
previousExpires time.Time
}
type MemoryNonceStore struct {
issueMu sync.Mutex
origins *ttlcache.Cache[string, memoryNonceEntry]
jtis *ttlcache.Cache[string, struct{}]
}
func NewMemoryNonceStore() *MemoryNonceStore {
return &MemoryNonceStore{
origins: ttlcache.New[string, memoryNonceEntry](ttlcache.Options{TTL: DefaultNonceLifetime}),
jtis: ttlcache.New[string, struct{}](ttlcache.Options{}),
}
}
func (s *MemoryNonceStore) IssueNonce(_ context.Context, origin string) (string, error) {
nonce, err := randomToken(16)
if err != nil {
return "", err
}
s.issueMu.Lock()
defer s.issueMu.Unlock()
entry, _ := s.origins.Get(origin)
entry.previous = entry.current
if entry.current != "" {
entry.previousExpires = time.Now().Add(DefaultNonceGrace)
}
entry.current = nonce
s.origins.Set(origin, entry)
return nonce, nil
}
func (s *MemoryNonceStore) VerifyNonce(_ context.Context, origin, nonce string) (bool, error) {
if nonce == "" {
return false, nil
}
entry, ok := s.origins.Get(origin)
if !ok {
return false, nil
}
if entry.current == nonce {
return true, nil
}
if entry.previous != "" && entry.previous == nonce && time.Now().Before(entry.previousExpires) {
return true, nil
}
return false, nil
}
func (s *MemoryNonceStore) RecordJTI(_ context.Context, jti string, expiresAt time.Time) (bool, error) {
ttl := time.Until(expiresAt)
ttl = opt.Positive(ttl, -1)
return s.jtis.Add(jti, struct{}{}, ttl), nil
}
var _ NonceStore = (*MemoryNonceStore)(nil)