Skip to content

Commit

Permalink
Merge branch 'pvw-3097-caching-demo-rp' into 'main'
Browse files Browse the repository at this point in the history
PVW-3097: Use memory-serve in demo RP to prevent undesired caching of assets

See merge request wallet/nl-wallet!1081
  • Loading branch information
Arjen committed Jul 22, 2024
2 parents e9e2b19 + 7646d1a commit 42dd024
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 23 deletions.
File renamed without changes.
81 changes: 81 additions & 0 deletions wallet_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wallet_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ josekit = "0.8.3"
jsonwebtoken = { version = "9.3.0", default-features = false }
lazy_static = "1.4.0"
libsqlite3-sys = { version = "0.27.0", default-features = false }
memory-serve = "0.6.0"
mime = "0.3.17"
mockall = "0.12.1"
nutype = "0.4.0"
Expand Down
1 change: 1 addition & 0 deletions wallet_core/mock_relying_party/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ base64.workspace = true
config = { workspace = true, features = ["toml"] }
futures = { workspace = true, features = ["std"] }
http.workspace = true
memory-serve.workspace = true
nutype = { workspace = true, features = ["serde"] }
once_cell.workspace = true
reqwest = { workspace = true, features = ["rustls-tls-webpki-roots"] }
Expand Down
30 changes: 15 additions & 15 deletions wallet_core/mock_relying_party/assets/usecase.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const wallet_buttons = document.getElementsByTagName("nl-wallet-button");
const wallet_buttons = document.getElementsByTagName("nl-wallet-button")
for (let button of wallet_buttons) {
button.addEventListener(
"success",
(e) => {
if (e.detail && e.detail.length > 1) {
const session_token = e.detail[0];
const session_type = e.detail[1];
const usecase = button.attributes.getNamedItem("usecase").value;
button.addEventListener(
"success",
(e) => {
if (e.detail && e.detail.length > 1) {
const session_token = e.detail[0]
const session_type = e.detail[1]
const usecase = button.attributes.getNamedItem("usecase").value

if (session_type === "cross_device") {
window.location.assign("../" + usecase + "/return?session_token=" + session_token);
}
}
},
false,
);
if (session_type === "cross_device") {
window.location.assign("../" + usecase + "/return?session_token=" + session_token)
}
}
},
false,
)
}
20 changes: 14 additions & 6 deletions wallet_core/mock_relying_party/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use std::{collections::HashMap, env, path::PathBuf, result::Result as StdResult, sync::Arc};
use std::{collections::HashMap, result::Result as StdResult, sync::Arc};

use askama::Template;
use axum::{
extract::{Path, Query, State},
handler::HandlerWithoutStateExt,
http::{Method, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
use base64::prelude::*;
use memory_serve::{load_assets, CacheControl, MemoryServe};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use tower_http::{
cors::{Any, CorsLayer},
services::ServeDir,
trace::TraceLayer,
};
use tracing::warn;
Expand Down Expand Up @@ -87,14 +86,15 @@ pub fn create_router(settings: Settings) -> Router {
wallet_web: settings.wallet_web,
});

let root_dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap_or_default();

let mut app = Router::new()
.route("/sessions", post(create_session))
.route("/:usecase/", get(usecase))
.route(&format!("/:usecase/{}", RETURN_URL_SEGMENT), get(disclosed_attributes))
.fallback_service(
ServeDir::new(root_dir.join("assets")).not_found_service({ StatusCode::NOT_FOUND }.into_service()),
MemoryServe::new(load_assets!("assets"))
.cache_control(CacheControl::NoCache)
.into_router()
.into_service(),
)
.with_state(application_state)
.layer(TraceLayer::new_for_http());
Expand Down Expand Up @@ -182,6 +182,10 @@ static USECASE_JS_SHA256: Lazy<String> =
Lazy::new(|| BASE64_STANDARD.encode(sha256(include_bytes!("../assets/usecase.js"))));

async fn usecase(State(state): State<Arc<ApplicationState>>, Path(usecase): Path<String>) -> Result<Response> {
if !state.usecases.contains_key(&usecase) {
return Ok(StatusCode::NOT_FOUND.into_response());
}

let result = UsecaseTemplate {
usecase: &usecase,
usecase_js_sha256: &USECASE_JS_SHA256,
Expand All @@ -197,6 +201,10 @@ async fn disclosed_attributes(
Path(usecase): Path<String>,
Query(params): Query<DisclosedAttributesParams>,
) -> Result<Response> {
if !state.usecases.contains_key(&usecase) {
return Ok(StatusCode::NOT_FOUND.into_response());
}

let attributes = state
.client
.disclosed_attributes(params.session_token, params.nonce)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{% when "xyz_bank" %}
{% include "xyz_bank.askama" %}
{% else %}
Usecase not found
{% call attributes::attributes(attributes) %}
{% endmatch %}

{# should be last for accessibility purposes #}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{% when "xyz_bank" %}
{% include "xyz_bank.askama" %}
{% else %}
Usecase not found
<nl-wallet-button text="Verder met NL Wallet" usecase="{{ usecase }}" base-url="../"></nl-wallet-button>
{% endmatch %}

{# should be last for accessibility purposes #}
Expand Down

0 comments on commit 42dd024

Please sign in to comment.