diff --git a/agent_holder/src/offer/aggregate.rs b/agent_holder/src/offer/aggregate.rs index 100b2f42..a0e9a3ba 100644 --- a/agent_holder/src/offer/aggregate.rs +++ b/agent_holder/src/offer/aggregate.rs @@ -65,7 +65,7 @@ impl Aggregate for Offer { .wallet .get_credential_offer(credential_offer_uri) .await - .unwrap(), + .map_err(|_| CredentialOfferByReferenceRetrievalError)?, CredentialOffer::CredentialOffer(credential_offer) => *credential_offer, }; @@ -76,7 +76,7 @@ impl Aggregate for Offer { let credential_issuer_metadata = wallet .get_credential_issuer_metadata(credential_issuer_url.clone()) .await - .unwrap(); + .map_err(|_| CredentialIssuerMetadataRetrievalError)?; let credential_configurations: HashMap = credential_issuer_metadata @@ -100,32 +100,40 @@ impl Aggregate for Offer { let wallet = &services.wallet; - let credential_issuer_url = self.credential_offer.as_ref().unwrap().credential_issuer.clone(); + let credential_offer = self.credential_offer.as_ref().ok_or(MissingCredentialOfferError)?; + + let credential_issuer_url = credential_offer.credential_issuer.clone(); // Get the authorization server metadata. let authorization_server_metadata = wallet .get_authorization_server_metadata(credential_issuer_url.clone()) .await - .unwrap(); + .map_err(|_| AuthorizationServerMetadataRetrievalError)?; // Create a token request with grant_type `pre_authorized_code`. - let token_request = match self.credential_offer.as_ref().unwrap().grants.clone() { + let token_request = match credential_offer.grants.clone() { Some(Grants { - pre_authorized_code, .. + pre_authorized_code: Some(pre_authorized_code), + .. }) => TokenRequest::PreAuthorizedCode { - pre_authorized_code: pre_authorized_code.unwrap().pre_authorized_code, + pre_authorized_code: pre_authorized_code.pre_authorized_code, tx_code: None, }, - None => unreachable!(), + _ => return Err(MissingPreAuthorizedCodeError), }; info!("token_request: {:?}", token_request); // Get an access token. let token_response = wallet - .get_access_token(authorization_server_metadata.token_endpoint.unwrap(), token_request) + .get_access_token( + authorization_server_metadata + .token_endpoint + .ok_or(MissingTokenEndpointError)?, + token_request, + ) .await - .unwrap(); + .map_err(|_| TokenResponseError)?; info!("token_response: {:?}", token_response); @@ -147,41 +155,40 @@ impl Aggregate for Offer { let wallet = &services.wallet; - let credential_issuer_url = self.credential_offer.as_ref().unwrap().credential_issuer.clone(); + let credential_offer = self.credential_offer.as_ref().ok_or(MissingCredentialOfferError)?; + + let credential_issuer_url = credential_offer.credential_issuer.clone(); // Get an access token. - let token_response = self.token_response.as_ref().unwrap().clone(); + let token_response = self.token_response.as_ref().ok_or(MissingTokenResponseError)?.clone(); - let credential_configuration_ids = self - .credential_offer - .as_ref() - .unwrap() - .credential_configuration_ids - .clone(); + let credential_configuration_ids = credential_offer.credential_configuration_ids.clone(); // Get the credential issuer metadata. let credential_issuer_metadata = wallet .get_credential_issuer_metadata(credential_issuer_url.clone()) .await - .unwrap(); + .map_err(|_| CredentialIssuerMetadataRetrievalError)?; + + let credential_configurations = self + .credential_configurations + .as_ref() + .ok_or(MissingCredentialConfigurationsError)?; let credentials: Vec = match credential_configuration_ids.len() { 0 => vec![], 1 => { - let credential_configuration_id = credential_configuration_ids[0].clone(); + let credential_configuration_id = &credential_configuration_ids[0]; - let credential_configuration = self - .credential_configurations - .as_ref() - .unwrap() - .get(&credential_configuration_id) - .unwrap(); + let credential_configuration = credential_configurations + .get(credential_configuration_id) + .ok_or(MissingCredentialConfigurationError)?; // Get the credential. let credential_response = wallet .get_credential(credential_issuer_metadata, &token_response, credential_configuration) .await - .unwrap(); + .map_err(|_| CredentialResponseError)?; let credential = match credential_response.credential { CredentialResponseType::Immediate { credential, .. } => credential, @@ -191,7 +198,7 @@ impl Aggregate for Offer { vec![credential] } _batch => { - todo!() + return Err(BatchCredentialRequestError); } }; diff --git a/agent_holder/src/offer/error.rs b/agent_holder/src/offer/error.rs index 7c44918a..71341451 100644 --- a/agent_holder/src/offer/error.rs +++ b/agent_holder/src/offer/error.rs @@ -2,8 +2,32 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum OfferError { + #[error("The Credential Offer could not be retrieved from the `credential_offer_uri`")] + CredentialOfferByReferenceRetrievalError, + #[error("The Credential Issuer Metadata could not be retrieved")] + CredentialIssuerMetadataRetrievalError, #[error("The Credential Offer has already been accepted and cannot be rejected anymore")] CredentialOfferStatusNotPendingError, + #[error("The Credential Offer is missing")] + MissingCredentialOfferError, + #[error("The Authorization Server Metadata could not be retrieved")] + AuthorizationServerMetadataRetrievalError, + #[error("The pre-authorized code is missing from the Credential Offer")] + MissingPreAuthorizedCodeError, + #[error("The Authorization Server Metadata is missing the `token_endpoint` parameter")] + MissingTokenEndpointError, + #[error("An error occurred while requesting the access token")] + TokenResponseError, #[error("The Credential Offer has not been accepted yet")] CredentialOfferStatusNotAcceptedError, + #[error("The Token Response is missing from the Credential Offer")] + MissingTokenResponseError, + #[error("The Credential Configurations are missing from the Credential Offer")] + MissingCredentialConfigurationsError, + #[error("The Credential Configuration is missing from the Credential Configurations")] + MissingCredentialConfigurationError, + #[error("An error occurred while requesting the credentials")] + CredentialResponseError, + #[error("Batch Credential Request are not supported")] + BatchCredentialRequestError, }