xnode_manager_sdk/auth/
handlers.rs

1use reqwest::Client;
2use serde::{Deserialize, Serialize};
3
4use crate::utils::{Empty, Error, Session, SessionPostInput, SessionPostOutput, session_post};
5
6pub fn scope() -> String {
7    "/xnode-auth".to_string()
8}
9
10#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
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, Clone, PartialEq)]
34pub struct LoginInput {
35    pub base_url: String,
36    pub user: User,
37}
38pub type LoginOutput = Result<Session, Error>;
39pub async fn login(input: LoginInput) -> LoginOutput {
40    let client = Client::builder()
41        .cookie_store(true)
42        .build()
43        .map_err(Error::ReqwestError)?;
44
45    client
46        .post(format!("{}{}/api/login", input.base_url, scope()))
47        .json(&input.user)
48        .send()
49        .await
50        .map_err(Error::ReqwestError)?;
51
52    Ok(Session {
53        reqwest_client: client,
54        base_url: input.base_url,
55    })
56}
57
58pub type LogoutInput<'a> = SessionPostInput<'a, Empty, Empty>;
59pub type LogoutOutput = Empty;
60pub async fn logout(input: LogoutInput<'_>) -> SessionPostOutput<LogoutOutput> {
61    session_post(input, scope(), |_path| "/api/logout").await
62}