refactor: 更新授权返回类型并增强令牌验证逻辑
- 将insert_authorize方法的返回值从i32改为Authorize类型。 - 为令牌创建逻辑添加过期检查和状态验证功能。
This commit is contained in:
@@ -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
|
||||||
|
|||||||
47
src/main.rs
47
src/main.rs
@@ -44,17 +44,24 @@ 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 {
|
|
||||||
Some(info) => {
|
|
||||||
return (
|
return (
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(CreateTokenInfo {
|
Json(CreateTokenInfo {
|
||||||
@@ -62,35 +69,38 @@ async fn create_token(
|
|||||||
project: info.project,
|
project: info.project,
|
||||||
device_id: info.device_id,
|
device_id: info.device_id,
|
||||||
token: info.token,
|
token: info.token,
|
||||||
|
status: valid,
|
||||||
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user