|
9 | 9 | * SPDX-License-Identifier: MIT OR Apache-2.0 |
10 | 10 | */ |
11 | 11 |
|
12 | | -import 'dart:convert'; |
| 12 | +import 'package:cbor/cbor.dart'; |
13 | 13 | import 'package:dcaf/dcaf.dart'; |
14 | 14 |
|
15 | 15 | 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: |
16 | 27 | final request = AccessTokenRequest( |
17 | 28 | clientId: "myclient", |
18 | 29 | audience: "valve242", |
19 | 30 | scope: TextScope("read"), |
20 | 31 | 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: |
23 | 53 |
|
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); |
25 | 91 | } |
0 commit comments