loadCredentialOffer method
Loads a credential offer from the given uri and returns a OID4VCIClaimContext
containing both the credential offer and the issuer metadata.
uri - The URI containing query parameters for the credential offer.
Returns a OID4VCIClaimContext with the loaded credential offer and issuer metadata.
Throws:
- TdkException if the credential offer URI is missing or if loading fails.
- server_error: when server is unable to process the claim for some unexpected reason.
- missing_uri: when the uri does not have a credential_offer query parameter.
- failed_to_load_credential_offer: when the credential offer specified in the uri parameter does not exist.
- credential_offer_expired: when the credential offer specified in the uri parameter has expired.
Example:
final claimContext = await service.loadCredentialOffer(
Uri.parse('https://example.com?credentialOfferUri=https://issuer.example.com/offers/123'),
);
Log.info('Loaded offer from: ${claimContext.credentialOffer}', component: 'YourComponent');
Implementation
@override
Future<OID4VCIClaimContext> loadCredentialOffer(Uri uri) async {
_logger.info('Started loading credential offer', component: _componentName);
final offerPayload = uri.queryParameters;
final credentialOfferUri =
offerPayload[_OfferPayloadParams.credentialOfferUri];
if (credentialOfferUri == null || credentialOfferUri.isEmpty) {
_logger.error(
'Failed loading credential offer',
component: _componentName,
);
Error.throwWithStackTrace(
TdkException(
message: 'Credential offer uri is missing',
code: TdkExceptionType.missingUri.code,
),
StackTrace.current,
);
}
try {
final issuerMetadataResponse = await _claimVerifiableCredentialApiService
.getIssuerMetadata(offerUri: credentialOfferUri);
final issuerMetadata = OID4VCIIssuerMetadata.fromJson(
issuerMetadataResponse.data as Map<String, dynamic>,
);
final credentialOfferResponse = await _getCredentialsOfferResponse(
credentialOfferUri,
);
final credentialOffer = OID4VCICredentialOffer.fromJson(
credentialOfferResponse.data as Map<String, dynamic>,
);
_logger.info(
'Completed loading credential offer',
component: _componentName,
);
return OID4VCIClaimContext(
issuerMetadata: issuerMetadata,
credentialOffer: credentialOffer,
);
} catch (e, stackTrace) {
_logger.error(
'Failed loading credential offer',
component: _componentName,
error: e,
stackTrace: stackTrace,
);
if (e is TdkException) {
rethrow;
}
if (e is DioException) {
final tdkException = e.asTdkException();
if (tdkException != null) {
Error.throwWithStackTrace(tdkException, stackTrace);
}
}
Error.throwWithStackTrace(
TdkException(
message: 'Failed to load credential offer',
originalMessage: e.toString(),
code: TdkExceptionType.failedToLoadCredentialOffer.code,
),
stackTrace,
);
}
}