2023-04-09 17:21:17 +02:00
|
|
|
use crate::config::Config;
|
|
|
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
|
|
|
|
use sqlx::{ConnectOptions, SqlitePool};
|
|
|
|
|
2023-04-10 11:21:53 +02:00
|
|
|
pub const INSERT_KEY: &str = "INSERT INTO key_db (
|
|
|
|
selector,
|
|
|
|
sdid,
|
|
|
|
algorithm,
|
|
|
|
creation,
|
|
|
|
not_after,
|
|
|
|
revocation,
|
|
|
|
published,
|
|
|
|
private_key,
|
|
|
|
public_key
|
|
|
|
) VALUES (
|
|
|
|
$1,
|
|
|
|
$2,
|
|
|
|
$3,
|
|
|
|
$4,
|
|
|
|
$5,
|
|
|
|
$6,
|
|
|
|
FALSE,
|
|
|
|
$7,
|
|
|
|
$8
|
|
|
|
)";
|
2023-04-10 12:35:12 +02:00
|
|
|
pub const SELECT_EXPIRED_KEYS: &str = "SELECT selector, sdid, algorithm, private_key
|
|
|
|
FROM key_db
|
|
|
|
WHERE
|
|
|
|
revocation <= unixepoch()
|
|
|
|
AND published IS FALSE
|
|
|
|
ORDER BY revocation";
|
2023-04-10 11:21:53 +02:00
|
|
|
pub const SELECT_LATEST_KEY: &str = "SELECT not_after
|
|
|
|
FROM key_db
|
|
|
|
WHERE
|
|
|
|
sdid = $1
|
|
|
|
AND algorithm = $2
|
|
|
|
AND published IS FALSE
|
|
|
|
ORDER BY not_after DESC
|
|
|
|
LIMIT 1";
|
2023-04-10 12:35:12 +02:00
|
|
|
pub const SELECT_NEAREST_KEY_PUBLICATION: &str = "SELECT revocation
|
|
|
|
FROM key_db
|
|
|
|
WHERE published IS FALSE
|
|
|
|
ORDER BY revocation
|
|
|
|
LIMIT 1";
|
|
|
|
pub const UPDATE_PUBLISHED_KEY: &str = "UPDATE key_db
|
|
|
|
SET published = TRUE
|
|
|
|
WHERE
|
|
|
|
selector = $1
|
|
|
|
AND sdid = $2
|
|
|
|
AND algorithm = $3";
|
2023-04-10 11:21:53 +02:00
|
|
|
|
2023-04-09 17:21:17 +02:00
|
|
|
pub async fn init(cnf: &Config) -> Result<SqlitePool, String> {
|
|
|
|
do_init(cnf).await.map_err(|e| e.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_init(cnf: &Config) -> Result<SqlitePool, sqlx::Error> {
|
|
|
|
let mut db_options = SqliteConnectOptions::new()
|
|
|
|
.filename(cnf.key_data_base())
|
|
|
|
.create_if_missing(true);
|
|
|
|
db_options.log_statements(log::LevelFilter::Trace);
|
|
|
|
let db_pool = SqlitePoolOptions::new().connect_with(db_options).await?;
|
|
|
|
sqlx::migrate!().run(&db_pool).await?;
|
|
|
|
Ok(db_pool)
|
|
|
|
}
|