-
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.
Put JWT extraction in separate module (#19)
In addition: - Add tests for JWT extraction - Write a debug log when JWT extraction fails (it's logged when validation fails, extraction should be consistent) - Rename JWT module that performs validation
- Loading branch information
Showing
5 changed files
with
89 additions
and
24 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,63 @@ | ||
use http::HeaderMap; | ||
|
||
use crate::error::AuthError; | ||
|
||
pub trait JwtExtractor { | ||
fn extract_jwt(&self, headers: &HeaderMap) -> Result<String, AuthError>; | ||
} | ||
|
||
pub struct BearerTokenJwtExtractor; | ||
|
||
impl JwtExtractor for BearerTokenJwtExtractor { | ||
fn extract_jwt(&self, headers: &HeaderMap) -> Result<String, AuthError> { | ||
Ok(headers | ||
.get(http::header::AUTHORIZATION) | ||
.ok_or(AuthError::MissingAuthorizationHeader)? | ||
.to_str() | ||
.map_err(|_| AuthError::InvalidAuthorizationHeader)? | ||
.strip_prefix("Bearer ") | ||
.ok_or(AuthError::InvalidAuthorizationHeader)? | ||
.to_owned()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use http::HeaderValue; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_missing_authorization() { | ||
let headers = HeaderMap::new(); | ||
let result = BearerTokenJwtExtractor {}.extract_jwt(&headers); | ||
|
||
assert!(result.is_err()); | ||
assert_eq!(result.unwrap_err(), AuthError::MissingAuthorizationHeader); | ||
} | ||
|
||
#[test] | ||
fn test_missing_bearer_prefix() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
"Authorization", | ||
HeaderValue::from_str("Boarer XXX").unwrap(), | ||
); | ||
let result = BearerTokenJwtExtractor {}.extract_jwt(&headers); | ||
|
||
assert!(result.is_err()); | ||
assert_eq!(result.unwrap_err(), AuthError::InvalidAuthorizationHeader); | ||
} | ||
|
||
#[test] | ||
fn test_ok() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
"Authorization", | ||
HeaderValue::from_str("Bearer XXX").unwrap(), | ||
); | ||
let result = BearerTokenJwtExtractor {}.extract_jwt(&headers); | ||
|
||
assert!(result.is_ok()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ pub mod validation; | |
|
||
mod error; | ||
mod jwks; | ||
mod jwt; | ||
mod jwt_extract; | ||
mod jwt_validate; | ||
mod oidc; |
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