summaryrefslogtreecommitdiffstats
path: root/src/auth.rs
blob: e7d8061af85629306d28735347cfa1eab3bcb0ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mod types;
mod oauth;

use std::error::Error;
use std::fmt::{Display, Formatter};
use chrono::{DateTime, Utc};
pub use types::*;

#[derive(Debug)]
pub enum AuthError {
    Request { what: &'static str, error: reqwest::Error },
    Internal(String),
    Timeout
}

impl Display for AuthError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            AuthError::Request { what, error } => write!(f, "auth request error ({}): {}", what, error),
            AuthError::Internal(msg) => write!(f, "internal auth error: {}", msg),
            AuthError::Timeout => f.write_str("interactive authentication timed out")
        }
    }
}

impl Error for AuthError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            AuthError::Request { error, .. } => Some(error),
            _ => None
        }
    }
}

impl Token {
    fn is_expired(&self, now: DateTime<Utc>) -> bool {
        self.expire.is_some_and(|exp| now < exp)
    }
}

impl MsaUser {
    async fn log_in(&mut self) -> Result<(), AuthError> {
        todo!()
    }
}

#[cfg(test)]
mod test {
    use reqwest::Client;
    use crate::auth::oauth::device_code;
    use super::*;

    #[tokio::test]
    async fn abc() {
        simple_logger::SimpleLogger::new().with_colors(true).with_level(log::LevelFilter::Trace).init().unwrap();
        
        _=device_code::DeviceCodeAuthBuilder::new()
            .client_id("00000000402b5328")
            .add_scope("service::user.auth.xboxlive.com::MBI_SSL", true)
            .code_request_url("https://login.live.com/oauth20_connect.srf")
            .check_url("https://login.live.com/oauth20_token.srf")
            .begin(Client::new()).await.unwrap().drive().await;
    }
}