Skip to content

Commit 2098e81

Browse files
Add ath and nonce as addtional parameters for genDpopToken function
1 parent 6fbabbb commit 2098e81

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

example/lib/screens/PrivateProfile.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434
library;
3535

3636
// Flutter imports:
37+
import 'dart:convert';
38+
3739
import 'package:flutter/material.dart';
3840

3941
import 'package:solid_auth/solid_auth.dart';
42+
import 'package:crypto/crypto.dart';
4043

41-
import 'package:solid_auth_example/components/Header.dart';
4244
// Project imports:
45+
import 'package:solid_auth_example/components/Header.dart';
4346
import 'package:solid_auth_example/models/Constants.dart';
4447
import 'package:solid_auth_example/models/GetRdfData.dart';
4548
import 'package:solid_auth_example/models/SolidApi.dart' as rest_api;
@@ -184,10 +187,33 @@ class _PrivateProfileState extends State<PrivateProfile> {
184187
String accessToken = authData['accessToken'];
185188
//Map<String, dynamic> decodedToken = JwtDecoder.decode(accessToken);
186189

190+
// Optional: If needed one can provide encoded access token to be included
191+
// in the dPoP proof. Currently this is not required for any of our
192+
// community solid server configurations.
193+
194+
// Convert access token to ASCII bytes
195+
// List<int> accesTokenBytes = utf8.encode(accessToken);
196+
List<int> accesTokenBytes = accessToken.codeUnits;
197+
198+
// Hash using SHA-256
199+
Digest hash = sha256.convert(accesTokenBytes);
200+
201+
// Convert hash to bytes for encoding
202+
List<int> hashBytes = hash.bytes;
203+
204+
// Base64URL encoding
205+
String base64UrlHash = base64Url.encode(hashBytes);
206+
187207
// Get profile
188208
String profCardUrl = webId.replaceAll('#me', '');
189-
String dPopToken =
190-
genDpopToken(profCardUrl, rsaKeyPair, publicKeyJwk, 'GET');
209+
String dPopToken = genDpopToken(
210+
profCardUrl,
211+
rsaKeyPair,
212+
publicKeyJwk,
213+
'GET',
214+
// Optional parameter ath to provide encoded access token
215+
// ath: base64UrlHash,
216+
);
191217

192218
return Scaffold(
193219
key: _scaffoldKey,

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ environment:
99
dependencies:
1010
flutter:
1111
sdk: flutter
12+
crypto: ^3.0.6
1213
http: any
1314
jwt_decoder: ^2.0.1
1415
# solid_auth: ^0.1.6

lib/solid_auth_client.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ String genDpopToken(
121121
String endPointUrl,
122122
KeyPair rsaKeyPair,
123123
dynamic publicKeyJwk,
124-
String httpMethod,
125-
) {
124+
String httpMethod, {
125+
String? ath,
126+
String? nonce,
127+
}) {
126128
/// https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop-03
127129
/// Unique identifier for DPoP proof JWT
128130
/// Here we are using a version 4 UUID according to https://datatracker.ietf.org/doc/html/rfc4122
@@ -141,6 +143,14 @@ String genDpopToken(
141143
'iat': (DateTime.now().millisecondsSinceEpoch / 1000).round(),
142144
};
143145

146+
if (ath != null) {
147+
tokenBody['ath'] = ath;
148+
}
149+
150+
if (nonce != null) {
151+
tokenBody['nonce'] = nonce;
152+
}
153+
144154
/// Create a json web token
145155
final jwt = JWT(
146156
tokenBody,
@@ -226,8 +236,12 @@ Future<Map> authenticate(
226236
var publicKeyJwk = rsaResults['pubKeyJwk'];
227237

228238
///Generate DPoP token using the RSA private key
229-
String dPopToken =
230-
genDpopToken(tokenEndpoint, rsaKeyPair, publicKeyJwk, 'POST');
239+
String dPopToken = genDpopToken(
240+
tokenEndpoint,
241+
rsaKeyPair,
242+
publicKeyJwk,
243+
'POST',
244+
);
231245

232246
final String clientId = regResJson['client_id'];
233247
final String clientSecret = regResJson['client_secret'];

0 commit comments

Comments
 (0)