-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7be35a
commit edafaf8
Showing
8 changed files
with
134 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/dart/consumer_auth_provider/lib/src/consumer_auth_provider_interface.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
abstract interface class ConsumerAuthProviderInterface { | ||
Future<String> fetchConsumerToken(); | ||
Future<String> fetchCisToken(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/dart/consumer_auth_provider/lib/src/provider/cis_token_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
part of 'token_provider.dart'; | ||
|
||
class CisTokenProvider extends TokenProvider { | ||
static final String _tokenEndpoint = Environment.fetchConsumerCisUrl(); | ||
|
||
@override | ||
Future<String> getToken(Uint8List seedBytes) async { | ||
final myDiD = _getDID(seedBytes); | ||
final header = _getHeader(_getKid(myDiD)); | ||
return await _getJwtToken(seedBytes, header, _tokenEndpoint); | ||
} | ||
|
||
Map<String, dynamic> _getHeader(String kid) { | ||
return {'alg': 'ES256K', 'kid': kid, 'typ': 'openid4vci-proof+jwt'}; | ||
} | ||
} |
77 changes: 9 additions & 68 deletions
77
packages/dart/consumer_auth_provider/lib/src/provider/consumer_token_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/dart/consumer_auth_provider/lib/src/provider/token_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:affinidi_tdk_common/affinidi_tdk_common.dart'; | ||
import 'package:base_codecs/base_codecs.dart'; | ||
import 'package:bip32/bip32.dart'; | ||
import 'package:crypto/crypto.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
import 'package:web3dart/credentials.dart'; | ||
import 'package:web3dart/crypto.dart'; | ||
|
||
part 'consumer_token_provider.dart'; | ||
part 'cis_token_provider.dart'; | ||
|
||
abstract class TokenProvider { | ||
static const _etheriumIdentityKey = "m/44'/60'/0'/0/0"; | ||
|
||
Future<String> getToken(Uint8List seedBytes); | ||
|
||
Future<String> _getJwtToken(Uint8List seedBytes, Map<String, dynamic> header, String tokenEndpoint) async { | ||
final myDiD = _getDID(seedBytes); | ||
final jsonHeader = json.encode(header); | ||
final payload = json.encode( | ||
_getPayload(myDiD, tokenEndpoint), | ||
); | ||
final b64header = _base64Unpadded(base64UrlEncode(utf8.encode(jsonHeader))); | ||
final b64payload = _base64Unpadded(base64UrlEncode(utf8.encode(payload))); | ||
final msgHashHex = sha256.convert(utf8.encode("$b64header.$b64payload")).bytes; | ||
|
||
final walletKey = _getKey(seedBytes); | ||
final assertion = (walletKey.sign(Uint8List.fromList(msgHashHex))); | ||
return '$b64header.$b64payload.${_base64Unpadded( | ||
base64UrlEncode(Uint8List.fromList(assertion)), | ||
)}'; | ||
} | ||
|
||
String _getDID(Uint8List seedBytes) { | ||
final key = _getKey(seedBytes); | ||
final private = EthPrivateKey.fromHex(bytesToHex(key.privateKey!)); | ||
return 'did:key:z${base58BitcoinEncode( | ||
Uint8List.fromList([231, 1] + private.publicKey.getEncoded().toList()), | ||
)}'; | ||
} | ||
|
||
BIP32 _getKey(Uint8List seedBytes) { | ||
final master = BIP32.fromSeed(seedBytes); | ||
return master.derivePath(_etheriumIdentityKey); | ||
} | ||
|
||
String _getKid(String did) { | ||
return "$did#${did.substring("did:key:".length)}"; | ||
} | ||
|
||
String _base64Unpadded(String value) { | ||
if (value.endsWith('==')) return value.substring(0, value.length - 2); | ||
if (value.endsWith('=')) return value.substring(0, value.length - 1); | ||
return value; | ||
} | ||
|
||
Map<String, dynamic> _getPayload(String did, String tokenEndpoint) { | ||
final issueTimeS = (DateTime.timestamp().millisecondsSinceEpoch / 1000).floor(); | ||
final payload = { | ||
'iss': did, | ||
'sub': did, | ||
'aud': tokenEndpoint, | ||
'jti': const Uuid().v4(), | ||
'exp': issueTimeS + 5 * 60, | ||
'iat': issueTimeS, | ||
}; | ||
return payload; | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
packages/dart/consumer_auth_provider/test/affinidi_consumer_auth_provider_test.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters