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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::{ErrorMessage, IdentityResult, WebViewDeviceCodeError};
use tokio::sync::mpsc::error::SendTimeoutError;
use url::ParseError;

pub type AF = AuthorizationFailure;

/// Errors typically from missing or invalid configuration using one of the
/// identity platform clients such as AuthorizationCodeCredential.
#[derive(Debug, thiserror::Error)]
pub enum AuthorizationFailure {
    #[error("Required value missing:\n{0:#?}", name)]
    RequiredValue {
        name: String,
        message: Option<String>,
    },

    #[error("{0:#?}")]
    UrlParse(#[from] url::ParseError),

    #[error("{0:#?}")]
    Uuid(#[from] uuid::Error),

    #[error("{0:#?}")]
    Openssl(String),

    #[error("{0:#?}")]
    SerdeJson(#[from] serde_json::Error),
}

impl AuthorizationFailure {
    pub fn required<T: AsRef<str>>(name: T) -> AuthorizationFailure {
        AuthorizationFailure::RequiredValue {
            name: name.as_ref().to_owned(),
            message: None,
        }
    }

    pub fn result<U>(name: impl AsRef<str>) -> IdentityResult<U> {
        Err(AuthorizationFailure::RequiredValue {
            name: name.as_ref().to_owned(),
            message: None,
        })
    }

    pub fn msg_err<T: AsRef<str>>(name: T, message: T) -> AuthorizationFailure {
        AuthorizationFailure::RequiredValue {
            name: name.as_ref().to_owned(),
            message: Some(message.as_ref().to_owned()),
        }
    }

    pub fn msg_internal_err<T: AsRef<str>>(name: T) -> AuthorizationFailure {
        AuthorizationFailure::RequiredValue {
            name: name.as_ref().to_owned(),
            message: Some("Internal error please file an issue on GitHub https://github.com/sreeise/graph-rs-sdk/issues".to_owned()),
        }
    }

    pub fn msg_result<T>(
        name: impl AsRef<str>,
        message: impl ToString,
    ) -> Result<T, AuthorizationFailure> {
        Err(AuthorizationFailure::RequiredValue {
            name: name.as_ref().to_owned(),
            message: Some(message.to_string()),
        })
    }

    pub fn msg_internal_result<T>(name: impl AsRef<str>) -> Result<T, AuthorizationFailure> {
        Err(AF::msg_internal_err(name))
    }

    pub fn condition(cond: bool, name: &str, msg: &str) -> IdentityResult<()> {
        if cond {
            AF::msg_result(name, msg)
        } else {
            Ok(())
        }
    }

    pub fn x509(message: impl ToString) -> AuthorizationFailure {
        AuthorizationFailure::Openssl(message.to_string())
    }

    pub fn x509_result<T>(message: impl ToString) -> Result<T, AuthorizationFailure> {
        Err(AuthorizationFailure::Openssl(message.to_string()))
    }
}

/// Error either from missing or invalid configuration using one of the
/// identity platform clients or an error from the result of executing
/// an http request using the identity platform clients.
#[derive(Debug, thiserror::Error)]
pub enum AuthExecutionError {
    #[error("{0:#?}")]
    Authorization(#[from] AuthorizationFailure),

    #[error("{0:#?}")]
    Request(#[from] reqwest::Error),

    #[error("{0:#?}")]
    Http(#[from] http::Error),

    #[error("message: {0:#?}, response: {1:#?}", message, response)]
    SilentTokenAuth {
        message: String,
        response: http::Response<Result<serde_json::Value, ErrorMessage>>,
    },

    #[error("{0:#?}")]
    JsonWebToken(#[from] jsonwebtoken::errors::Error),
}

impl AuthExecutionError {
    pub fn silent_token_auth(
        response: http::Response<Result<serde_json::Value, ErrorMessage>>,
    ) -> AuthExecutionError {
        AuthExecutionError::SilentTokenAuth {
            message: "silent token auth failed".into(),
            response,
        }
    }
}

impl From<serde_json::error::Error> for AuthExecutionError {
    fn from(value: serde_json::error::Error) -> Self {
        AuthExecutionError::Authorization(AuthorizationFailure::from(value))
    }
}

impl From<WebViewDeviceCodeError> for AuthExecutionError {
    fn from(value: WebViewDeviceCodeError) -> Self {
        AuthExecutionError::Authorization(AuthorizationFailure::msg_err(
            "Unknown",
            &value.to_string(),
        ))
    }
}

impl From<url::ParseError> for AuthExecutionError {
    fn from(value: ParseError) -> Self {
        AuthExecutionError::Authorization(AuthorizationFailure::UrlParse(value))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum AuthTaskExecutionError<R> {
    #[error("{0:#?}")]
    AuthExecutionError(#[from] AuthExecutionError),
    #[error("Tokio SendTimeoutError - Reason: {0:#?}")]
    SendTimeoutErrorAsync(#[from] SendTimeoutError<R>),
    #[error("{0:#?}")]
    JoinError(#[from] tokio::task::JoinError),
}