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 serde::Serialize;
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
||||||
@@ -87,11 +88,23 @@ impl Db {
|
|||||||
&self,
|
&self,
|
||||||
token: &str,
|
token: &str,
|
||||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
) -> 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)
|
.limit(1)
|
||||||
.run(&self.client)
|
.run(&self.client)
|
||||||
.await?;
|
.await?
|
||||||
Ok(!rows.is_empty())
|
.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> 如果存在
|
/// 查询 token 的详细信息,返回 Option<Authorize> 如果存在
|
||||||
|
|||||||
21
src/main.rs
21
src/main.rs
@@ -98,8 +98,8 @@ async fn create_token(
|
|||||||
project: args.project.clone(),
|
project: args.project.clone(),
|
||||||
token: token.clone(),
|
token: token.clone(),
|
||||||
device_id: args.device_id.clone(),
|
device_id: args.device_id.clone(),
|
||||||
expire: str_time,
|
expire: exp_time,
|
||||||
insert_time: exp_time,
|
insert_time: str_time,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@@ -120,9 +120,21 @@ async fn verify_token(
|
|||||||
Query(args): Query<VerifyToken>,
|
Query(args): Query<VerifyToken>,
|
||||||
) -> (StatusCode, Json<VerifyResult>) {
|
) -> (StatusCode, Json<VerifyResult>) {
|
||||||
if state.db.verify_token(&args.token).await.unwrap() {
|
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 {
|
} 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)]
|
#[derive(Serialize)]
|
||||||
struct VerifyResult {
|
struct VerifyResult {
|
||||||
code: i16,
|
code: i16,
|
||||||
|
msg: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|||||||
Reference in New Issue
Block a user