Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Make the upstream provider URL better display & fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Oct 30, 2023
1 parent 5c7ab48 commit 3d1109b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 2 additions & 6 deletions crates/handlers/src/views/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,11 @@ mod test {
let response = state.request(Request::get("/login").empty()).await;
response.assert_status(StatusCode::OK);
response.assert_header_value(CONTENT_TYPE, "text/html; charset=utf-8");
assert!(response
.body()
.contains(&escape_html(&first_provider.issuer)));
assert!(response.body().contains(&escape_html("first.com/")));
assert!(response
.body()
.contains(&escape_html(&first_provider_login.path_and_query())));
assert!(response
.body()
.contains(&escape_html(&second_provider.issuer)));
assert!(response.body().contains(&escape_html("second.com/")));
assert!(response
.body()
.contains(&escape_html(&second_provider_login.path_and_query())));
Expand Down
21 changes: 16 additions & 5 deletions crates/templates/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ fn filter_to_params(params: &Value, kwargs: Kwargs) -> Result<String, Error> {
}

/// Filter which simplifies a URL to its domain name for HTTP(S) URLs
fn filter_simplify_url(url: &str) -> String {
fn filter_simplify_url(url: &str, kwargs: Kwargs) -> Result<String, minijinja::Error> {
// Do nothing if the URL is not valid
let Ok(mut url) = Url::from_str(url) else {
return url.to_owned();
return Ok(url.to_owned());
};

// Always at least remove the query parameters and fragment
Expand All @@ -130,15 +130,26 @@ fn filter_simplify_url(url: &str) -> String {

// Do nothing else for non-HTTPS URLs
if url.scheme() != "https" {
return url.to_string();
return Ok(url.to_string());
}

let keep_path = kwargs.get::<Option<bool>>("keep_path")?.unwrap_or_default();
kwargs.assert_all_used()?;

// Only return the domain name
let Some(domain) = url.domain() else {
return url.to_string();
return Ok(url.to_string());
};

domain.to_owned()
if keep_path {
Ok(format!(
"{domain}{path}",
domain = domain,
path = url.path(),
))
} else {
Ok(domain.to_owned())
}
}

enum ParamsWhere {
Expand Down
2 changes: 1 addition & 1 deletion templates/pages/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h1 class="title">{{ _("mas.login.headline") }}</h1>
{% set params = next["params"] | default({}) | to_params(prefix="?") %}
{% for provider in providers %}
<a class="cpd-button" data-kind="secondary" data-size="lg" href="{{ ('/upstream/authorize/' ~ provider.id ~ params) | prefix_url }}">
{{ _("mas.login.continue_with_provider", provider=provider.issuer | simplify_url) }}
{{ _("mas.login.continue_with_provider", provider=provider.issuer | simplify_url(keep_path=True)) }}
</a>
{% endfor %}
{% endif %}
Expand Down

0 comments on commit 3d1109b

Please sign in to comment.