-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verify_jwt_claims for JwtAuthService
- Loading branch information
Showing
21 changed files
with
422 additions
and
187 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
use axum::{body::Body, middleware::Next, response::Response}; | ||
use zino::{prelude::*, Request, Result}; | ||
use zino_model::user::{JwtAuthService, User}; | ||
|
||
pub async fn init_user_session(mut req: Request, next: Next<Body>) -> Result<Response> { | ||
if let Ok(claims) = req.parse_jwt_claims(JwtClaims::shared_key()) | ||
&& let Ok(mut user_session) = UserSession::<Uuid>::try_from_jwt_claims(claims) | ||
{ | ||
if let Ok(session_id) = req.parse_session_id() { | ||
user_session.set_session_id(session_id); | ||
if let Ok(claims) = req.parse_jwt_claims(JwtClaims::shared_key()) { | ||
match User::verify_jwt_claims(&claims).await { | ||
Ok(verified) => { | ||
if verified && | ||
let Ok(mut user_session) = UserSession::<Uuid>::try_from_jwt_claims(claims) | ||
{ | ||
if let Ok(session_id) = req.parse_session_id() { | ||
user_session.set_session_id(session_id); | ||
} | ||
req.set_data(user_session); | ||
} else { | ||
reject!(req, unauthorized, "invalid JWT claims"); | ||
} | ||
} | ||
Err(err) => reject!(req, unauthorized, err), | ||
} | ||
req.set_data(user_session); | ||
} else if req.request_method() == "POST" { | ||
reject!(req, unauthorized, "login is required"); | ||
} | ||
Ok(next.run(req.into()).await) | ||
} |
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
Oops, something went wrong.