-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Groundwork for supporting multiple authorization providers
- Loading branch information
1 parent
782623f
commit 5642194
Showing
10 changed files
with
263 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::authorization_backends::espocrm::EspoAuthorizationClient; | ||
use thiserror::Error; | ||
|
||
pub mod espocrm; | ||
|
||
pub enum AuthorizationProvider { | ||
Espocrm(EspoAuthorizationClient), | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AuthorizationProviderError { | ||
#[error(transparent)] | ||
Espocrm(#[from] reqwest::Error), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct LoginUser { | ||
pub id: String, | ||
pub name: String, | ||
pub email: String, | ||
pub is_admin: bool, | ||
pub is_active: bool, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum CheckResult { | ||
Ok(LoginUser), | ||
TwoFactorRequired, | ||
Invalid, | ||
} | ||
|
||
pub trait AuthorizationBackend { | ||
type Error: std::error::Error; | ||
|
||
async fn check_credentials( | ||
&self, | ||
username: &str, | ||
password: &str, | ||
two_factor_code: Option<&str>, | ||
) -> Result<CheckResult, Self::Error>; | ||
async fn get_user(&self, user_id: &str) -> Result<LoginUser, Self::Error>; | ||
} | ||
|
||
impl AuthorizationBackend for AuthorizationProvider { | ||
type Error = AuthorizationProviderError; | ||
|
||
async fn check_credentials( | ||
&self, | ||
username: &str, | ||
password: &str, | ||
two_factor_code: Option<&str>, | ||
) -> Result<CheckResult, Self::Error> { | ||
match self { | ||
Self::Espocrm(client) => { | ||
client | ||
.check_credentials(username, password, two_factor_code) | ||
.await | ||
} | ||
} | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
async fn get_user(&self, user_id: &str) -> Result<LoginUser, Self::Error> { | ||
match self { | ||
Self::Espocrm(client) => client.get_user(user_id).await, | ||
} | ||
.map_err(|e| e.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.