This commit is contained in:
2022-09-25 05:26:38 +00:00
committed by GitHub
parent bb8078ce6a
commit d9d477f954
5 changed files with 72 additions and 5 deletions

View File

@@ -84,6 +84,21 @@ pub struct PixivDownloaderSqlite {
}
impl PixivDownloaderSqlite {
async fn _add_root_user(
&self,
name: &str,
username: &str,
password: &[u8],
) -> Result<(), PixivDownloaderDbError> {
let mut db = self.db.lock().await;
let tx = db.transaction()?;
tx.execute(
"INSERT OR REPLACE INTO users VALUES (0, ?, ?, ?, true);",
(name, username, password),
)?;
tx.commit()?;
Ok(())
}
/// Check if the database needed create all tables.
async fn _check_database(&self) -> Result<bool, SqliteError> {
let tables = self._get_exists_table().await?;
@@ -241,6 +256,17 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
}
}
#[cfg(feature = "server")]
async fn add_root_user(
&self,
name: &str,
username: &str,
password: &[u8],
) -> Result<User, PixivDownloaderDbError> {
self._add_root_user(name, username, password).await?;
Ok(self.get_user(0).await?.expect("Root user not found:"))
}
#[cfg(feature = "server")]
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError> {
Ok(self._get_user(id).await?)

View File

@@ -13,6 +13,17 @@ pub trait PixivDownloaderDb {
where
Self: Sized + Send + Sync;
#[cfg(feature = "server")]
/// Add root user to database.
/// * `name` - User name
/// * `username` - Unique user name
/// * `password` - Hashed password
async fn add_root_user(
&self,
name: &str,
username: &str,
password: &[u8],
) -> Result<User, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Get a user by ID
/// * `id`: The user's ID
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError>;

View File

@@ -1,5 +1,7 @@
use bytes::BytesMut;
use crate::ext::json::ToJson2;
/// A user in the database
pub struct User {
/// The user ID
@@ -13,3 +15,14 @@ pub struct User {
/// Whether the user is an admin
pub is_admin: bool,
}
impl ToJson2 for User {
fn to_json2(&self) -> json::JsonValue {
json::object! {
"id": self.id,
"name": self.name.as_str(),
"username": self.username.as_str(),
"is_admin": self.is_admin,
}
}
}

View File

@@ -58,10 +58,21 @@ impl AuthUserContext {
let password = key
.decrypt(&password)
.try_err3(7, gettext("Failed to decrypt password with RSA:"))?;
let hashed_password = openssl::sha::sha512(&password);
if root_user.is_none() {
let user = self
.ctx
.db
.add_root_user(name, username, &hashed_password)
.await
.try_err3(8, gettext("Failed to add user to database:"))?;
Ok(user.to_json2())
} else {
Ok(json::object! {})
}
}
None => return Err((5, gettext("No RSA key found.")).into()),
}
Ok(json::object! {})
}
},
None => {

View File

@@ -4,10 +4,7 @@ use crate::ext::json::FromJson;
use crate::server::result::JSONResult;
use bytes::BytesMut;
use hyper::{Body, Request};
use openssl::{
pkey::Public,
rsa::{Padding, Rsa},
};
use openssl::rsa::{Padding, Rsa};
/// Test authentification methods
/// Returns token
@@ -40,5 +37,14 @@ pub async fn test(ctx: &UnitTestContext) -> Result<BytesMut, PixivDownloaderErro
.await?
.unwrap();
let result = JSONResult::from_json(re)?.expect("Failed to add user:");
assert_eq!(
result,
json::object! {
"id": 0,
"name": "test",
"username": "test",
"is_admin": true,
}
);
Ok(BytesMut::new())
}