apps: sql + get valid one, get by id, insert, generate app id, generate secret

This commit is contained in:
Philippe Loctaux 2023-03-15 22:00:04 +01:00
parent b5c2be6c9f
commit 71b083895d
19 changed files with 490 additions and 0 deletions

View file

@ -0,0 +1,61 @@
use crate::error::Error;
use crate::App;
use database::sqlx::SqliteExecutor;
use database::Apps as DatabaseApps;
use hash::Secret;
use id::AppID;
use url::Url;
impl From<DatabaseApps> for App {
fn from(db: DatabaseApps) -> Self {
Self {
id: AppID(db.id),
created_at: db.created_at,
updated_at: db.updated_at,
is_archived: db.is_archived,
label: db.label,
redirect_uri: db.redirect_uri,
secret: db.secret,
is_confidential: db.is_confidential,
}
}
}
impl App {
pub async fn insert(
conn: impl SqliteExecutor<'_>,
id: &AppID,
label: &str,
redirect_uri: &Url,
secret: &Secret,
is_confidential: bool,
) -> Result<Option<()>, Error> {
Ok(DatabaseApps::insert(
conn,
id.as_ref(),
label,
redirect_uri.as_str(),
secret.hash(),
is_confidential,
)
.await?)
}
/// App needs to be not archived
pub async fn get_one(
conn: impl SqliteExecutor<'_>,
id: &str,
redirect: &str,
) -> Result<Option<Self>, Error> {
Ok(DatabaseApps::get_one(conn, id, redirect)
.await?
.map(Self::from))
}
pub async fn get_one_by_id(
conn: impl SqliteExecutor<'_>,
id: &str,
) -> Result<Option<Self>, Error> {
Ok(DatabaseApps::get_one_by_id(conn, id).await?.map(Self::from))
}
}

8
crates/apps/src/error.rs Normal file
View file

@ -0,0 +1,8 @@
// error
#[derive(thiserror::Error)]
// the rest
#[derive(Debug)]
pub enum Error {
#[error("Database: {0}")]
Database(#[from] database::Error),
}

31
crates/apps/src/lib.rs Normal file
View file

@ -0,0 +1,31 @@
mod database;
mod error;
use chrono::{DateTime, Utc};
use id::AppID;
pub use crate::error::Error;
#[derive(Debug)]
pub struct App {
id: AppID,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
label: String,
redirect_uri: String,
secret: String,
is_confidential: bool,
is_archived: bool,
}
impl App {
pub fn id(&self) -> &AppID {
&self.id
}
pub fn label(&self) -> &str {
&self.label
}
pub fn redirect_uri(&self) -> &str {
&self.redirect_uri
}
}