Skip to content

Commit

Permalink
show token endpoint in developer mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Feb 28, 2024
1 parent c05744b commit 20afacc
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 203 deletions.
1 change: 1 addition & 0 deletions lib/app/shared/enum/status/qr_scan_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ enum QrScanStatus {
error,
success,
goBack,
pauseForDialog,
}
6 changes: 3 additions & 3 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1176,12 +1176,12 @@ $codeForAuthorisedFlow
}

String showFormatted({
required String title,
required Map<String, dynamic> jsonValue,
required String? title,
required Map<String, dynamic>? jsonValue,
}) {
return '''
<b>$title :</b>
${const JsonEncoder.withIndent(' ').convert(jsonValue)}
${jsonValue != null ? const JsonEncoder.withIndent(' ').convert(jsonValue) : 'None'}
''';
}

Expand Down
105 changes: 80 additions & 25 deletions lib/dashboard/qr_code/qr_code_scan/cubit/qr_code_scan_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
}
}

Completer<bool>? completer;

Future<void> addCredentialsInLoop({
required List<dynamic> selectedCredentials,
required bool isEBSIV3,
Expand All @@ -1213,31 +1215,84 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
final customOidc4vcProfile = profileCubit.state.model.profileSetting
.selfSovereignIdentityOptions.customOidc4vcProfile;

await getAndAddCredential(
scannedResponse: state.uri.toString(),
credentialsCubit: credentialsCubit,
oidc4vc: oidc4vc,
isEBSIV3: isEBSIV3,
didKitProvider: didKitProvider,
secureStorageProvider: getSecureStorage,
credential: selectedCredentials[i],
isLastCall: i + 1 == selectedCredentials.length,
dioClient: client,
userPin: userPin,
issuer: issuer,
preAuthorizedCode: preAuthorizedCode,
codeForAuthorisedFlow: codeForAuthorisedFlow,
codeVerifier: codeVerifier,
cryptoHolderBinding: customOidc4vcProfile.cryptoHolderBinding,
authorization: authorization,
oidc4vciDraftType: customOidc4vcProfile.oidc4vciDraft,
didKeyType: customOidc4vcProfile.defaultDid,
clientId: clientId,
clientSecret: clientSecret,
profileCubit: profileCubit,
jwtDecode: jwtDecode,
blockchainType: walletCubit.state.currentAccount!.blockchainType,
);
if (preAuthorizedCode != null ||
(codeForAuthorisedFlow != null && codeVerifier != null)) {
/// codeForAuthorisedFlow != null
/// this is second phase flow for authorization_code
/// first phase is need for the authentication
///
/// preAuthorizedCode != null
/// this is full phase flow for preAuthorizedCode
/// get credentials
final (
List<dynamic> encodedCredentialOrFutureTokens,
String? deferredCredentialEndpoint,
String format,
OpenIdConfiguration? openIdConfiguration,
Map<String, dynamic>? tokenResponse,
) = await getCredential(
oidc4vc: oidc4vc,
isEBSIV3: isEBSIV3,
didKitProvider: didKitProvider,
credential: selectedCredentials[i],
userPin: userPin,
issuer: issuer,
preAuthorizedCode: preAuthorizedCode,
codeForAuthorisedFlow: codeForAuthorisedFlow,
codeVerifier: codeVerifier,
cryptoHolderBinding: customOidc4vcProfile.cryptoHolderBinding,
authorization: authorization,
oidc4vciDraftType: customOidc4vcProfile.oidc4vciDraft,
didKeyType: customOidc4vcProfile.defaultDid,
clientId: clientId,
clientSecret: clientSecret,
profileCubit: profileCubit,
);

if (profileCubit.state.model.isDeveloperMode &&
tokenResponse != null) {
completer = Completer<bool>();
emit(
state.copyWith(
qrScanStatus: QrScanStatus.pauseForDialog,
title: 'TOKEN_RESPONSE',
jsonValue: tokenResponse,
),
);

final value = await completer!.future;

if (value) {
completer = null;
} else {
completer = null;
oidc4vc.resetNonceAndAccessTokenAndAuthorizationDetails();
goBack();
return;
}
}
await addCredentialData(
scannedResponse: state.uri.toString(),
credentialsCubit: credentialsCubit,
secureStorageProvider: getSecureStorage,
credential: selectedCredentials[i],
isLastCall: i + 1 == selectedCredentials.length,
issuer: issuer,
profileCubit: profileCubit,
jwtDecode: jwtDecode,
blockchainType: walletCubit.state.currentAccount!.blockchainType,
deferredCredentialEndpoint: deferredCredentialEndpoint,
encodedCredentialOrFutureTokens: encodedCredentialOrFutureTokens,
format: format,
openIdConfiguration: openIdConfiguration,
tokenResponse: tokenResponse,
);

/// add credentials
} else {
throw Exception();
}
}

oidc4vc.resetNonceAndAccessTokenAndAuthorizationDetails();
Expand Down
18 changes: 17 additions & 1 deletion lib/dashboard/qr_code/qr_code_scan/cubit/qr_code_scan_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class QRCodeScanState extends Equatable {
this.route,
this.isScan = false,
this.message,
this.title,
this.jsonValue,
});

