|
| 1 | +use agent_issuance::{ |
| 2 | + server_config::{command::ServerConfigCommand, queries::ServerConfigView}, |
| 3 | + state::{IssuanceState, SERVER_CONFIG_ID}, |
| 4 | +}; |
| 5 | +use agent_shared::handlers::{command_handler, query_handler}; |
| 6 | +use axum::{ |
| 7 | + extract::{Json, State}, |
| 8 | + http::StatusCode, |
| 9 | + response::{IntoResponse, Response}, |
| 10 | +}; |
| 11 | +use oid4vci::{ |
| 12 | + credential_format_profiles::{CredentialFormats, WithParameters}, |
| 13 | + credential_issuer::credential_issuer_metadata::CredentialIssuerMetadata, |
| 14 | +}; |
| 15 | +use serde::{Deserialize, Serialize}; |
| 16 | +use serde_json::Value; |
| 17 | +use tracing::info; |
| 18 | + |
| 19 | +#[derive(Deserialize, Serialize)] |
| 20 | +#[serde(rename_all = "camelCase")] |
| 21 | +pub struct CredentialConfigurationsEndpointRequest { |
| 22 | + pub credential_configuration_id: String, |
| 23 | + #[serde(flatten)] |
| 24 | + pub credential_format_with_parameters: CredentialFormats<WithParameters>, |
| 25 | + #[serde(default)] |
| 26 | + pub display: Vec<serde_json::Value>, |
| 27 | +} |
| 28 | + |
| 29 | +#[axum_macros::debug_handler] |
| 30 | +pub(crate) async fn credential_configurations( |
| 31 | + State(state): State<IssuanceState>, |
| 32 | + Json(payload): Json<Value>, |
| 33 | +) -> Response { |
| 34 | + info!("Request Body: {}", payload); |
| 35 | + |
| 36 | + let Ok(CredentialConfigurationsEndpointRequest { |
| 37 | + credential_configuration_id, |
| 38 | + credential_format_with_parameters, |
| 39 | + display, |
| 40 | + }) = serde_json::from_value(payload) |
| 41 | + else { |
| 42 | + return (StatusCode::BAD_REQUEST, "invalid payload").into_response(); |
| 43 | + }; |
| 44 | + |
| 45 | + let command = ServerConfigCommand::AddCredentialConfiguration { |
| 46 | + credential_configuration_id, |
| 47 | + credential_format_with_parameters, |
| 48 | + display, |
| 49 | + }; |
| 50 | + |
| 51 | + if command_handler(SERVER_CONFIG_ID, &state.command.server_config, command) |
| 52 | + .await |
| 53 | + .is_err() |
| 54 | + { |
| 55 | + return StatusCode::INTERNAL_SERVER_ERROR.into_response(); |
| 56 | + } |
| 57 | + |
| 58 | + match query_handler(SERVER_CONFIG_ID, &state.query.server_config).await { |
| 59 | + Ok(Some(ServerConfigView { |
| 60 | + credential_issuer_metadata: |
| 61 | + Some(CredentialIssuerMetadata { |
| 62 | + credential_configurations_supported, |
| 63 | + .. |
| 64 | + }), |
| 65 | + .. |
| 66 | + })) => (StatusCode::OK, Json(credential_configurations_supported)).into_response(), |
| 67 | + Ok(None) => StatusCode::NOT_FOUND.into_response(), |
| 68 | + _ => StatusCode::INTERNAL_SERVER_ERROR.into_response(), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +pub mod tests { |
| 74 | + use std::collections::HashMap; |
| 75 | + |
| 76 | + use crate::{app, tests::BASE_URL}; |
| 77 | + |
| 78 | + use super::*; |
| 79 | + use agent_issuance::{startup_commands::startup_commands, state::initialize}; |
| 80 | + use agent_shared::metadata::{load_metadata, set_metadata_configuration}; |
| 81 | + use agent_store::in_memory; |
| 82 | + use agent_verification::services::test_utils::test_verification_services; |
| 83 | + use axum::{ |
| 84 | + body::Body, |
| 85 | + http::{self, Request}, |
| 86 | + Router, |
| 87 | + }; |
| 88 | + use jsonwebtoken::Algorithm; |
| 89 | + use oid4vci::{ |
| 90 | + credential_format_profiles::{w3c_verifiable_credentials::jwt_vc_json::CredentialDefinition, Parameters}, |
| 91 | + credential_issuer::credential_configurations_supported::CredentialConfigurationsSupportedObject, |
| 92 | + proof::KeyProofMetadata, |
| 93 | + ProofType, |
| 94 | + }; |
| 95 | + use serde_json::json; |
| 96 | + use tower::Service; |
| 97 | + |
| 98 | + pub async fn credential_configurations(app: &mut Router) { |
| 99 | + let response = app |
| 100 | + .call( |
| 101 | + Request::builder() |
| 102 | + .method(http::Method::POST) |
| 103 | + .uri("/v1/configurations/credential_configurations") |
| 104 | + .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) |
| 105 | + .body(Body::from( |
| 106 | + serde_json::to_vec(&json!({ |
| 107 | + "credentialConfigurationId": "badge", |
| 108 | + "format": "jwt_vc_json", |
| 109 | + "credential_definition": { |
| 110 | + "type": [ |
| 111 | + "VerifiableCredential", |
| 112 | + "OpenBadgeCredential" |
| 113 | + ] |
| 114 | + }, |
| 115 | + "display": [{ |
| 116 | + "name": "Badge", |
| 117 | + "logo": { |
| 118 | + "url": "https://example.com/logo.png", |
| 119 | + } |
| 120 | + }] |
| 121 | + })) |
| 122 | + .unwrap(), |
| 123 | + )) |
| 124 | + .unwrap(), |
| 125 | + ) |
| 126 | + .await |
| 127 | + .unwrap(); |
| 128 | + |
| 129 | + assert_eq!(response.status(), StatusCode::OK); |
| 130 | + assert_eq!(response.headers().get("Content-Type").unwrap(), "application/json"); |
| 131 | + |
| 132 | + let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap(); |
| 133 | + let credential_configurations_supported: HashMap<String, CredentialConfigurationsSupportedObject> = |
| 134 | + serde_json::from_slice(&body).unwrap(); |
| 135 | + |
| 136 | + assert_eq!( |
| 137 | + credential_configurations_supported, |
| 138 | + HashMap::from_iter([( |
| 139 | + "badge".to_string(), |
| 140 | + CredentialConfigurationsSupportedObject { |
| 141 | + credential_format: CredentialFormats::JwtVcJson(Parameters { |
| 142 | + parameters: ( |
| 143 | + CredentialDefinition { |
| 144 | + type_: vec!["VerifiableCredential".to_string(), "OpenBadgeCredential".to_string()], |
| 145 | + credential_subject: Default::default(), |
| 146 | + }, |
| 147 | + None, |
| 148 | + ) |
| 149 | + .into(), |
| 150 | + }), |
| 151 | + cryptographic_binding_methods_supported: vec![ |
| 152 | + "did:key".to_string(), |
| 153 | + "did:key".to_string(), |
| 154 | + "did:iota:rms".to_string(), |
| 155 | + "did:jwk".to_string(), |
| 156 | + ], |
| 157 | + credential_signing_alg_values_supported: vec!["EdDSA".to_string()], |
| 158 | + proof_types_supported: HashMap::from_iter([( |
| 159 | + ProofType::Jwt, |
| 160 | + KeyProofMetadata { |
| 161 | + proof_signing_alg_values_supported: vec![Algorithm::EdDSA], |
| 162 | + }, |
| 163 | + )]), |
| 164 | + display: vec![json!({ |
| 165 | + "name": "Badge", |
| 166 | + "logo": { |
| 167 | + "url": "https://example.com/logo.png" |
| 168 | + } |
| 169 | + })], |
| 170 | + ..Default::default() |
| 171 | + } |
| 172 | + )]) |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + #[tokio::test] |
| 177 | + #[tracing_test::traced_test] |
| 178 | + async fn test_credential_configurations() { |
| 179 | + set_metadata_configuration("did:key"); |
| 180 | + |
| 181 | + let issuance_state = in_memory::issuance_state(Default::default()).await; |
| 182 | + |
| 183 | + let verification_state = in_memory::verification_state(test_verification_services(), Default::default()).await; |
| 184 | + |
| 185 | + initialize(&issuance_state, startup_commands(BASE_URL.clone(), &load_metadata())).await; |
| 186 | + |
| 187 | + let mut app = app((issuance_state, verification_state)); |
| 188 | + |
| 189 | + credential_configurations(&mut app).await; |
| 190 | + } |
| 191 | +} |
0 commit comments