Skip to content

Commit

Permalink
fix: Set default value for Jwt cookie name (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski authored Aug 11, 2024
1 parent 4a2586a commit 9ede96c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
13 changes: 10 additions & 3 deletions src/config/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::util::serde::UriOrString;
use axum::http::header::AUTHORIZATION;
use serde_derive::{Deserialize, Serialize};
use validator::Validate;

Expand All @@ -15,9 +16,9 @@ pub struct Auth {
#[non_exhaustive]
pub struct Jwt {
/// Name of the cookie used to pass the JWT access token. If not set, will use
/// [`axum::http::header::AUTHORIZATION`] as the cookie name.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cookie_name: Option<String>,
/// [`AUTHORIZATION`] as the cookie name.
#[serde(default = "Jwt::default_cookie_name")]
pub cookie_name: String,

pub secret: String,

Expand All @@ -26,6 +27,12 @@ pub struct Jwt {
pub claims: JwtClaims,
}

impl Jwt {
fn default_cookie_name() -> String {
AUTHORIZATION.as_str().to_string()
}
}

#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
#[non_exhaustive]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/config/auth/mod.rs
expression: auth
---
[jwt]
cookie-name = 'authorization'
secret = 'foo'

[jwt.claims]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/config/auth/mod.rs
expression: auth
---
[jwt]
cookie-name = 'authorization'
secret = 'foo'

[jwt.claims]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/config/auth/mod.rs
expression: auth
---
[jwt]
cookie-name = 'authorization'
secret = 'foo'

[jwt.claims]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/config/auth/mod.rs
expression: auth
---
[jwt]
cookie-name = 'authorization'
secret = 'foo'

[jwt.claims]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ timeout = true
max-duration = 60
disable-argument-coercion = false
[auth.jwt]
cookie-name = 'authorization'
secret = 'secret-test'

[auth.jwt.claims]
Expand Down
12 changes: 3 additions & 9 deletions src/middleware/http/auth/jwt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::middleware::http::auth::jwt::openid::Claims;
use crate::util::serde::{deserialize_from_str, serialize_to_str};
use async_trait::async_trait;
use axum::extract::{FromRef, FromRequestParts};
use axum::http::header::AUTHORIZATION;
use axum::http::request::Parts;
use axum::RequestPartsExt;
use axum_extra::extract::CookieJar;
Expand Down Expand Up @@ -89,16 +88,10 @@ where
}

fn bearer_token_from_cookies(context: &AppContext, cookies: Option<CookieJar>) -> Option<String> {
let cookie_name = context
.config()
.auth
.jwt
.cookie_name
.clone()
.unwrap_or_else(|| AUTHORIZATION.to_string());
let cookie_name = &context.config().auth.jwt.cookie_name;
cookies
.as_ref()
.and_then(|cookies| cookies.get(&cookie_name))
.and_then(|cookies| cookies.get(cookie_name))
.map(|cookie| cookie.value())
.and_then(|token| HeaderValue::from_str(token).ok())
.and_then(|header_value| {
Expand Down Expand Up @@ -168,6 +161,7 @@ mod tests {
use super::*;
use crate::testing::snapshot::TestCase;
use crate::util::serde::Wrapper;
use axum::http::header::AUTHORIZATION;
use axum_extra::extract::cookie::Cookie;
use insta::assert_debug_snapshot;
use rstest::{fixture, rstest};
Expand Down

0 comments on commit 9ede96c

Please sign in to comment.