Skip to content

Commit

Permalink
feat: Support DEFAULT profile VC issuance if credential_offer_uri is …
Browse files Browse the repository at this point in the history
…provided #1793
  • Loading branch information
bibash28 committed Aug 16, 2023
1 parent 59e4cdc commit 94355b4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
8 changes: 1 addition & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@
"request": "launch",
"type": "dart",
"program": "lib/main_production.dart",
"args": [
"--flavor",
"production",
"--target",
"lib/main_production.dart",
"--release"
]
"args": ["--flavor", "production", "--target", "lib/main_production.dart"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:beacon_flutter/beacon_flutter.dart';
import 'package:bloc/bloc.dart';
import 'package:credential_manifest/credential_manifest.dart';
import 'package:did_kit/did_kit.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
Expand Down Expand Up @@ -340,6 +341,7 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
Future<void> accept({
required Issuer issuer,
required QRCodeScanCubit qrCodeScanCubit,
required DioClient dioClient,
}) async {
emit(state.loading());
final log = getLogger('QRCodeScanCubit - accept');
Expand All @@ -366,6 +368,7 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
didKitProvider: didKitProvider,
qrCodeScanCubit: qrCodeScanCubit,
secureStorageProvider: getSecureStorage,
dioClient: dioClient,
);
return;
}
Expand Down Expand Up @@ -865,6 +868,7 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
secureStorageProvider: getSecureStorage,
credentialTypeOrId: credentialTypeOrId.toString(),
isLastCall: i + 1 == credentials.length,
dioClient: DioClient('', Dio()),
);
}
oidc4vc.resetNonceAndAccessToken();
Expand Down
46 changes: 42 additions & 4 deletions lib/oidc4vc/initiate_oidv4vc_credential_issuance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Future<void> initiateOIDC4VCCredentialIssuance({
required DIDKitProvider didKitProvider,
required CredentialsCubit credentialsCubit,
required SecureStorageProvider secureStorageProvider,
required DioClient dioClient,
}) async {
final Uri uriFromScannedResponse = Uri.parse(scannedResponse);

Expand All @@ -25,9 +26,12 @@ Future<void> initiateOIDC4VCCredentialIssuance({
switch (oidc4vcType) {
case OIDC4VCType.DEFAULT:
case OIDC4VCType.HEDERA:
final credentialOfferJson = jsonDecode(
uriFromScannedResponse.queryParameters['credential_offer'].toString(),
final dynamic credentialOfferJson = await getCredentialOfferJson(
scannedResponse: scannedResponse,
dioClient: dioClient,
);
if (credentialOfferJson == null) throw Exception();

credentialTypeOrId = credentialOfferJson['credentials'];
break;
case OIDC4VCType.GAIAX:
Expand All @@ -54,6 +58,7 @@ Future<void> initiateOIDC4VCCredentialIssuance({
credentialTypeOrId: credentialTypeOrId.toString(),
secureStorageProvider: secureStorageProvider,
isLastCall: true,
dioClient: dioClient,
);
oidc4vc.resetNonceAndAccessToken();
qrCodeScanCubit.goBack();
Expand All @@ -69,6 +74,7 @@ Future<void> getAndAddCredential({
required String credentialTypeOrId,
required SecureStorageProvider secureStorageProvider,
required bool isLastCall,
required DioClient dioClient,
}) async {
final Uri uriFromScannedResponse = Uri.parse(scannedResponse);

Expand All @@ -78,9 +84,12 @@ Future<void> getAndAddCredential({
switch (oidc4vcType) {
case OIDC4VCType.DEFAULT:
case OIDC4VCType.HEDERA:
final credentialOfferJson = jsonDecode(
uriFromScannedResponse.queryParameters['credential_offer'].toString(),
final dynamic credentialOfferJson = await getCredentialOfferJson(
scannedResponse: scannedResponse,
dioClient: dioClient,
);
if (credentialOfferJson == null) throw Exception();

preAuthorizedCode = credentialOfferJson['grants']
['urn:ietf:params:oauth:grant-type:pre-authorized_code']
['pre-authorized_code']
Expand Down Expand Up @@ -189,3 +198,32 @@ List<int> getThumbprint(Map<String, dynamic> privateKey) {

return sha256Digest.bytes;
}

Future<dynamic> getCredentialOfferJson({
required String scannedResponse,
required DioClient dioClient,
}) async {
final Uri uriFromScannedResponse = Uri.parse(scannedResponse);

final keys = <String>[];
uriFromScannedResponse.queryParameters.forEach((key, value) => keys.add(key));

dynamic credentialOfferJson;

if (keys.contains('credential_offer')) {
credentialOfferJson = jsonDecode(
uriFromScannedResponse.queryParameters['credential_offer'].toString(),
);
} else if (keys.contains('credential_offer_uri')) {
final url = uriFromScannedResponse.queryParameters['credential_offer_uri']
.toString();
final responseUrl = await dioClient.get(url);
final Uri uriFromResponseUrl = Uri.parse(responseUrl as String);

credentialOfferJson = jsonDecode(
uriFromResponseUrl.queryParameters['credential_offer'].toString(),
);
}

return credentialOfferJson;
}
1 change: 1 addition & 0 deletions lib/splash/bloclisteners/blocklisteners.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ final qrCodeBlocListener = BlocListener<QRCodeScanCubit, QRCodeScanState>(
await context.read<QRCodeScanCubit>().accept(
issuer: approvedIssuer,
qrCodeScanCubit: context.read<QRCodeScanCubit>(),
dioClient: DioClient('', Dio()),
);
} else {
context.read<QRCodeScanCubit>().emitError(
Expand Down
3 changes: 3 additions & 0 deletions lib/splash/view/splash_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:altme/polygon_id/polygon_id.dart';
import 'package:altme/splash/splash.dart';
import 'package:altme/theme/app_theme/app_theme.dart';
import 'package:did_kit/did_kit.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' as services;
Expand Down Expand Up @@ -157,13 +158,15 @@ class _SplashViewState extends State<SplashView> {
}

if (currentOIIDC4VCType != null) {
// ignore: require_trailing_commas
await initiateOIDC4VCCredentialIssuance(
scannedResponse: uri.toString(),
credentialsCubit: context.read<CredentialsCubit>(),
oidc4vcType: currentOIIDC4VCType,
didKitProvider: DIDKitProvider(),
qrCodeScanCubit: context.read<QRCodeScanCubit>(),
secureStorageProvider: secure_storage.getSecureStorage,
dioClient: DioClient('', Dio()),
);
}
}
Expand Down

0 comments on commit 94355b4

Please sign in to comment.