-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic scaffolding for custom authorization functions
- Loading branch information
1 parent
313b8ca
commit a4837cc
Showing
5 changed files
with
49 additions
and
3 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
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,14 @@ | ||
use axum::extract::Request; | ||
use entity_api::user::AuthSession; | ||
|
||
struct AuthorizationError {} | ||
|
||
pub(crate) async fn protect( | ||
auth_session: AuthSession, | ||
request: Request, | ||
) -> Result<Request, AuthorizationError> { | ||
// here we have access to the current user (actor) making the request | ||
// as well as the request itself (from which we can determine which resource is being acted upon). | ||
// We can use both pieces of information to determine if the actor is allowed to operate on the resource. | ||
Ok(request) | ||
} |
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,22 @@ | ||
pub(crate) mod coaching_relationships; | ||
|
||
#[macro_export] | ||
macro_rules! protected_resource { | ||
($protect:expr, $alternative:expr) => {{ | ||
use axum::{ | ||
extract::Request, | ||
middleware::{from_fn, Next}, | ||
response::IntoResponse, | ||
}; | ||
use entity_api::user::AuthSession; | ||
|
||
from_fn( | ||
|auth_session: AuthSession, req: Request, next: Next| async move { | ||
match $protect(auth_session, req).await { | ||
Ok(req) => next.run(req).await, | ||
Err(_) => $alternative.into_response(), | ||
} | ||
}, | ||
) | ||
}}; | ||
} |
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