Skip to main content

openauth_plugins/last_login_method/
resolve.rs

1use openauth_core::api::ApiRequest;
2use openauth_core::context::AuthContext;
3
4/// Request data used to resolve a login method.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct LoginMethodContext {
7    path: String,
8}
9
10impl LoginMethodContext {
11    pub fn new(path: impl Into<String>) -> Self {
12        Self { path: path.into() }
13    }
14
15    pub fn from_request(context: &AuthContext, request: &ApiRequest) -> Self {
16        Self::new(auth_path(context, request))
17    }
18
19    pub fn path(&self) -> &str {
20        &self.path
21    }
22}
23
24pub fn default_login_method(context: &LoginMethodContext) -> Option<String> {
25    let path = context.path();
26    if path.starts_with("/callback/") || path.starts_with("/oauth2/callback/") {
27        return path.rsplit('/').next().map(str::to_owned);
28    }
29    if path == "/sign-in/email" || path == "/sign-up/email" {
30        return Some("email".to_owned());
31    }
32    if path.contains("siwe") {
33        return Some("siwe".to_owned());
34    }
35    if path.contains("/passkey/verify-authentication") {
36        return Some("passkey".to_owned());
37    }
38    if path.starts_with("/magic-link/verify") {
39        return Some("magic-link".to_owned());
40    }
41    None
42}
43
44fn auth_path(context: &AuthContext, request: &ApiRequest) -> String {
45    let path = request.uri().path();
46    let base_path = context.base_path.trim_end_matches('/');
47    path.strip_prefix(base_path)
48        .filter(|value| !value.is_empty())
49        .unwrap_or(path)
50        .to_owned()
51}