-
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.
- Loading branch information
1 parent
ace3e2c
commit 8c53437
Showing
8 changed files
with
117 additions
and
80 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,74 +1,2 @@ | ||
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; | ||
use rocket::http::Status; | ||
use rocket::request::{FromRequest, Outcome}; | ||
use rocket_db_pools::deadpool_redis::redis::AsyncCommands; | ||
|
||
use crate::config::Config; | ||
use crate::{Claims, RedisDb}; | ||
|
||
pub struct JwtMiddleware { | ||
pub username: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum JwtError { | ||
ConfigError, | ||
CacheError, | ||
MissingToken, | ||
InvalidToken, | ||
ExpiredToken, | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for JwtMiddleware { | ||
type Error = JwtError; | ||
|
||
async fn from_request(request: &'r rocket::Request<'_>) -> Outcome<Self, Self::Error> { | ||
let config = match request.rocket().figment().extract::<Config>() { | ||
Ok(config) => config, | ||
Err(_) => return Outcome::Error((Status::InternalServerError, JwtError::ConfigError)), | ||
}; | ||
|
||
let token = match request.headers().get_one("Authorization") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, JwtError::MissingToken)), | ||
}; | ||
|
||
let token = match token.strip_prefix("Bearer ") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, JwtError::MissingToken)), | ||
}; | ||
|
||
let is_in_white_list: &Option<bool> = request | ||
.local_cache_async(async { | ||
let redis = request.guard::<&RedisDb>().await.succeeded()?; | ||
let mut connection = redis.get().await.ok()?; | ||
|
||
let result = connection.exists(token).await.ok()?; | ||
|
||
Some(result) | ||
}) | ||
.await; | ||
|
||
if is_in_white_list.is_none() { | ||
return Outcome::Error((Status::Unauthorized, JwtError::CacheError)); | ||
} | ||
|
||
if *is_in_white_list == Some(false) { | ||
return Outcome::Error((Status::Unauthorized, JwtError::InvalidToken)); | ||
} | ||
|
||
let token_data = match decode::<Claims>( | ||
token, | ||
&DecodingKey::from_secret(config.jwt.secret.as_bytes()), | ||
&Validation::new(Algorithm::HS256), | ||
) { | ||
Ok(token) => token, | ||
Err(_) => return Outcome::Error((Status::Unauthorized, JwtError::InvalidToken)), | ||
}; | ||
|
||
let username = token_data.claims.sub.clone(); | ||
|
||
Outcome::Success(JwtMiddleware { username }) | ||
} | ||
} | ||
pub mod jwt; | ||
pub mod token; |
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,74 @@ | ||
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; | ||
use rocket::http::Status; | ||
use rocket::request::{FromRequest, Outcome}; | ||
use rocket_db_pools::deadpool_redis::redis::AsyncCommands; | ||
|
||
use crate::config::Config; | ||
use crate::{Claims, RedisDb}; | ||
|
||
pub struct JwtMiddleware { | ||
pub username: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum JwtError { | ||
ConfigError, | ||
CacheError, | ||
MissingToken, | ||
InvalidToken, | ||
ExpiredToken, | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for JwtMiddleware { | ||
type Error = JwtError; | ||
|
||
async fn from_request(request: &'r rocket::Request<'_>) -> Outcome<Self, Self::Error> { | ||
let config = match request.rocket().figment().extract::<Config>() { | ||
Ok(config) => config, | ||
Err(_) => return Outcome::Error((Status::InternalServerError, JwtError::ConfigError)), | ||
}; | ||
|
||
let token = match request.headers().get_one("Authorization") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, JwtError::MissingToken)), | ||
}; | ||
|
||
let token = match token.strip_prefix("Bearer ") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, JwtError::MissingToken)), | ||
}; | ||
|
||
let is_in_white_list: &Option<bool> = request | ||
.local_cache_async(async { | ||
let redis = request.guard::<&RedisDb>().await.succeeded()?; | ||
let mut connection = redis.get().await.ok()?; | ||
|
||
let result = connection.exists(token).await.ok()?; | ||
|
||
Some(result) | ||
}) | ||
.await; | ||
|
||
if is_in_white_list.is_none() { | ||
return Outcome::Error((Status::Unauthorized, JwtError::CacheError)); | ||
} | ||
|
||
if *is_in_white_list == Some(false) { | ||
return Outcome::Error((Status::Unauthorized, JwtError::InvalidToken)); | ||
} | ||
|
||
let token_data = match decode::<Claims>( | ||
token, | ||
&DecodingKey::from_secret(config.jwt.secret.as_bytes()), | ||
&Validation::new(Algorithm::HS256), | ||
) { | ||
Ok(token) => token, | ||
Err(_) => return Outcome::Error((Status::Unauthorized, JwtError::InvalidToken)), | ||
}; | ||
|
||
let username = token_data.claims.sub.clone(); | ||
|
||
Outcome::Success(JwtMiddleware { username }) | ||
} | ||
} |
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,33 @@ | ||
use rocket::http::Status; | ||
use rocket::request::{FromRequest, Outcome}; | ||
|
||
pub struct TokenMiddleware { | ||
pub token: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TokenError { | ||
MissingToken, | ||
InvalidToken, | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for TokenMiddleware { | ||
type Error = TokenError; | ||
|
||
async fn from_request(request: &'r rocket::Request<'_>) -> Outcome<Self, Self::Error> { | ||
let token = match request.headers().get_one("Authorization") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, TokenError::MissingToken)), | ||
}; | ||
|
||
let token = match token.strip_prefix("Bearer ") { | ||
Some(token) => token, | ||
None => return Outcome::Error((Status::Unauthorized, TokenError::InvalidToken)), | ||
}; | ||
|
||
Outcome::Success(Self { | ||
token: token.to_string(), | ||
}) | ||
} | ||
} |
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
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