Skip to content

Commit

Permalink
Merge d6a7f93 into c24edb1
Browse files Browse the repository at this point in the history
  • Loading branch information
msrd0 committed Apr 13, 2021
2 parents c24edb1 + d6a7f93 commit e311902
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
10 changes: 8 additions & 2 deletions middleware/jwt/src/lib.rs
Expand Up @@ -6,7 +6,8 @@
//! Status Code `400: Bad Request`. Tokens that fail
//! validation cause the middleware to return Status Code
//! `401: Unauthorized`.
#![warn(missing_docs, deprecated)]
#![warn(missing_docs, rust_2018_idioms)]

#[macro_use]
extern crate gotham_derive;
#[macro_use]
Expand All @@ -18,5 +19,10 @@ extern crate serde_derive;
mod middleware;
mod state_data;

pub use self::middleware::JWTMiddleware;
pub use self::middleware::JwtMiddleware;
pub use self::state_data::AuthorizationToken;

/// This type alias is deprecated. Use `JwtMiddleware` instead.
#[deprecated(since = "0.6.1", note = "Please use `JwtMiddleware` instead")]
#[allow(clippy::upper_case_acronyms)]
pub type JWTMiddleware<T> = JwtMiddleware<T>;
33 changes: 16 additions & 17 deletions middleware/jwt/src/middleware.rs
Expand Up @@ -47,7 +47,7 @@ const DEFAULT_SCHEME: &str = "Bearer";
/// router::{builder::*, Router},
/// state::{FromState, State},
/// };
/// use gotham_middleware_jwt::{AuthorizationToken, JWTMiddleware};
/// use gotham_middleware_jwt::{AuthorizationToken, JwtMiddleware};
/// use std::pin::Pin;
///
/// #[derive(Deserialize, Debug)]
Expand All @@ -69,7 +69,7 @@ const DEFAULT_SCHEME: &str = "Bearer";
/// let pipelines = new_pipeline_set();
/// let (pipelines, defaults) = pipelines.add(
/// new_pipeline()
/// .add(JWTMiddleware::<Claims>::new("secret"))
/// .add(JwtMiddleware::<Claims>::new("secret"))
/// .build(),
/// );
/// let default_chain = (defaults, ());
Expand All @@ -83,15 +83,14 @@ const DEFAULT_SCHEME: &str = "Bearer";
/// # let _ = router();
/// # }
/// ```
#[allow(clippy::upper_case_acronyms)]
pub struct JWTMiddleware<T> {
pub struct JwtMiddleware<T> {
secret: String,
validation: Validation,
scheme: String,
claims: PhantomData<T>,
}

impl<T> JWTMiddleware<T>
impl<T> JwtMiddleware<T>
where
T: for<'de> Deserialize<'de> + Send + Sync,
{
Expand All @@ -100,7 +99,7 @@ where
pub fn new<S: Into<String>>(secret: S) -> Self {
let validation = Validation::default();

JWTMiddleware {
Self {
secret: secret.into(),
validation,
scheme: DEFAULT_SCHEME.into(),
Expand All @@ -111,19 +110,19 @@ where
/// Create a new instance of the middleware by appending new
/// validation constraints.
pub fn validation(self, validation: Validation) -> Self {
JWTMiddleware { validation, ..self }
Self { validation, ..self }
}

/// Create a new instance of the middleware with a custom scheme
pub fn scheme<S: Into<String>>(self, scheme: S) -> Self {
JWTMiddleware {
Self {
scheme: scheme.into(),
..self
}
}
}

impl<T> Middleware for JWTMiddleware<T>
impl<T> Middleware for JwtMiddleware<T>
where
T: for<'de> Deserialize<'de> + Send + Sync + 'static,
{
Expand Down Expand Up @@ -169,14 +168,14 @@ where
}
}

impl<T> NewMiddleware for JWTMiddleware<T>
impl<T> NewMiddleware for JwtMiddleware<T>
where
T: for<'de> Deserialize<'de> + RefUnwindSafe + Send + Sync + 'static,
{
type Instance = JWTMiddleware<T>;
type Instance = Self;

fn new_middleware(&self) -> anyhow::Result<Self::Instance> {
Ok(JWTMiddleware {
Ok(Self {
secret: self.secret.clone(),
validation: self.validation.clone(),
scheme: self.scheme.clone(),
Expand Down Expand Up @@ -234,17 +233,17 @@ mod tests {
future::ok((state, res)).boxed()
}

fn default_jwt_middleware() -> JWTMiddleware<Claims> {
JWTMiddleware::<Claims>::new(SECRET).validation(Validation::default())
fn default_jwt_middleware() -> JwtMiddleware<Claims> {
JwtMiddleware::<Claims>::new(SECRET).validation(Validation::default())
}

fn jwt_middleware_with_scheme(scheme: &str) -> JWTMiddleware<Claims> {
JWTMiddleware::<Claims>::new(SECRET)
fn jwt_middleware_with_scheme(scheme: &str) -> JwtMiddleware<Claims> {
JwtMiddleware::<Claims>::new(SECRET)
.validation(Validation::default())
.scheme(scheme)
}

fn router(middleware: JWTMiddleware<Claims>) -> Router {
fn router(middleware: JwtMiddleware<Claims>) -> Router {
// Create JWTMiddleware with HS256 algorithm (default).

let (chain, pipelines) = single_pipeline(new_pipeline().add(middleware).build());
Expand Down

0 comments on commit e311902

Please sign in to comment.