Skip to content

Commit

Permalink
add rss routes to http server
Browse files Browse the repository at this point in the history
  • Loading branch information
edchapman88 committed Nov 22, 2023
1 parent 47bc865 commit d4c3da8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
34 changes: 33 additions & 1 deletion trustchain-http/src/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub trait TrustchainIssuerHTTP {
subject_id: Option<&str>,
issuer_did: &str,
resolver: &dyn TrustchainResolver,
rss: bool,
) -> Result<Credential, TrustchainHTTPError>;
}

Expand All @@ -95,6 +96,7 @@ impl TrustchainIssuerHTTP for TrustchainIssuerHTTPHandler {
subject_id: Option<&str>,
issuer_did: &str,
resolver: &dyn TrustchainResolver,
rss: bool,
) -> Result<Credential, TrustchainHTTPError> {
let mut credential = credential.to_owned();
credential.issuer = Some(ssi::vc::Issuer::URI(ssi::vc::URI::String(
Expand All @@ -108,11 +110,16 @@ impl TrustchainIssuerHTTP for TrustchainIssuerHTTPHandler {
}
}
let issuer = IONAttestor::new(issuer_did);
let key_id = if rss {
Some("Un2E28ffH75_lvA59p7R0wUaGaACzbg8i2H9ksviS34")
} else {
None
};
Ok(issuer
.sign(
&credential,
None,
None,
key_id,
resolver,
// TODO: add context loader to app_state
&mut ContextLoader::default(),
Expand Down Expand Up @@ -146,6 +153,29 @@ impl TrustchainIssuerHTTPHandler {
Ok(Html(str_to_qr_code_html(&qr_code_str, "Issuer")))
}

pub async fn get_issuer_qrcode_rss(
State(app_state): State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Html<String>, TrustchainHTTPError> {
let qr_code_str = if http_config().verifiable_endpoints.unwrap_or(true) {
serde_json::to_string(&DIDQRCode {
did: app_state.config.server_did.as_ref().unwrap().to_owned(),
route: "/vc_rss/issuer/".to_string(),
id,
})
.unwrap()
} else {
format!(
"{}://{}:{}/vc_rss/issuer/{id}",
http_config().http_scheme(),
app_state.config.host_display,
app_state.config.port
)
};
// Respond with the QR code as a png embedded in html
Ok(Html(str_to_qr_code_html(&qr_code_str, "Issuer")))
}

/// API endpoint taking the UUID of a VC. Response is the VC JSON.
pub async fn get_issuer(
Path(credential_id): Path<String>,
Expand Down Expand Up @@ -176,6 +206,7 @@ impl TrustchainIssuerHTTPHandler {
pub async fn post_issuer(
(Path(credential_id), Json(vc_info)): (Path<String>, Json<VcInfo>),
app_state: Arc<AppState>,
rss: bool,
) -> impl IntoResponse {
info!("Received VC info: {:?}", vc_info);
let issuer_did = app_state
Expand All @@ -190,6 +221,7 @@ impl TrustchainIssuerHTTPHandler {
Some(&vc_info.subject_id),
issuer_did,
app_state.verifier.resolver(),
rss,
)
.await?;
Ok((StatusCode::OK, Json(credential_signed)))
Expand Down
23 changes: 22 additions & 1 deletion trustchain-http/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl TrustchainRouter {
"/issuer/:id",
get(issuer::TrustchainIssuerHTTPHandler::get_issuer_qrcode),
)
.route(
"/issuer_rss/:id",
get(issuer::TrustchainIssuerHTTPHandler::get_issuer_qrcode_rss),
)
.route(
"/verifier",
get(verifier::TrustchainVerifierHTTPHandler::get_verifier_qrcode),
Expand All @@ -48,7 +52,24 @@ impl TrustchainRouter {
get(issuer::TrustchainIssuerHTTPHandler::get_issuer).post({
let state = shared_state.clone();
move |(id, vc_info)| {
issuer::TrustchainIssuerHTTPHandler::post_issuer((id, vc_info), state)
issuer::TrustchainIssuerHTTPHandler::post_issuer(
(id, vc_info),
state,
false,
)
}
}),
)
.route(
"/vc_rss/issuer/:id",
get(issuer::TrustchainIssuerHTTPHandler::get_issuer).post({
let state = shared_state.clone();
move |(id, vc_info)| {
issuer::TrustchainIssuerHTTPHandler::post_issuer(
(id, vc_info),
state,
true,
)
}
}),
)
Expand Down
4 changes: 4 additions & 0 deletions trustchain-http/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ <h2>Receive a credential</h2>
<button type=submit>Driving License</button>
</form>
<br>
<form action=/issuer_rss/c3e199ae-594b-11ee-b375-6af6d5dd6607 method=GET>
<button type=submit>Driving License (RSS)</button>
</form>
<br>
<br>
<h2>Present a credential</h2>
<form action=/verifier method=GET>
Expand Down

0 comments on commit d4c3da8

Please sign in to comment.