-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from MiSArch/authentication
Initial authentication setup
- Loading branch information
Showing
7 changed files
with
195 additions
and
74 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,79 @@ | ||
use async_graphql::{Context, Error, Result}; | ||
use axum::http::HeaderMap; | ||
use bson::Uuid; | ||
use serde::Deserialize; | ||
|
||
/// Authorized-User HTTP header. | ||
#[derive(Deserialize, Debug)] | ||
pub struct AuthorizedUserHeader { | ||
id: Uuid, | ||
roles: Vec<Role>, | ||
} | ||
|
||
/// Extraction of AuthorizedUserHeader from HeaderMap. | ||
impl TryFrom<&HeaderMap> for AuthorizedUserHeader { | ||
type Error = Error; | ||
|
||
/// Tries to extract the AuthorizedUserHeader from a HeaderMap. | ||
/// | ||
/// Returns a GraphQL Error if the extraction fails. | ||
fn try_from(header_map: &HeaderMap) -> Result<Self, Self::Error> { | ||
if let Some(authenticate_user_header_value) = header_map.get("Authorized-User") { | ||
if let Ok(authenticate_user_header_str) = authenticate_user_header_value.to_str() { | ||
let authenticate_user_header: AuthorizedUserHeader = | ||
serde_json::from_str(authenticate_user_header_str)?; | ||
return Ok(authenticate_user_header); | ||
} | ||
} | ||
Err(Error::new("Authorized-User header could not be parsed.")) | ||
} | ||
} | ||
|
||
/// Role of user. | ||
#[derive(Deserialize, Debug, PartialEq, Clone, Copy)] | ||
#[serde(rename_all = "snake_case")] | ||
enum Role { | ||
Buyer, | ||
Admin, | ||
Employee, | ||
} | ||
|
||
impl Role { | ||
/// Defines if user has a permissive role. | ||
fn is_permissive(self) -> bool { | ||
match self { | ||
Self::Buyer => false, | ||
Self::Admin => true, | ||
Self::Employee => true, | ||
} | ||
} | ||
} | ||
|
||
/// Authenticate user of UUID for a Context. | ||
pub fn authenticate_user(ctx: &Context, id: Uuid) -> Result<()> { | ||
match ctx.data::<AuthorizedUserHeader>() { | ||
Ok(authenticate_user_header) => check_permissions(&authenticate_user_header, id), | ||
Err(_) => Err(Error::new("Authorized-User header could not be parsed.")), | ||
} | ||
} | ||
|
||
/// Check if user of UUID has a valid permission according to the AuthorizedUserHeader. | ||
/// | ||
/// Permission is valid if the user has `Role::Buyer` and the same UUID as provided in the function parameter. | ||
/// Permission is valid if the user has a permissive role: `user.is_permissive() == true`, regardless of the users UUID. | ||
pub fn check_permissions(authenticate_user_header: &AuthorizedUserHeader, id: Uuid) -> Result<()> { | ||
if authenticate_user_header | ||
.roles | ||
.iter() | ||
.any(|r| r.is_permissive()) | ||
|| authenticate_user_header.id == id | ||
{ | ||
return Ok(()); | ||
} else { | ||
let message = format!( | ||
"Authentication failed for user of UUID: `{}`. Operation not permitted.", | ||
authenticate_user_header.id | ||
); | ||
return Err(Error::new(message)); | ||
} | ||
} |
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.