后端重写提交
This commit is contained in:
@@ -10,6 +10,7 @@ sha2 = "0.10"
|
||||
jsonwebtoken = "9"
|
||||
rand_core = { version = "0.6", features = ["getrandom"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
shared-kernel = { path = "../shared-kernel" }
|
||||
time = { version = "0.3", features = ["std"] }
|
||||
urlencoding = "2"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
|
||||
@@ -7,8 +7,8 @@ use jsonwebtoken::{
|
||||
use rand_core::OsRng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use shared_kernel::{new_uuid_simple_string, normalize_optional_string, normalize_required_string};
|
||||
use time::{Duration, OffsetDateTime};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const ACCESS_TOKEN_ALGORITHM: Algorithm = Algorithm::HS256;
|
||||
pub const DEFAULT_ACCESS_TOKEN_TTL_SECONDS: u64 = 2 * 60 * 60;
|
||||
@@ -114,16 +114,10 @@ impl JwtConfig {
|
||||
secret: String,
|
||||
access_token_ttl_seconds: u64,
|
||||
) -> Result<Self, JwtError> {
|
||||
let issuer = issuer.trim().to_string();
|
||||
let secret = secret.trim().to_string();
|
||||
|
||||
if issuer.is_empty() {
|
||||
return Err(JwtError::InvalidConfig("JWT issuer 不能为空"));
|
||||
}
|
||||
|
||||
if secret.is_empty() {
|
||||
return Err(JwtError::InvalidConfig("JWT secret 不能为空"));
|
||||
}
|
||||
let issuer = normalize_required_string(&issuer)
|
||||
.ok_or(JwtError::InvalidConfig("JWT issuer 不能为空"))?;
|
||||
let secret = normalize_required_string(&secret)
|
||||
.ok_or(JwtError::InvalidConfig("JWT secret 不能为空"))?;
|
||||
|
||||
if access_token_ttl_seconds == 0 {
|
||||
return Err(JwtError::InvalidConfig(
|
||||
@@ -174,20 +168,12 @@ impl RefreshCookieConfig {
|
||||
cookie_same_site: RefreshCookieSameSite,
|
||||
refresh_session_ttl_days: u32,
|
||||
) -> Result<Self, RefreshCookieError> {
|
||||
let cookie_name = cookie_name.trim().to_string();
|
||||
let cookie_path = cookie_path.trim().to_string();
|
||||
|
||||
if cookie_name.is_empty() {
|
||||
return Err(RefreshCookieError::InvalidConfig(
|
||||
"refresh cookie 名称不能为空",
|
||||
));
|
||||
}
|
||||
|
||||
if cookie_path.is_empty() {
|
||||
return Err(RefreshCookieError::InvalidConfig(
|
||||
"refresh cookie path 不能为空",
|
||||
));
|
||||
}
|
||||
let cookie_name = normalize_required_string(&cookie_name).ok_or(
|
||||
RefreshCookieError::InvalidConfig("refresh cookie 名称不能为空"),
|
||||
)?;
|
||||
let cookie_path = normalize_required_string(&cookie_path).ok_or(
|
||||
RefreshCookieError::InvalidConfig("refresh cookie path 不能为空"),
|
||||
)?;
|
||||
|
||||
if refresh_session_ttl_days == 0 {
|
||||
return Err(RefreshCookieError::InvalidConfig(
|
||||
@@ -401,7 +387,7 @@ pub async fn verify_password(
|
||||
}
|
||||
|
||||
pub fn create_refresh_session_token() -> String {
|
||||
Uuid::new_v4().simple().to_string()
|
||||
new_uuid_simple_string()
|
||||
}
|
||||
|
||||
pub fn hash_refresh_session_token(token: &str) -> String {
|
||||
@@ -484,23 +470,11 @@ fn normalize_required_field(
|
||||
value: String,
|
||||
error_message: &'static str,
|
||||
) -> Result<String, JwtError> {
|
||||
let value = value.trim().to_string();
|
||||
if value.is_empty() {
|
||||
return Err(JwtError::InvalidClaims(error_message));
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
normalize_required_string(&value).ok_or(JwtError::InvalidClaims(error_message))
|
||||
}
|
||||
|
||||
fn normalize_optional_field(value: Option<String>) -> Option<String> {
|
||||
value.and_then(|field| {
|
||||
let field = field.trim().to_string();
|
||||
if field.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(field)
|
||||
})
|
||||
normalize_optional_string(value)
|
||||
}
|
||||
|
||||
fn normalize_roles(roles: Vec<String>) -> Result<Vec<String>, JwtError> {
|
||||
@@ -681,7 +655,7 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
hash,
|
||||
"0b6901f0dcee3f50df4115ecb29214f7740f8173919f94cc1f5eb92ff2481ce8"
|
||||
"9fab76f9100ec6c151c8caa0c42ab10e10fbc7618f15e24cf3dffc93e19c4c4e"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user