-
Notifications
You must be signed in to change notification settings - Fork 3
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
74319ce
commit b00e490
Showing
12 changed files
with
42 additions
and
30 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
pub mod jwt; | ||
pub mod registration; | ||
|
||
use sea_orm::FromJsonQueryResult; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, FromJsonQueryResult, PartialEq, Eq, Default)] | ||
pub struct Config { | ||
pub jwt: jwt::Config, | ||
pub registration: registration::Config, | ||
} |
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,7 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Env { | ||
pub secret: String, | ||
pub expiration: i64, | ||
} |
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,7 +1,10 @@ | ||
pub mod jwt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Env { | ||
pub host: String, | ||
pub port: u16, | ||
pub jwt: jwt::Env, | ||
} |
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,29 +1,44 @@ | ||
use jsonwebtoken::{EncodingKey, Header, encode}; | ||
use regex::Regex; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Claims { | ||
pub id: i64, | ||
pub exp: usize, | ||
} | ||
|
||
pub async fn get_secret() -> String { | ||
cds_config::get_config().await.auth.jwt.secret_key | ||
pub async fn get_jwt_config() -> cds_env::axum::jwt::Env { | ||
if let Some(jwt) = cds_cache::get::<cds_env::axum::jwt::Env>("jwt") | ||
.await | ||
.unwrap() | ||
{ | ||
return jwt; | ||
} | ||
|
||
let mut jwt = cds_env::get_env().axum.jwt.clone(); | ||
let re = Regex::new(r"\[([Uu][Uu][Ii][Dd])]").unwrap(); | ||
jwt.secret = re | ||
.replace_all(&jwt.secret, Uuid::new_v4().simple().to_string()) | ||
.to_string(); | ||
let _ = cds_cache::set("jwt", jwt.clone()).await; | ||
|
||
jwt | ||
} | ||
|
||
pub async fn generate_jwt_token(user_id: i64) -> String { | ||
let secret = get_secret().await; | ||
let jwt_config = get_jwt_config().await; | ||
let claims = Claims { | ||
id: user_id, | ||
exp: (chrono::Utc::now() | ||
+ chrono::Duration::minutes(cds_config::get_config().await.auth.jwt.expiration)) | ||
.timestamp() as usize, | ||
exp: (chrono::Utc::now() + chrono::Duration::minutes(jwt_config.expiration)).timestamp() | ||
as usize, | ||
}; | ||
|
||
encode( | ||
&Header::default(), | ||
&claims, | ||
&EncodingKey::from_secret(secret.as_bytes()), | ||
&EncodingKey::from_secret(jwt_config.secret.as_bytes()), | ||
) | ||
.unwrap() | ||
} |