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

Commit 8e5ebcd

Browse files
committed
Simplify the URL displayed on compatibility SSO logins
See #1638
1 parent fc1e5b9 commit 8e5ebcd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/templates/src/functions.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub fn register(tera: &mut Tera, url_builder: UrlBuilder, vite_manifest: ViteMan
2626
tera.register_tester("empty", self::tester_empty);
2727
tera.register_filter("to_params", filter_to_params);
2828
tera.register_filter("safe_get", filter_safe_get);
29+
tera.register_filter("simplify_url", filter_simplify_url);
2930
tera.register_function("add_params_to_url", function_add_params_to_url);
3031
tera.register_function("merge", function_merge);
3132
tera.register_function("dict", function_dict);
@@ -77,6 +78,38 @@ pub fn filter_safe_get(value: &Value, args: &HashMap<String, Value>) -> Result<V
7778
}
7879
}
7980

81+
/// Filter which simplifies a URL to its domain name for HTTP(S) URLs
82+
fn filter_simplify_url(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
83+
let url = value
84+
.as_str()
85+
.ok_or_else(|| tera::Error::msg("Invalid input for `simplify_url` filter"))?;
86+
87+
if !args.is_empty() {
88+
return Err(tera::Error::msg("`simplify_url` filter takes no arguments"));
89+
}
90+
91+
// Do nothing if the URL is not valid
92+
let Ok(mut url) = Url::from_str(url) else {
93+
return Ok(Value::String(url.to_owned()));
94+
};
95+
96+
// Always at least remove the query parameters and fragment
97+
url.set_query(None);
98+
url.set_fragment(None);
99+
100+
// Do nothing else for non-HTTPS URLs
101+
if url.scheme() != "https" {
102+
return Ok(Value::String(url.to_string()));
103+
}
104+
105+
// Only return the domain name
106+
let Some(domain) = url.domain() else {
107+
return Ok(Value::String(url.to_string()));
108+
};
109+
110+
Ok(Value::String(domain.to_owned()))
111+
}
112+
80113
enum ParamsWhere {
81114
Fragment,
82115
Query,

templates/pages/sso.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<form method="POST" class="grid grid-cols-1 gap-6">
2323
<div class="rounded-lg bg-grey-25 dark:bg-grey-450 p-2 flex flex-col">
2424
<div class="text-center">
25-
<h1 class="text-lg text-center font-medium">{{ login.redirect_uri }}</h1>
25+
<h1 class="text-lg text-center font-medium">{{ login.redirect_uri | simplify_url }}</h1>
2626
<h1>wants to access your Matrix account</h1>
2727
</div>
2828
</div>

0 commit comments

Comments
 (0)