修改refresh token生成逻辑
This commit is contained in:
parent
1a39728d3e
commit
44bbc846df
@ -9,6 +9,7 @@ pub struct Claims {
|
|||||||
pub exp: i64, // Token过期时间戳
|
pub exp: i64, // Token过期时间戳
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn generate_token(sub: &str) -> String {
|
pub fn generate_token(sub: &str) -> String {
|
||||||
let claim = Claims {
|
let claim = Claims {
|
||||||
sub: sub.to_string(),
|
sub: sub.to_string(),
|
||||||
@ -25,10 +26,11 @@ pub fn generate_token(sub: &str) -> String {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn generate_refresh_token(sub: &str) -> String {
|
pub fn generate_refresh_token(sub: &str) -> String {
|
||||||
let claim = Claims {
|
let claim = Claims {
|
||||||
sub: sub.to_string(),
|
sub: sub.to_string(),
|
||||||
exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.expires).unwrap()).timestamp(),
|
exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.refresh_expires).unwrap()).timestamp(),
|
||||||
};
|
};
|
||||||
let token = jsonwebtoken::encode(
|
let token = jsonwebtoken::encode(
|
||||||
&jsonwebtoken::Header::default(),
|
&jsonwebtoken::Header::default(),
|
||||||
@ -36,11 +38,12 @@ pub fn generate_refresh_token(sub: &str) -> String {
|
|||||||
&jsonwebtoken::EncodingKey::from_secret(config!().jwt.refresh_token_secret.as_bytes()),
|
&jsonwebtoken::EncodingKey::from_secret(config!().jwt.refresh_token_secret.as_bytes()),
|
||||||
);
|
);
|
||||||
token.unwrap_or_else(|e| {
|
token.unwrap_or_else(|e| {
|
||||||
tracing::error!(error =?e, "生成Rfresh Token失败");
|
tracing::error!(error =?e, "生成Refresh Token失败");
|
||||||
"".to_string()
|
"".to_string()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn verify_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
|
pub fn verify_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
|
||||||
jsonwebtoken::decode::<Claims>(
|
jsonwebtoken::decode::<Claims>(
|
||||||
token,
|
token,
|
||||||
@ -50,6 +53,7 @@ pub fn verify_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error>
|
|||||||
.map(|data| data.claims)
|
.map(|data| data.claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn verify_refresh_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
|
pub fn verify_refresh_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
|
||||||
jsonwebtoken::decode::<Claims>(
|
jsonwebtoken::decode::<Claims>(
|
||||||
token,
|
token,
|
||||||
|
Loading…
Reference in New Issue
Block a user