feat: 添加令牌过期时间验证并优化查询处理
- 在数据库查询逻辑中添加令牌过期时间验证功能,通过比较当前时间与存储的过期时间来判断令牌有效性,并优化查询结果处理方式。 - 交换令牌创建中的过期时间和插入时间字段,并在令牌验证结果中添加状态消息。
This commit is contained in:
19
src/db.rs
19
src/db.rs
@@ -1,3 +1,4 @@
|
||||
use chrono::{Local, NaiveDateTime};
|
||||
use serde::Serialize;
|
||||
use std::{fs, path::Path};
|
||||
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
||||
@@ -87,11 +88,23 @@ impl Db {
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let rows = Authorize::where_col(|p| p.token.equal(token))
|
||||
let row = Authorize::where_col(|p| p.token.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
Ok(!rows.is_empty())
|
||||
.await?
|
||||
.into_iter()
|
||||
.next();
|
||||
match row {
|
||||
Some(a) => {
|
||||
// 判断截止时间
|
||||
let expire_time =
|
||||
NaiveDateTime::parse_from_str(&a.expire, "%Y-%m-%d %H:%M:%S")?;
|
||||
let now_time = Local::now().naive_local();
|
||||
return Ok(now_time < expire_time);
|
||||
}
|
||||
// 如果没有找到,直接返回 false
|
||||
None => return Ok(false),
|
||||
};
|
||||
}
|
||||
|
||||
/// 查询 token 的详细信息,返回 Option<Authorize> 如果存在
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -98,8 +98,8 @@ async fn create_token(
|
||||
project: args.project.clone(),
|
||||
token: token.clone(),
|
||||
device_id: args.device_id.clone(),
|
||||
expire: str_time,
|
||||
insert_time: exp_time,
|
||||
expire: exp_time,
|
||||
insert_time: str_time,
|
||||
})
|
||||
.await;
|
||||
|
||||
@@ -120,9 +120,21 @@ async fn verify_token(
|
||||
Query(args): Query<VerifyToken>,
|
||||
) -> (StatusCode, Json<VerifyResult>) {
|
||||
if state.db.verify_token(&args.token).await.unwrap() {
|
||||
(StatusCode::OK, Json(VerifyResult { code: 200 }))
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(VerifyResult {
|
||||
code: 200,
|
||||
msg: "正常".to_owned(),
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
(StatusCode::OK, Json(VerifyResult { code: 404 }))
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(VerifyResult {
|
||||
code: 404,
|
||||
msg: "token已过期".to_owned(),
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +196,7 @@ struct VerifyToken {
|
||||
#[derive(Serialize)]
|
||||
struct VerifyResult {
|
||||
code: i16,
|
||||
msg: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
||||
Reference in New Issue
Block a user