Skip to content

Commit

Permalink
[ORANGE] oidc4vci refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkbee1 committed Jan 17, 2025
1 parent 9d443b0 commit b9dfc68
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 480 deletions.
161 changes: 0 additions & 161 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -685,168 +685,7 @@ bool isSIOPV2OROIDC4VPUrl(Uri uri) {
return isOpenIdUrl || isAuthorizeEndPoint || isSiopv2Url;
}

/// OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?,
/// credentialOfferJson, issuer, pre-authorizedCode
Future<Oidc4vcParameters> getIssuanceData({
required String url,
required DioClient client,
required OIDC4VC oidc4vc,
required OIDC4VCIDraftType oidc4vciDraftType,
required bool useOAuthAuthorizationServerLink,
}) async {
final uri = Uri.parse(url);

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

late Map<String, dynamic> credentialOfferJson;
String? issuer;
String? preAuthorizedCode;
bool userPinRequired = false;
TxCode? txCode;

if (keys.contains('credential_offer') ||
keys.contains('credential_offer_uri')) {
/// issuance case 2
credentialOfferJson = await getCredentialOffer(
scannedResponse: uri.toString(),
dioClient: client,
);

final grants = credentialOfferJson['grants'];

if (grants != null && grants is Map) {
final dynamic preAuthorizedCodeGrant =
grants['urn:ietf:params:oauth:grant-type:pre-authorized_code'];
if (preAuthorizedCodeGrant != null && preAuthorizedCodeGrant is Map) {
if (preAuthorizedCodeGrant.containsKey('pre-authorized_code')) {
preAuthorizedCode =
preAuthorizedCodeGrant['pre-authorized_code'] as String;
}
if (preAuthorizedCodeGrant.containsKey('user_pin_required')) {
userPinRequired = preAuthorizedCodeGrant['user_pin_required'] as bool;
} else if (preAuthorizedCodeGrant.containsKey('tx_code')) {
/// draft 13
final txCodeMap = preAuthorizedCodeGrant['tx_code'];

if (txCodeMap is Map<String, dynamic>) {
txCode = TxCode.fromJson(txCodeMap);
userPinRequired = true;
}
}
}

issuer = credentialOfferJson['credential_issuer'].toString();
}
} else {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'The credential offer is missing.',
},
);
}

if (keys.contains('issuer')) {
/// issuance case 1
issuer = uri.queryParameters['issuer'].toString();

/// preAuthorizedCode can be null
preAuthorizedCode = uri.queryParameters['pre-authorized_code'];
}

if (issuer == null) {
return Oidc4vcParameters(
oidc4vciDraftType: oidc4vciDraftType,
useOAuthAuthorizationServerLink: useOAuthAuthorizationServerLink,
initialUri: uri,
);
}

final issuerOpenIdConfiguration = await oidc4vc.getIssuerMetaData(
baseUrl: issuer,
dio: client.dio,
);

if (preAuthorizedCode == null) {
final grantTypesSupported = issuerOpenIdConfiguration.grantTypesSupported;
if (grantTypesSupported != null && grantTypesSupported.isNotEmpty) {
if (!grantTypesSupported.contains('authorization_code')) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'No grant specified.',
},
);
}
}
}

final Oidc4vcParameters oidc4vcParametersfromIssuer = Oidc4vcParameters(
oidc4vciDraftType: oidc4vciDraftType,
useOAuthAuthorizationServerLink: useOAuthAuthorizationServerLink,
initialUri: uri,
classIssuerOpenIdConfiguration: issuerOpenIdConfiguration,
classCredentialOffer: credentialOfferJson,
preAuthorizedCode: preAuthorizedCode,
classIssuer: issuer,
userPinRequired: userPinRequired,
txCode: txCode,
);
final Oidc4vcParameters oidc4vcParameters =
await oidc4vc.authorizationParameters(
oidc4vcParameters: oidc4vcParametersfromIssuer,
dio: Dio(),
);
final credentialsSupported = issuerOpenIdConfiguration.credentialsSupported;
final credentialConfigurationsSupported =
issuerOpenIdConfiguration.credentialConfigurationsSupported;

if (credentialsSupported == null &&
credentialConfigurationsSupported == null) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'The credential supported is missing.',
},
);
}

CredentialsSupported? credSupported;

if (credentialsSupported != null) {
credSupported = credentialsSupported[0];
}
for (final oidc4vcType in OIDC4VCType.values) {
if (oidc4vcType.isEnabled && url.startsWith(oidc4vcType.offerPrefix)) {
if (oidc4vcType == OIDC4VCType.DEFAULT ||
oidc4vcType == OIDC4VCType.EBSI) {
if (credSupported?.trustFramework != null &&
credSupported == credSupported?.trustFramework) {
return oidc4vcParameters.copyWith(
oidc4vcType: OIDC4VCType.DEFAULT,
);
}

if (credSupported?.trustFramework?.name != null &&
credSupported?.trustFramework?.name == 'ebsi') {
return oidc4vcParameters.copyWith(
oidc4vcType: OIDC4VCType.EBSI,
);
} else {
return oidc4vcParameters.copyWith(
oidc4vcType: OIDC4VCType.DEFAULT,
);
}
}
return oidc4vcParameters.copyWith(
oidc4vcType: oidc4vcType,
);
}
}

return oidc4vcParameters;
}

Future<void> handleErrorForOID4VCI({
required Oidc4vcParameters oidc4vcParameters,
Expand Down
Loading

0 comments on commit b9dfc68

Please sign in to comment.