factory QRCodeScanState.fromJson(Map<String, dynamic> json) =>
Expand All @@ -20,6 +22,8 @@ class QRCodeScanState extends Equatable {
final bool isScan;

final StateMessage? message;
final String? title;
final Map<String, dynamic>? jsonValue;

Map<String, dynamic> toJson() => _$QRCodeScanStateToJson(this);

Expand Down Expand Up @@ -54,16 +58,28 @@ class QRCodeScanState extends Equatable {
Route<dynamic>? route,
Uri? uri,
bool? isScan,
String? title,
Map<String, dynamic>? jsonValue,
}) {
return QRCodeScanState(
status: qrScanStatus,
message: message,
isScan: isScan ?? this.isScan,
uri: uri ?? this.uri,
route: route, // route should be cleared when one route is done
title: title,
jsonValue: jsonValue,
);
}

@override
List<Object?> get props => [status, uri, route, isScan, message];
List<Object?> get props => [
status,
uri,
route,
isScan,
message,
title,
jsonValue,
];
}
96 changes: 96 additions & 0 deletions lib/oidc4vc/add_credential_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import 'package:altme/app/app.dart';
import 'package:altme/credentials/credentials.dart';
import 'package:altme/dashboard/dashboard.dart';

import 'package:altme/oidc4vc/oidc4vc.dart';
import 'package:jwt_decode/jwt_decode.dart';
import 'package:oidc4vc/oidc4vc.dart';
import 'package:secure_storage/secure_storage.dart';
import 'package:uuid/uuid.dart';

Future<void> addCredentialData({
required List<dynamic> encodedCredentialOrFutureTokens,
required String? deferredCredentialEndpoint,
required String format,
required OpenIdConfiguration? openIdConfiguration,
required Map<String, dynamic>? tokenResponse,
required SecureStorageProvider secureStorageProvider,
required ProfileCubit profileCubit,
required CredentialsCubit credentialsCubit,
required String scannedResponse,
required dynamic credential,
required String issuer,
required bool isLastCall,
required JWTDecode jwtDecode,
required BlockchainType blockchainType,
}) async {
for (int i = 0; i < encodedCredentialOrFutureTokens.length; i++) {
final data = encodedCredentialOrFutureTokens[i];
final String credentialName = getCredentialData(credential);
final acceptanceToken = data['acceptance_token'];

if (acceptanceToken != null && deferredCredentialEndpoint != null) {
/// add deferred card
final id = const Uuid().v4();

final credentialModel = CredentialModel(
id: id,
credentialPreview: Credential(
'dummy1',
['dummy2'],
[credentialName],
'dummy4',
'dummy5',
'',
[Proof.dummy()],
CredentialSubjectModel(
id: 'dummy7',
type: 'dummy8',
issuedBy: const Author(''),
credentialCategory: CredentialCategory.pendingCards,
credentialSubjectType: CredentialSubjectType.defaultCredential,
),
[Translation('en', '')],
[Translation('en', '')],
CredentialStatusField.emptyCredentialStatusField(),
[Evidence.emptyEvidence()],
),
data: const {},
jwt: null,
format: 'jwt_vc',
image: '',
shareLink: '',
pendingInfo: PendingInfo(
acceptanceToken: acceptanceToken.toString(),
deferredCredentialEndpoint: deferredCredentialEndpoint,
format: format,
url: scannedResponse,
issuer: issuer,
requestedAt: DateTime.now(),
),
);
// insert the credential in the wallet
await credentialsCubit.insertCredential(
credential: credentialModel,
showStatus: false,
showMessage:
isLastCall && i + 1 == encodedCredentialOrFutureTokens.length,
isPendingCredential: true,
blockchainType: blockchainType,
);
} else {
await addOIDC4VCCredential(
encodedCredentialFromOIDC4VC: data,
credentialsCubit: credentialsCubit,
issuer: issuer,
credentialType: credentialName,
isLastCall:
isLastCall && i + 1 == encodedCredentialOrFutureTokens.length,
format: format,
openIdConfiguration: openIdConfiguration,
jwtDecode: jwtDecode,
blockchainType: blockchainType,
);
}
}
}
Loading

0 comments on commit 20afacc

Please sign in to comment.