This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Allow user with multiple roles, add UserManager role #86
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,20 +19,83 @@ pub struct JwtManager { | |
decoding_key: DecodingKey, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] | ||
#[allow(clippy::upper_case_acronyms)] | ||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] | ||
#[serde(tag = "role", content = "id")] | ||
pub enum AuthRole { | ||
BL, | ||
VEN, | ||
UserManager, | ||
Business(Option<String>), | ||
VEN(String), | ||
} | ||
|
||
impl AuthRole { | ||
pub fn is_business(&self) -> bool { | ||
matches!(self, AuthRole::Business(_)) | ||
} | ||
|
||
pub fn is_ven(&self) -> bool { | ||
matches!(self, AuthRole::VEN(_)) | ||
} | ||
|
||
pub fn is_user_manager(&self) -> bool { | ||
matches!(self, AuthRole::UserManager) | ||
} | ||
} | ||
|
||
#[derive(Debug, serde::Serialize, serde::Deserialize)] | ||
pub struct Claims { | ||
exp: usize, | ||
nbf: usize, | ||
pub sub: String, | ||
pub role: AuthRole, | ||
pub ven: Option<String>, | ||
pub roles: Vec<AuthRole>, | ||
} | ||
|
||
impl Claims { | ||
pub fn ven_ids(&self) -> Vec<String> { | ||
self.roles | ||
.iter() | ||
.filter_map(|role| { | ||
if let AuthRole::VEN(id) = role { | ||
Some(id.clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect() | ||
} | ||
|
||
pub fn business_ids(&self) -> (Vec<String>, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be better as an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! Will change! |
||
let mut allow_any = false; | ||
let ids = self | ||
.roles | ||
.iter() | ||
.filter_map(|role| { | ||
if let AuthRole::Business(id) = role { | ||
if let Some(id) = id { | ||
Some(id.clone()) | ||
} else { | ||
allow_any = true; | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
(ids, allow_any) | ||
} | ||
|
||
pub fn is_ven(&self) -> bool { | ||
self.roles.iter().any(AuthRole::is_ven) | ||
} | ||
|
||
pub fn is_business(&self) -> bool { | ||
self.roles.iter().any(AuthRole::is_business) | ||
} | ||
|
||
pub fn is_user_manager(&self) -> bool { | ||
self.roles.iter().any(AuthRole::is_user_manager) | ||
} | ||
} | ||
|
||
impl JwtManager { | ||
|
@@ -63,8 +126,7 @@ impl JwtManager { | |
&self, | ||
expires_in: std::time::Duration, | ||
client_id: String, | ||
role: AuthRole, | ||
ven: Option<String>, | ||
roles: Vec<AuthRole>, | ||
) -> Result<String, jsonwebtoken::errors::Error> { | ||
let now = chrono::Utc::now(); | ||
let exp = now + expires_in; | ||
|
@@ -73,8 +135,7 @@ impl JwtManager { | |
exp: exp.timestamp() as usize, | ||
nbf: now.timestamp() as usize, | ||
sub: client_id, | ||
role, | ||
ven, | ||
roles, | ||
}; | ||
|
||
let token = encode(&Header::default(), &claims, &self.encoding_key)?; | ||
|
@@ -90,9 +151,17 @@ impl JwtManager { | |
} | ||
} | ||
|
||
/// User claims extracted from the request | ||
pub struct User(pub Claims); | ||
pub struct BLUser(pub Claims); | ||
// pub struct VENUser(pub Claims); | ||
|
||
/// User claims extracted from the request, with the requirement that the user is a business user | ||
pub struct BusinessUser(pub Claims); | ||
|
||
/// User claims extracted from the request, with the requirement that the user is a VEN user | ||
pub struct VENUser(pub Claims); | ||
|
||
/// User claims extracted from the request, with the requirement that the user is a user manager | ||
pub struct UserManagerUser(pub Claims); | ||
|
||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for User | ||
|
@@ -125,36 +194,55 @@ where | |
} | ||
|
||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for BLUser | ||
impl<S: Send + Sync> FromRequestParts<S> for BusinessUser | ||
where | ||
Arc<JwtManager>: FromRef<S>, | ||
{ | ||
type Rejection = AppError; | ||
|
||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
let user = User::from_request_parts(parts, state).await?; | ||
|
||
if user.0.role != AuthRole::BL { | ||
let User(user) = User::from_request_parts(parts, state).await?; | ||
if !user.is_business() { | ||
return Err(AppError::Auth( | ||
"User does not have the required role".to_string(), | ||
)); | ||
} | ||
|
||
Ok(BLUser(user.0)) | ||
Ok(BusinessUser(user)) | ||
} | ||
} | ||
|
||
// #[async_trait] | ||
// impl<S: Send + Sync> FromRequestParts<S> for VENUser where Arc<JwtManager>: FromRef<S> { | ||
// type Rejection = AppError; | ||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for VENUser | ||
where | ||
Arc<JwtManager>: FromRef<S>, | ||
{ | ||
type Rejection = AppError; | ||
|
||
// async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
// let user = User::from_request_parts(parts, state).await?; | ||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
let User(user) = User::from_request_parts(parts, state).await?; | ||
if !user.is_ven() { | ||
return Err(AppError::Auth( | ||
"User does not have the required role".to_string(), | ||
)); | ||
} | ||
Ok(VENUser(user)) | ||
} | ||
} | ||
|
||
// if user.0.role != AuthRole::VEN { | ||
// return Err(AppError::Auth("User does not have the required role".to_string())); | ||
// } | ||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for UserManagerUser | ||
where | ||
Arc<JwtManager>: FromRef<S>, | ||
{ | ||
type Rejection = AppError; | ||
|
||
// Ok(VENUser(user.0)) | ||
// } | ||
// } | ||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
let User(user) = User::from_request_parts(parts, state).await?; | ||
if !user.is_user_manager() { | ||
return Err(AppError::Auth( | ||
"User does not have the required role".to_string(), | ||
)); | ||
} | ||
Ok(UserManagerUser(user)) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be a new-type instead of
String
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably will be a newtype eventually, given we already have several of those, but I wanted to keep this PR a little smaller.