Repository navigation
Expand file tree
/
Copy pathdpop_client_example.dart
More file actions
99 lines (88 loc) · 4.05 KB
/
Copy pathdpop_client_example.dart
File metadata and controls
99 lines (88 loc) · 4.05 KB
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
// Signs proofs for a short request sequence, printing the claims a server
// would check. Nothing here talks to a network.
import 'dart:convert';
import 'package:dpop_client/dpop_client.dart';
/// Decodes a JWT segment for display. A real client never needs this — the
/// server does the reading.
Map<String, dynamic> decode(String segment) =>
jsonDecode(utf8.decode(base64Url.decode(base64Url.normalize(segment))))
as Map<String, dynamic>;
void main() {
// 1. Once per installation: generate a pair and persist the private PEM.
// In a real app that string belongs in the platform keystore.
final keyPair = DPoPKeyPair.generate();
final stored = keyPair.privateKeyPem;
// 2. On a later launch: restore it. The public half is derived, so the one
// string is all that has to be kept. Storage that holds JSON rather
// than PEM can keep `privateJwk` instead and restore it with
// `DPoPKeyPair.fromPrivateJwk` — the same key either way.
final restored = DPoPKeyPair.fromPrivateKeyPem(stored);
final viaJwk = DPoPKeyPair.fromPrivateJwk(
jsonDecode(jsonEncode(keyPair.privateJwk)) as Map<String, dynamic>,
);
print(
'PEM and JWK restore the same key: '
'${viaJwk.thumbprint == restored.thumbprint}',
);
print(
'thumbprint survives a restore: '
'${restored.thumbprint == keyPair.thumbprint}',
);
print('jkt to expect in the token\'s cnf claim: ${restored.thumbprint}');
// A nonce store keeps the most recent `DPoP-Nonce` each server issued, so
// no call site has to remember one. Optional: leave it out and pass
// `nonce:` yourself.
final nonces = DPoPNonceStore();
final client = DPoPClient(restored, nonces: nonces);
// 3. The token request, before any access token exists — no `ath`.
final tokenProof = client.createProof(
url: Uri.parse('https://auth.example.com/oauth2/token'),
method: 'POST',
);
print('\ntoken request -> ${decode(tokenProof.token.split('.')[1])}');
// 4. A resource request. Passing the access token adds `ath`, which is what
// binds this proof to that token.
const accessToken = 'stub.access.token';
final apiProof = client.createProof(
url: Uri.parse('https://api.example.com/v1/orders?limit=20&lang=en'),
method: 'POST',
accessToken: accessToken,
);
final claims = decode(apiProof.token.split('.')[1]);
print('\napi request -> $claims');
// Note the htu: the query string is gone, as RFC 9449 requires.
print('htu has no query: ${!(claims['htu'] as String).contains('?')}');
// 5. The server answers 401 with `error="use_dpop_nonce"` and a
// `DPoP-Nonce` header. Hand the response headers to the store: it
// returns true only for a nonce it had not already tried, which is the
// signal to retry once rather than forever.
final url = Uri.parse('https://api.example.com/v1/orders?limit=20&lang=en');
final worthRetrying = nonces.update(url, {
'DPoP-Nonce': 'eyJ7S_zG.eyJIOjki.hSfMzSHA',
'content-type': 'application/json',
});
print('\nnonce is new, so retry once: $worthRetrying');
// The retry needs no `nonce:` argument — the client reads the store.
final retried = client.createProof(
url: url,
method: 'POST',
accessToken: accessToken,
);
print('retry w/ nonce -> ${decode(retried.token.split('.')[1])}');
print(
'same nonce again is nothing new: '
'${nonces.update(url, const {'DPoP-Nonce': 'eyJ7S_zG.eyJIOjki.hSfMzSHA'})}',
);
print('\nheaders to send:');
// 6. Attaching it to a request. When the proof was built with an access
// token, `headers()` also returns the Authorization header — under the
// DPoP scheme, not Bearer, which RFC 9449 §7.1 requires for a bound
// token and which is the step most integrations miss.
retried.headers().forEach((name, value) {
final shown = value.length > 48 ? '${value.substring(0, 45)}...' : value;
print(' $name: $shown');
});
// A proof with no access token carries no Authorization header, matching
// the absent `ath` claim.
print('\ntoken request headers: ${tokenProof.headers().keys.toList()}');
}