#[derive(Debug)]
pub enum LoginError {
RequestFailure(reqwest::Error),
InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
UnexpectedStatus(reqwest::StatusCode),
Status400 {
errors: Vec<String>,
},
Status403 {
error: String,
},
Status500 {
error: String,
},
Status401 {
error: String,
},
}
impl From<reqwest::Error> for LoginError {
fn from(error: reqwest::Error) -> LoginError {
LoginError::RequestFailure(error)
}
}
impl From<reqwest::header::InvalidHeaderValue> for LoginError {
fn from(error: reqwest::header::InvalidHeaderValue) -> LoginError {
LoginError::InvalidHeaderValue(error)
}
}
impl LoginError {
pub fn to_login_endpoint_error(&self) -> Option<crate::model::LoginEndpointError> {
match self {
LoginError::Status400 { errors } => Some(crate::model::LoginEndpointError::ArgValidation { errors: errors.clone() }),
LoginError::Status403 { error } => Some(crate::model::LoginEndpointError::NotWhitelisted { error: error.clone() }),
LoginError::Status500 { error } => Some(crate::model::LoginEndpointError::Internal { error: error.clone() }),
LoginError::Status401 { error } => Some(crate::model::LoginEndpointError::External { error: error.clone() }),
_ => None
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LoginEndpointErrorArgValidationPayload {
pub errors: Vec<String>,
}
#[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,
}
#[async_trait::async_trait]
pub trait Login {
async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, LoginError>;
async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, LoginError>;
async fn start_o_auth2(&self, ) -> Result<crate::model::OAuth2Data, LoginError>;
async fn complete_o_auth2(&self, field0: String) -> Result<crate::model::UnsafeToken, LoginError>;
}
#[derive(Clone, Debug)]
pub struct LoginLive {
pub base_url: reqwest::Url,
pub allow_insecure: bool,
}
#[async_trait::async_trait]
impl Login for LoginLive {
async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, LoginError> {
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap()
.push("v1")
.push("oauth2");
url.query_pairs_mut().append_pair("provider", &format!("{provider}"));
url.query_pairs_mut().append_pair("access_token", &format!("{access_token}"));
{
let headers_vec: Vec<(&str, String)> = vec![]; tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body="<no_body>", "oauth2");
}
let mut builder = reqwest::Client::builder();
if self.allow_insecure {
builder = builder.danger_accept_invalid_certs(true);
}
let client = builder.build()?;
let result = client
.post(url)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::UnsafeToken>().await?;
Ok(body)
}
400 => {
let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
Err(LoginError::Status400 { errors: body.errors })
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(LoginError::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(LoginError::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(LoginError::Status401 { error: body.error })
}
_ => Err(LoginError::UnexpectedStatus(result.status()))
}
}
async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, LoginError> {
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap()
.push("v1")
.push("login")
.push("token");
let mut headers = reqwest::header::HeaderMap::new();
headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
{
let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "current_token");
}
let mut builder = reqwest::Client::builder();
if self.allow_insecure {
builder = builder.danger_accept_invalid_certs(true);
}
let client = builder.build()?;
let result = client
.get(url)
.headers(headers)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::Token>().await?;
Ok(body)
}
400 => {
let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
Err(LoginError::Status400 { errors: body.errors })
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(LoginError::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(LoginError::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(LoginError::Status401 { error: body.error })
}
_ => Err(LoginError::UnexpectedStatus(result.status()))
}
}
async fn start_o_auth2(&self, ) -> Result<crate::model::OAuth2Data, LoginError> {
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap()
.push("login")
.push("oauth2")
.push("device")
.push("start");
{
let headers_vec: Vec<(&str, String)> = vec![]; tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body="<no_body>", "start_o_auth2");
}
let mut builder = reqwest::Client::builder();
if self.allow_insecure {
builder = builder.danger_accept_invalid_certs(true);
}
let client = builder.build()?;
let result = client
.post(url)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::OAuth2Data>().await?;
Ok(body)
}
400 => {
let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
Err(LoginError::Status400 { errors: body.errors })
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(LoginError::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(LoginError::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(LoginError::Status401 { error: body.error })
}
_ => Err(LoginError::UnexpectedStatus(result.status()))
}
}
async fn complete_o_auth2(&self, field0: String) -> Result<crate::model::UnsafeToken, LoginError> {
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap()
.push("login")
.push("oauth2")
.push("device")
.push("complete");
{
let headers_vec: Vec<(&str, String)> = vec![]; tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body=serde_json::to_string(&field0).unwrap(), "complete_o_auth2");
}
let mut builder = reqwest::Client::builder();
if self.allow_insecure {
builder = builder.danger_accept_invalid_certs(true);
}
let client = builder.build()?;
let result = client
.post(url)
.json(&field0)
.send()
.await?;
match result.status().as_u16() {
200 => {
let body = result.json::<crate::model::UnsafeToken>().await?;
Ok(body)
}
400 => {
let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
Err(LoginError::Status400 { errors: body.errors })
}
403 => {
let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
Err(LoginError::Status403 { error: body.error })
}
500 => {
let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
Err(LoginError::Status500 { error: body.error })
}
401 => {
let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
Err(LoginError::Status401 { error: body.error })
}
_ => Err(LoginError::UnexpectedStatus(result.status()))
}
}
}