Skip to content

Commit 608b172

Browse files
committed
docs: add proper example
1 parent 61fb692 commit 608b172

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

example/dcaf_example.dart

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,83 @@
99
* SPDX-License-Identifier: MIT OR Apache-2.0
1010
*/
1111

12-
import 'dart:convert';
12+
import 'package:cbor/cbor.dart';
1313
import 'package:dcaf/dcaf.dart';
1414

1515
void main() {
16+
// Creating and (de)serializing a Creation Hint:
17+
final scope = TextScope("rTempC");
18+
final hint = AuthServerRequestCreationHint(
19+
authorizationServer: "coaps://as.example.com/token",
20+
audience: "coaps://rs.example.com",
21+
scope: scope,
22+
clientNonce: [0xe0, 0xa1, 0x56, 0xbb, 0x3f]);
23+
final List<int> serializedHint = hint.serialize();
24+
25+
assert(AuthServerRequestCreationHint.fromSerialized(serializedHint) == hint);
26+
// Creating and (de)serializing an Access Token Request:
1627
final request = AccessTokenRequest(
1728
clientId: "myclient",
1829
audience: "valve242",
1930
scope: TextScope("read"),
2031
reqCnf: KeyId([0xDC, 0xAF]));
21-
final List<int> serialized = request.serialize();
22-
assert(AccessTokenRequest.fromSerialized(serialized) == request);
32+
final List<int> serializedRequest = request.serialize();
33+
assert(AccessTokenRequest.fromSerialized(serializedRequest) == request);
34+
35+
// Creating and (de)serializing an Access Token Response
36+
// which is represented in CBOR diagnostic notation like this...
37+
// {
38+
// "access_token" : b64'SlAV32hkKG ...
39+
// (remainder of CWT omitted for brevity;
40+
// CWT contains COSE_Key in the "cnf" claim)',
41+
// "ace_profile" : "coap_dtls",
42+
// "expires_in" : "3600",
43+
// "cnf" : {
44+
// "COSE_Key" : {
45+
// "kty" : "Symmetric",
46+
// "kid" : b64'39Gqlw',
47+
// "k" : b64'hJtXhkV8FJG+Onbc6mxCcQh'
48+
// }
49+
// }
50+
// }
51+
//
52+
// ...would look like this:
2353

24-
// TODO(falko17): Further examples, including responses, creation hints, etc.
54+
final key = CoseKey(keyType: KeyType.symmetric, keyId: [
55+
0xDF,
56+
0xD1,
57+
0xAA,
58+
0x97
59+
], parameters: {
60+
// field "k" (the key itself)
61+
-1: CborBytes([
62+
0x84,
63+
0x9b,
64+
0x57,
65+
0x86,
66+
0x45,
67+
0x7c,
68+
0x14,
69+
0x91,
70+
0xbe,
71+
0x3a,
72+
0x76,
73+
0xdc,
74+
0xea,
75+
0x6c,
76+
0x42,
77+
0x71,
78+
0x08
79+
])
80+
});
81+
final response = AccessTokenResponse(
82+
// NOTE: This is not what the access token is really supposed to be!
83+
// In an actual implementation, it should contain a properly
84+
// signed/encrypted CWT with the `key` defined above in its `cnf` claim!
85+
accessToken: [0xDC, 0xAF],
86+
aceProfile: AceProfile.coapDtls,
87+
expiresIn: 3600,
88+
cnf: PlainCoseKey(key));
89+
final serialized = response.serialize();
90+
assert(AccessTokenResponse.fromSerialized(serialized) == response);
2591
}

0 commit comments

Comments
 (0)