mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Update
This commit is contained in:
@@ -73,7 +73,26 @@ impl AuthTokenContext {
|
||||
if pass != &hashed_password {
|
||||
return Err((9, gettext("Wrong password.")).into());
|
||||
}
|
||||
Ok(json::object! {})
|
||||
let mut token = [0; 64];
|
||||
openssl::rand::rand_bytes(&mut token)
|
||||
.try_err3(-1003, gettext("Failed to generate token:"))?;
|
||||
let created_at = chrono::Utc::now();
|
||||
let expired_at = created_at + chrono::Duration::days(30);
|
||||
let token = loop {
|
||||
if let Some(token) = self
|
||||
.ctx
|
||||
.db
|
||||
.add_token(user.id, &token, &created_at, &expired_at)
|
||||
.await
|
||||
.try_err3(-1001, gettext("Failed to operate the database:"))?
|
||||
{
|
||||
break token;
|
||||
}
|
||||
};
|
||||
let b64token = base64::encode(&token.token);
|
||||
Ok(
|
||||
json::object! { "id": token.id, "user_id": token.user_id, "token": b64token, "created_at": token.created_at.timestamp(), "expired_at": token.expired_at.timestamp() },
|
||||
)
|
||||
}
|
||||
},
|
||||
None => {
|
||||
|
||||
@@ -103,7 +103,7 @@ impl ServerContext {
|
||||
par.insert(k, v);
|
||||
}
|
||||
let mut sha = openssl::sha::Sha512::new();
|
||||
sha.update(token.token.as_bytes());
|
||||
sha.update(&token.token);
|
||||
for (k, v) in par {
|
||||
for v in v {
|
||||
sha.update(k.as_bytes());
|
||||
|
||||
@@ -31,7 +31,7 @@ pub async fn test(ctx: &UnitTestContext) -> Result<BytesMut, PixivDownloaderErro
|
||||
&json::object! {
|
||||
"username" => "test",
|
||||
"name" => "test",
|
||||
"password" => b64_password,
|
||||
"password" => b64_password.as_str(),
|
||||
},
|
||||
)
|
||||
.await?
|
||||
@@ -46,5 +46,17 @@ pub async fn test(ctx: &UnitTestContext) -> Result<BytesMut, PixivDownloaderErro
|
||||
"is_admin": true,
|
||||
}
|
||||
);
|
||||
let re = ctx
|
||||
.request_json2(
|
||||
"/auth/token/add",
|
||||
&json::object! {
|
||||
"username": "test",
|
||||
"password": b64_password.as_str(),
|
||||
},
|
||||
)
|
||||
.await?
|
||||
.unwrap();
|
||||
let result = JSONResult::from_json(re)?.expect("Failed to add token:");
|
||||
assert_eq!(Some(0), result["user_id"].as_u64());
|
||||
Ok(BytesMut::new())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user