Skip to main content

rustauth_oidc/
flow.rs

1/// Options needed to compute an OIDC redirect URI.
2pub trait OidcFlowOptions {
3    /// Optional shared redirect URI override.
4    fn redirect_uri(&self) -> Option<&str>;
5}
6
7pub fn oidc_redirect_uri(
8    base_url: &str,
9    provider_id: &str,
10    options: &impl OidcFlowOptions,
11) -> String {
12    if let Some(redirect_uri) = options
13        .redirect_uri()
14        .filter(|value| !value.trim().is_empty())
15    {
16        if url::Url::parse(redirect_uri).is_ok() {
17            return redirect_uri.to_owned();
18        }
19        let path = if redirect_uri.starts_with('/') {
20            redirect_uri.to_owned()
21        } else {
22            format!("/{redirect_uri}")
23        };
24        return format!("{}{}", base_url.trim_end_matches('/'), path);
25    }
26    format!(
27        "{}/sso/callback/{}",
28        base_url.trim_end_matches('/'),
29        provider_id
30    )
31}