xnode_manager_sdk/auth/
handlers.rs

1use reqwest::Client;
2use serde::{Deserialize, Serialize};
3
4use crate::utils::{Empty, Session, SessionPostInput, SessionPostOutput, session_post};
5
6pub fn scope() -> String {
7    "/xnode-auth".to_string()
8}
9
10#[derive(Serialize, Deserialize, Debug)]
11pub struct User {
12    user: String,
13    signature: Option<String>,
14    timestamp: Option<String>,
15}
16impl User {
17    pub fn new(user: String) -> Self {
18        Self {
19            user,
20            signature: None,
21            timestamp: None,
22        }
23    }
24
25    pub fn with_signature(user: String, signature: String, timestamp: String) -> Self {
26        Self {
27            user,
28            signature: Some(signature),
29            timestamp: Some(timestamp),
30        }
31    }
32}
33#[derive(Debug)]
34pub struct LoginInput {
35    pub base_url: String,
36    pub user: User,
37}
38pub type LoginOutput = Result<Session, reqwest::Error>;
39pub async fn login(input: LoginInput) -> LoginOutput {
40    let client = Client::builder().cookie_store(true).build()?;
41
42    client
43        .post(format!("{}{}/api/login", input.base_url, scope()))
44        .json(&input.user)
45        .send()
46        .await?;
47
48    Ok(Session {
49        reqwest_client: client,
50        base_url: input.base_url,
51    })
52}
53
54pub type LogoutInput<'a> = SessionPostInput<'a, Empty, Empty>;
55pub type LogoutOutput = Empty;
56pub async fn logout(input: LogoutInput<'_>) -> SessionPostOutput<LogoutOutput> {
57    session_post(input, scope(), |_path| "/api/logout").await
58}