This commit is contained in:
2022-09-28 09:22:33 +00:00
committed by GitHub
parent 2c93b98c65
commit 79b22e3501
6 changed files with 116 additions and 7 deletions

View File

@@ -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 => {

View File

@@ -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());

View File

@@ -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())
}