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 @@
drop table if exists apps;

View file

@ -0,0 +1,22 @@
create table if not exists apps
(
id TEXT not null primary key,
created_at TEXT not null default CURRENT_TIMESTAMP,
updated_at TEXT not null default CURRENT_TIMESTAMP,
label TEXT not null,
redirect_uri TEXT not null,
secret TEXT not null,
is_confidential INTEGER not null,
is_archived INTEGER not null default 0
);
-- update "updated_at"
create trigger if not exists apps_updated_at
after update
on apps
for each row
begin
update apps
set updated_at = CURRENT_TIMESTAMP
where id is NEW.id;
end;

View file

@ -0,0 +1,13 @@
select id,
created_at as "created_at: DateTime<Utc>",
updated_at as "updated_at: DateTime<Utc>",
label,
redirect_uri,
secret,
is_confidential as "is_confidential: bool",
is_archived as "is_archived: bool"
from apps
where id is (?)
and redirect_uri is (?)
and is_archived is 0

View file

@ -0,0 +1,11 @@
select id,
created_at as "created_at: DateTime<Utc>",
updated_at as "updated_at: DateTime<Utc>",
label,
redirect_uri,
secret,
is_confidential as "is_confidential: bool",
is_archived as "is_archived: bool"
from apps
where id is (?)

View file

@ -0,0 +1,2 @@
insert into apps (id, label, redirect_uri, secret, is_confidential)
values (?, ?, ?, ?, ?)

View file

@ -510,6 +510,136 @@
},
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n revoked_at as \"revoked_at: DateTime<Utc>\",\n private_der,\n public_der\n\nfrom keys\nwhere revoked_at is null\norder by created_at desc\n"
},
"e22ba816faac0c17ca9f2c31fd1b4a5f13a09cece9ec78e0b6e018950c91facb": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "created_at: DateTime<Utc>",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "updated_at: DateTime<Utc>",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "label",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "redirect_uri",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "secret",
"ordinal": 5,
"type_info": "Text"
},
{
"name": "is_confidential: bool",
"ordinal": 6,
"type_info": "Int64"
},
{
"name": "is_archived: bool",
"ordinal": 7,
"type_info": "Int64"
}
],
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false
],
"parameters": {
"Right": 1
}
},
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n label,\n redirect_uri,\n secret,\n is_confidential as \"is_confidential: bool\",\n is_archived as \"is_archived: bool\"\nfrom apps\n\nwhere id is (?)\n"
},
"eb1a0153c88b0b2744ed1b71df04a91a129a0173fbbc3e2536f52d41e8dc99c4": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "created_at: DateTime<Utc>",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "updated_at: DateTime<Utc>",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "label",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "redirect_uri",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "secret",
"ordinal": 5,
"type_info": "Text"
},
{
"name": "is_confidential: bool",
"ordinal": 6,
"type_info": "Int64"
},
{
"name": "is_archived: bool",
"ordinal": 7,
"type_info": "Int64"
}
],
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false
],
"parameters": {
"Right": 2
}
},
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n label,\n redirect_uri,\n secret,\n is_confidential as \"is_confidential: bool\",\n is_archived as \"is_archived: bool\"\nfrom apps\n\nwhere id is (?)\n and redirect_uri is (?)\n and is_archived is 0\n"
},
"ed27954feb3e21b5c519ccd0312526e68fb3d88a1feb28bdafb414e990da55e8": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 5
}
},
"query": "insert into apps (id, label, redirect_uri, secret, is_confidential)\nvalues (?, ?, ?, ?, ?)\n"
},
"f4edf4567542eaead2e0db14b0d4197c5d3c1bc02da1897b571bf63bfcb4526a": {
"describe": {
"columns": [

View file

@ -1,7 +1,9 @@
mod apps;
mod keys;
mod settings;
mod users;
pub use apps::Apps;
pub use keys::Keys;
pub use settings::Settings;
pub use users::Users;

View file

@ -0,0 +1,62 @@
use crate::error::{handle_error, Error};
use sqlx::sqlite::SqliteQueryResult;
use sqlx::types::chrono::{DateTime, Utc};
use sqlx::{FromRow, SqliteExecutor};
#[derive(FromRow)]
pub struct Apps {
pub id: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub label: String,
pub redirect_uri: String,
pub secret: String,
pub is_confidential: bool,
pub is_archived: bool,
}
impl Apps {
pub async fn insert(
conn: impl SqliteExecutor<'_>,
id: &str,
label: &str,
redirect_uri: &str,
secret: &str,
is_confidential: bool,
) -> Result<Option<()>, Error> {
let query: SqliteQueryResult = sqlx::query_file!(
"queries/apps/insert.sql",
id,
label,
redirect_uri,
secret,
is_confidential
)
.execute(conn)
.await
.map_err(handle_error)?;
Ok((query.rows_affected() == 1).then_some(()))
}
pub async fn get_one(
conn: impl SqliteExecutor<'_>,
id: &str,
redirect: &str,
) -> Result<Option<Self>, Error> {
sqlx::query_file_as!(Self, "queries/apps/get_one.sql", id, redirect)
.fetch_optional(conn)
.await
.map_err(handle_error)
}
pub async fn get_one_by_id(
conn: impl SqliteExecutor<'_>,
id: &str,
) -> Result<Option<Self>, Error> {
sqlx::query_file_as!(Self, "queries/apps/get_one_by_id.sql", id)
.fetch_optional(conn)
.await
.map_err(handle_error)
}
}