refactor: 重命名授权字段并优化令牌处理逻辑
- 将数据库中的授权字段从 `key` 重命名为 `token` 并调整相关查询和插入逻辑,同时修改 `get_token_info` 方法返回类型为 `Option<Authorize>` 以直接返回结构体而非 JSON 字符串。 - 添加令牌验证和信息查询功能,重构路由处理逻辑并优化数据结构
This commit is contained in:
33
src/db.rs
33
src/db.rs
@@ -1,5 +1,4 @@
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use std::{fs, path::Path};
|
||||
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
||||
|
||||
@@ -9,7 +8,7 @@ pub struct Authorize {
|
||||
#[welds(primary_key)]
|
||||
pub id: i32,
|
||||
pub project: String,
|
||||
pub key: String,
|
||||
pub token: String,
|
||||
pub device_id: String,
|
||||
pub expire: String,
|
||||
pub insert_time: String,
|
||||
@@ -17,7 +16,7 @@ pub struct Authorize {
|
||||
|
||||
pub(crate) struct InsertArgs {
|
||||
pub project: String,
|
||||
pub key: String,
|
||||
pub token: String,
|
||||
pub device_id: String,
|
||||
pub expire: String,
|
||||
pub insert_time: String,
|
||||
@@ -52,7 +51,7 @@ impl Db {
|
||||
CREATE TABLE IF NOT EXISTS authorize (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
project TEXT NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
token TEXT NOT NULL,
|
||||
device_id TEXT NOT NULL,
|
||||
expire TEXT NOT NULL,
|
||||
insert_time TEXT NOT NULL
|
||||
@@ -70,7 +69,7 @@ impl Db {
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let rows = Authorize::where_col(|p| p.key.equal(token))
|
||||
let rows = Authorize::where_col(|p| p.token.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
@@ -81,20 +80,16 @@ impl Db {
|
||||
pub async fn get_token_info(
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let rows: Vec<DbState<Authorize>> =
|
||||
Authorize::where_col(|p| p.key.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
) -> Result<Option<Authorize>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let row = Authorize::where_col(|p| p.token.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?
|
||||
.into_inners()
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
if let Some(auth) = rows.into_inners().into_iter().next() {
|
||||
// 转 JSON
|
||||
let json_str = serde_json::to_string(&auth)?;
|
||||
Ok(Some(json_str))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
/// 插入新的授权数据
|
||||
@@ -104,7 +99,7 @@ impl Db {
|
||||
) -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let mut auth = Authorize::new();
|
||||
auth.project = args.project.to_string();
|
||||
auth.key = args.key.to_string();
|
||||
auth.token = args.token.to_string();
|
||||
auth.device_id = args.device_id.to_string();
|
||||
auth.expire = args.expire.to_string();
|
||||
auth.insert_time = args.insert_time.to_string();
|
||||
|
||||
Reference in New Issue
Block a user