mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 06:48:37 +08:00
JWT Authentication and token generation
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::utils::jwt::check_jwt;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
use pingora_proxy::Session;
|
||||
@@ -7,6 +8,7 @@ trait AuthValidator {
|
||||
}
|
||||
struct BasicAuth<'a>(&'a str);
|
||||
struct ApiKeyAuth<'a>(&'a str);
|
||||
struct JwtAuth<'a>(&'a str);
|
||||
|
||||
impl AuthValidator for BasicAuth<'_> {
|
||||
fn validate(&self, session: &Session) -> bool {
|
||||
@@ -30,6 +32,16 @@ impl AuthValidator for ApiKeyAuth<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthValidator for JwtAuth<'_> {
|
||||
fn validate(&self, session: &Session) -> bool {
|
||||
let jwtsecret = self.0;
|
||||
if let Some(header) = session.get_header("x-jwt-token") {
|
||||
let tok = header.to_str().ok().unwrap();
|
||||
return check_jwt(tok, jwtsecret);
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
fn validate(auth: &dyn AuthValidator, session: &Session) -> bool {
|
||||
auth.validate(session)
|
||||
}
|
||||
@@ -44,6 +56,10 @@ pub fn authenticate(c: &[String], session: &Session) -> bool {
|
||||
let auth = ApiKeyAuth(c[1].as_str().into());
|
||||
validate(&auth, session)
|
||||
}
|
||||
"jwt" => {
|
||||
let auth = JwtAuth(c[1].as_str().into());
|
||||
validate(&auth, session)
|
||||
}
|
||||
_ => {
|
||||
println!("Unsupported authentication mechanism : {}", c[0]);
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user