pub enum Oauth2Error {
RequestFailure(reqwest::Error),
InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
UnexpectedStatus(reqwest::StatusCode),
Status403 {
error: String,
},
Status500 {
error: String,
},
Status401 {
error: String,
},
}
impl From<reqwest::Error> for Oauth2Error {
fn from(error: reqwest::Error) -> Oauth2Error {
Oauth2Error::RequestFailure(error)
}
}
impl From<reqwest::header::InvalidHeaderValue> for Oauth2Error {
fn from(error: reqwest::header::InvalidHeaderValue) -> Oauth2Error {
Oauth2Error::InvalidHeaderValue(error)
}
}
impl Oauth2Error {
pub fn to_login_endpoint_error(&self) -> Option<crate::model::LoginEndpointError> {
match self {
Oauth2Error::Status403 { error } => Some(crate::model::LoginEndpointError::NotWhitelisted { error: error.clone() }),
Oauth2Error::Status500 { error } => Some(crate::model::LoginEndpointError::Internal { error: error.clone() }),
Oauth2Error::Status401 { error } => Some(crate::model::LoginEndpointError::External { error: error.clone() }),
_ => None
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LoginEndpointErrorNotWhitelistedPayload {
pub error: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LoginEndpointErrorInternalPayload {
pub error: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LoginEndpointErrorExternalPayload {
pub error: String,
}
pub enum CurrentTokenError {
RequestFailure(reqwest::Error),
InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
UnexpectedStatus(reqwest::StatusCode),
Status403 {
error: String,
},
Status500 {
error: String,
},
Status401 {
error: String,
},
}
impl From<reqwest::Error> for CurrentTokenError {
fn from(error: reqwest::Error) -> CurrentTokenError {
CurrentTokenError::RequestFailure(error)
}
}
impl From<reqwest::header::InvalidHeaderValue> for CurrentTokenError {
fn from(error: reqwest::header::InvalidHeaderValue) -> CurrentTokenError {
CurrentTokenError::InvalidHeaderValue(error)
}
}
impl CurrentTokenError {
pub fn to_login_endpoint_error(&self) -> Option<crate::model::LoginEndpointError> {
match self {
CurrentTokenError::Status403 { error } => Some(crate::model::LoginEndpointError::NotWhitelisted { error: error.clone() }),
CurrentTokenError::Status500 { error } => Some(crate::model::LoginEndpointError::Internal { error: error.clone() }),
CurrentTokenError::Status401 { error } => Some(crate::model::LoginEndpointError::External { error: error.clone() }),
_ => None
}
}
}
#[async_trait::async_trait]
pub trait Login {
async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, Oauth2Error>;
async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, CurrentTokenError>;
}
#[derive(Clone, Debug)]
pub struct LoginLive {
pub base_url: reqwest::Url,
}
#[async_trait::async_trait]
impl Login for LoginLive {
async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, Oauth2Error> {
let mut url = self.base_url.clone();
url.set_path("oauth2");
url.query_pairs_mut().append_pair("provider", &format!("{provider}"));
url.query_pairs_mut().append_pair("access_token", &format!("{access_token}"));
let result = reqwest::Client::builder()
.build()?
.post(url)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::UnsafeToken>().await?;
Ok(body)
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(Oauth2Error::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(Oauth2Error::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(Oauth2Error::Status401 { error: body.error })
}
_ => Err(Oauth2Error::UnexpectedStatus(result.status()))
}
}
async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, CurrentTokenError> {
let mut url = self.base_url.clone();
url.set_path("login/token");
let mut headers = reqwest::header::HeaderMap::new();
headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
let result = reqwest::Client::builder()
.build()?
.get(url)
.headers(headers)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::Token>().await?;
Ok(body)
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(CurrentTokenError::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(CurrentTokenError::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(CurrentTokenError::Status401 { error: body.error })
}
_ => Err(CurrentTokenError::UnexpectedStatus(result.status()))
}
}
}