-
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
root
authored and
root
committed
Feb 5, 2024
1 parent
4cf7d01
commit 79621f7
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
#[derive(Clone, Debug)] | ||
pub struct Ctx { | ||
user_id: u64, | ||
} | ||
|
||
impl Ctx { | ||
pub fn new(user_id: u64) -> Self { | ||
Self { user_id } | ||
} | ||
} | ||
|
||
impl Ctx { | ||
pub fn user_id(&self) -> u64 { | ||
self.user_id | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod mw_auth; | ||
pub mod routes_login; | ||
pub mod routes_tickets; | ||
|
||
|
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,49 @@ | ||
use crate::ctx::Ctx; | ||
use crate::web::AUTH_TOKEN; | ||
use crate::{Error, Result}; | ||
use async_trait::async_trait; | ||
use axum::extract::FromRequestParts; | ||
use axum::http::request::Parts; | ||
use axum::RequestPartsExt; | ||
use axum::{body::Body, extract::Request, middleware::Next, response::Response}; | ||
use lazy_regex::regex_captures; | ||
use tower_cookies::Cookies; | ||
|
||
pub async fn mw_require_auth(cookies: Cookies, req: Request<Body>, next: Next) -> Result<Response> { | ||
let auth_token = cookies.get(AUTH_TOKEN).map(|c| c.to_string()); | ||
|
||
let (user_id, exp, sign) = auth_token | ||
.ok_or(Error::AuthFailNoAuthTokenCookie) | ||
.and_then(parse_token)?; | ||
|
||
Ok(next.run(req).await) | ||
} | ||
|
||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for Ctx { | ||
type Rejection = Error; | ||
|
||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self> { | ||
println!("->> {:<12} - Ctx", "EXTRACTOR"); | ||
|
||
parts | ||
.extensions | ||
.get::<Result<Ctx>>() | ||
.ok_or(Error::AuthFailCtxNotInRequestExt)? | ||
.clone() | ||
} | ||
} | ||
|
||
// Parse token of format `user-[user-id]-[expiration].[signature]` | ||
// Returns (user_id, expiration, signature) | ||
fn parse_token(token: String) -> Result<(u64, String, String)> { | ||
let (_whole, user_id, exp, sign) = regex_captures!(r#"^user-(\d+)\.(.+)\.(.+)"#, &token) | ||
.ok_or(Error::AuthFailTokenWrongFormat) | ||
.unwrap(); | ||
|
||
let user_id: u64 = user_id | ||
.parse() | ||
.map_err(|_| Error::AuthFailTokenWrongFormat)?; | ||
|
||
Ok((user_id, exp.to_string(), sign.to_string())) | ||
} |