refactor: 更新授权返回类型并增强令牌验证逻辑

- 将insert_authorize方法的返回值从i32改为Authorize类型。
- 为令牌创建逻辑添加过期检查和状态验证功能。
This commit is contained in:
2025-08-28 16:18:14 +08:00
parent 1d107a97ff
commit 6e8eb0aaaa
2 changed files with 41 additions and 30 deletions

View File

@@ -133,7 +133,7 @@ impl Db {
pub async fn insert_authorize( pub async fn insert_authorize(
&self, &self,
args: InsertArgs, args: InsertArgs,
) -> Result<i32, Box<dyn std::error::Error>> { ) -> Result<Authorize, Box<dyn std::error::Error>> {
let mut auth = Authorize::new(); let mut auth = Authorize::new();
auth.project = args.project.to_string(); auth.project = args.project.to_string();
auth.token = args.token.to_string(); auth.token = args.token.to_string();
@@ -142,8 +142,8 @@ impl Db {
auth.expire = args.expire.to_string(); auth.expire = args.expire.to_string();
auth.insert_time = args.insert_time.to_string(); auth.insert_time = args.insert_time.to_string();
let _created = auth.save(&self.client).await?; auth.save(&self.client).await?;
Ok(auth.id) Ok(auth.into_inner())
} }
/// 禁用 Token /// 禁用 Token

View File

@@ -44,53 +44,63 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
fn check_datetime(t: &str) -> Result<bool, ParseError> {
let expire_time = NaiveDateTime::parse_from_str(&t, "%Y-%m-%d %H:%M:%S")?;
Ok(Local::now().naive_local() < expire_time)
}
fn is_valid(disable: i8, expire: &str) -> bool {
disable == 1 && check_datetime(expire).unwrap_or(false)
}
async fn create_token( async fn create_token(
State(state): State<AppState>, State(state): State<AppState>,
Query(args): Query<CreateToken>, Query(args): Query<CreateToken>,
) -> (StatusCode, Json<CreateTokenInfo>) { ) -> (StatusCode, Json<CreateTokenInfo>) {
let exists = state let CreateToken { project, device_id } = args;
.db if let Some(info) =
.exists_project(&args.project, &args.device_id) state.db.exists_project(&project, &device_id).await.unwrap()
.await {
.unwrap(); let valid = is_valid(info.disable, &info.expire);
match exists { return (
Some(info) => { StatusCode::OK,
return ( Json(CreateTokenInfo {
StatusCode::OK, code: 200,
Json(CreateTokenInfo { project: info.project,
code: 200, device_id: info.device_id,
project: info.project, token: info.token,
device_id: info.device_id, status: valid,
token: info.token, msg: "token已存在请勿重复创建".to_owned(),
msg: "token已存在请勿重复创建".to_owned(), }),
}), );
)
}
None => (),
} }
let str_time = get_current_datetime(); let str_time = get_current_datetime();
let exp_time = add_day(&str_time, 7).unwrap(); let exp_time = add_day(&str_time, 7).unwrap();
let token: String = state.generator.generate(16); let token: String = state.generator.generate(16);
let _token_id = state let create_auth = state
.db .db
.insert_authorize(InsertArgs { .insert_authorize(InsertArgs {
project: args.project.clone(), project,
token: token.clone(), token: token,
device_id: args.device_id.clone(), device_id,
disable: 1, disable: 1,
expire: exp_time, expire: exp_time,
insert_time: str_time, insert_time: str_time,
}) })
.await; .await
.unwrap();
let valid = is_valid(create_auth.disable, &create_auth.expire);
( (
StatusCode::OK, StatusCode::OK,
Json(CreateTokenInfo { Json(CreateTokenInfo {
code: 200, code: 200,
project: args.project, project: create_auth.project,
device_id: args.device_id, device_id: create_auth.device_id,
token, token: create_auth.token,
status: valid,
msg: "token创建成功".to_owned(), msg: "token创建成功".to_owned(),
}), }),
) )
@@ -341,6 +351,7 @@ struct CreateTokenInfo {
project: String, project: String,
device_id: String, device_id: String,
token: String, token: String,
status: bool,
msg: String, msg: String,
} }