-
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.
heimdallr-v2: implement
Claim
extractor
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pub(crate) mod nft_marketplace; | ||
pub(crate) mod worldmobile; |
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,2 @@ | ||
//! Extractors module. | ||
mod claims; |
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,55 @@ | ||
//! Claim extractor | ||
//! | ||
//! This module implements the claim extractor from a bearer token. | ||
use axum::extract::{FromRef, FromRequestParts}; | ||
use axum::headers::authorization::{Authorization, Bearer}; | ||
use axum::http::request::Parts; | ||
use axum::{async_trait, RequestPartsExt, TypedHeader}; | ||
use jsonwebtoken::{decode, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::error::AuthError; | ||
use crate::state::AppState; | ||
|
||
/// Claims represents the claims data from JWT | ||
#[derive(Debug, Deserialize, Serialize)] | ||
#[allow(clippy::missing_docs_in_private_items)] | ||
pub struct Claims { | ||
sub: String, | ||
exp: usize, | ||
} | ||
|
||
impl Claims { | ||
/// Return the customer ID in this claim. | ||
pub fn get_customer_id(&self) -> Result<u64, AuthError> { | ||
self.sub.parse().map_err(|_| AuthError::WrongCredentials) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for Claims | ||
where | ||
AppState: FromRef<S>, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = AuthError; | ||
|
||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
// Extract the token from the authorization header | ||
let TypedHeader(Authorization(bearer)) = parts | ||
.extract::<TypedHeader<Authorization<Bearer>>>() | ||
.await | ||
.map_err(|_| AuthError::MissingCredentials)?; | ||
// Decode the user data | ||
|
||
let app_state = AppState::from_ref(state); | ||
let token_data = decode::<Claims>( | ||
bearer.token(), | ||
&app_state.jwt_decoding_key, | ||
&Validation::default(), | ||
) | ||
.map_err(|_| AuthError::InvalidToken)?; | ||
|
||
Ok(token_data.claims) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
|
||
pub mod bootstrap; | ||
pub mod error; | ||
mod extractor; | ||
pub mod settings; | ||
mod state; |