Skip to content

Commit

Permalink
feat: Re-request credential if error gives c_nonce #2734
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Jun 26, 2024
1 parent b27c2b1 commit 7127167
Showing 1 changed file with 77 additions and 32 deletions.
109 changes: 77 additions & 32 deletions packages/oidc4vc/lib/src/oidc4vc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,8 @@ class OIDC4VC {
return (tokenResponse, accessToken, cnonce, authorizationDetails);
}

int count = 0;

Future<dynamic> getSingleCredential({
required IssuerTokenParameters issuerTokenParameters,
required OpenIdConfiguration openIdConfiguration,
Expand All @@ -644,44 +646,87 @@ class OIDC4VC {
required String? nonce,
required Dio dio,
}) async {
final credentialData = await buildCredentialData(
nonce: nonce,
issuerTokenParameters: issuerTokenParameters,
openIdConfiguration: openIdConfiguration,
credentialType: credentialType,
types: types,
format: format,
credentialIdentifier: credentialIdentifier,
cryptoHolderBinding: cryptoHolderBinding,
oidc4vciDraftType: oidc4vciDraftType,
credentialDefinition: credentialDefinition,
clientAuthentication: clientAuthentication,
vct: vct,
proofType: proofType,
did: did,
issuer: issuer,
kid: kid,
privateKey: privateKey,
);
try {
final credentialData = await buildCredentialData(
nonce: nonce,
issuerTokenParameters: issuerTokenParameters,
openIdConfiguration: openIdConfiguration,
credentialType: credentialType,
types: types,
format: format,
credentialIdentifier: credentialIdentifier,
cryptoHolderBinding: cryptoHolderBinding,
oidc4vciDraftType: oidc4vciDraftType,
credentialDefinition: credentialDefinition,
clientAuthentication: clientAuthentication,
vct: vct,
proofType: proofType,
did: did,
issuer: issuer,
kid: kid,
privateKey: privateKey,
);

/// sign proof
/// sign proof
final credentialEndpoint = readCredentialEndpoint(openIdConfiguration);
final credentialEndpoint = readCredentialEndpoint(openIdConfiguration);

final credentialHeaders = <String, dynamic>{
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
};
final credentialHeaders = <String, dynamic>{
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
};

final dynamic credentialResponse = await dio.post<dynamic>(
credentialEndpoint,
options: Options(headers: credentialHeaders),
data: credentialData,
);
final dynamic credentialResponse = await dio.post<dynamic>(
credentialEndpoint,
options: Options(headers: credentialHeaders),
data: credentialData,
);

final credentialResponseData = credentialResponse.data;
final credentialResponseData = credentialResponse.data;

return credentialResponseData;
return credentialResponseData;
} catch (e) {
if (count == 1) {
count = 0;
rethrow;
}

if (e is DioException &&
e.response != null &&
e.response!.data is Map<String, dynamic> &&
(e.response!.data as Map<String, dynamic>).containsKey('c_nonce')) {
count++;

final nonce = e.response!.data['c_nonce'].toString();

final credentialResponseDataValue = await getSingleCredential(
issuerTokenParameters: issuerTokenParameters,
openIdConfiguration: openIdConfiguration,
credentialType: credentialType,
types: types,
format: format,
cryptoHolderBinding: cryptoHolderBinding,
oidc4vciDraftType: oidc4vciDraftType,
credentialDefinition: credentialDefinition,
clientAuthentication: clientAuthentication,
vct: vct,
credentialIdentifier: null,
proofType: proofType,
did: did,
issuer: issuer,
kid: kid,
privateKey: privateKey,
accessToken: accessToken,
nonce: nonce,
dio: dio,
);
count = 0;
return credentialResponseDataValue;
} else {
count = 0;
rethrow;
}
}
}

/// get Deferred credential from url
Expand Down

0 comments on commit 7127167

Please sign in to comment.