mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
implement existing certificate selection
This commit is contained in:
parent
10cf4fc6a1
commit
728a9c06d5
9 changed files with 189 additions and 41 deletions
|
|
@ -18,6 +18,7 @@ pub struct Gemini {
|
|||
pub auth: Rc<Auth>,
|
||||
pub database: Rc<Database>,
|
||||
pub memory: Rc<Memory>,
|
||||
profile_identity_id: Rc<i64>,
|
||||
}
|
||||
|
||||
impl Gemini {
|
||||
|
|
@ -33,7 +34,7 @@ impl Gemini {
|
|||
Ok(auth) => Rc::new(auth),
|
||||
Err(_) => return Err(Error::AuthInit), // @TODO
|
||||
};
|
||||
let database = Rc::new(Database::new(connection, profile_identity_id));
|
||||
let database = Rc::new(Database::new(connection, profile_identity_id.clone()));
|
||||
let memory = Rc::new(Memory::new());
|
||||
|
||||
// Init `Self`
|
||||
|
|
@ -41,6 +42,7 @@ impl Gemini {
|
|||
auth,
|
||||
database,
|
||||
memory,
|
||||
profile_identity_id,
|
||||
};
|
||||
|
||||
// Build initial index
|
||||
|
|
@ -69,8 +71,6 @@ impl Gemini {
|
|||
};
|
||||
Ok(()) // @TODO
|
||||
}
|
||||
|
||||
// @TODO create new identity API
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
//! Controller for children `database` and `memory` components
|
||||
|
||||
mod database;
|
||||
mod error;
|
||||
mod memory;
|
||||
|
|
@ -35,6 +37,45 @@ impl Auth {
|
|||
|
||||
// Actions
|
||||
|
||||
/// Apply `profile_identity_gemini_id` certificate as the auth for `url`
|
||||
/// * deactivate active auth by remove previous records from `Self` database
|
||||
/// * reindex `Self` memory index on success
|
||||
/// * return last insert `profile_identity_gemini_auth_id` on success
|
||||
pub fn apply(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
|
||||
// Get all records match request
|
||||
match self.database.records(Some(url)) {
|
||||
Ok(records) => {
|
||||
// Cleanup records match `profile_identity_gemini_id` (unauth)
|
||||
for record in records {
|
||||
if record.profile_identity_gemini_id == profile_identity_gemini_id {
|
||||
if self.database.delete(record.id).is_err() {
|
||||
return Err(Error::DatabaseRecordDelete(record.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create new record (auth)
|
||||
let profile_identity_gemini_auth_id =
|
||||
match self.database.add(profile_identity_gemini_id, url) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return Err(Error::DatabaseRecordCreate(
|
||||
profile_identity_gemini_id,
|
||||
url.to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
// Reindex
|
||||
self.index()?;
|
||||
|
||||
// Done
|
||||
Ok(profile_identity_gemini_auth_id)
|
||||
}
|
||||
Err(_) => return Err(Error::DatabaseRecordsRead(url.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create new `Memory` index from `Database` for `Self`
|
||||
pub fn index(&self) -> Result<(), Error> {
|
||||
// Clear previous records
|
||||
|
|
@ -44,14 +85,12 @@ impl Auth {
|
|||
match self.database.records(None) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
if record.is_active {
|
||||
if self
|
||||
.memory
|
||||
.add(record.url, record.profile_identity_gemini_id)
|
||||
.is_err()
|
||||
{
|
||||
return Err(Error::MemoryIndex);
|
||||
}
|
||||
if self
|
||||
.memory
|
||||
.add(record.url, record.profile_identity_gemini_id)
|
||||
.is_err()
|
||||
{
|
||||
return Err(Error::MemoryIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@ use sqlite::{Connection, Error, Transaction};
|
|||
use std::{rc::Rc, sync::RwLock};
|
||||
|
||||
pub struct Table {
|
||||
//pub id: i64,
|
||||
pub id: i64,
|
||||
pub profile_identity_gemini_id: i64,
|
||||
pub is_active: bool,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
|
|
@ -21,6 +20,43 @@ impl Database {
|
|||
Self { connection }
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Create new record in database
|
||||
pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
|
||||
// Begin new transaction
|
||||
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||
let tx = writable.transaction()?;
|
||||
|
||||
// Create new record
|
||||
insert(&tx, profile_identity_gemini_id, url)?;
|
||||
|
||||
// Hold insert ID for result
|
||||
let id = last_insert_id(&tx);
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(id),
|
||||
Err(reason) => Err(reason),
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete record with given `id` from database
|
||||
pub fn delete(&self, id: i64) -> Result<(), Error> {
|
||||
// Begin new transaction
|
||||
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||
let tx = writable.transaction()?;
|
||||
|
||||
// Create new record
|
||||
delete(&tx, id)?;
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(reason) => Err(reason),
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get records from database match current `profile_id` optionally filtered by `url`
|
||||
|
|
@ -39,14 +75,12 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`profile_identity_gemini_id` INTEGER NOT NULL,
|
||||
`is_active` INTEGER NOT NULL,
|
||||
`url` VARCHAR(1024) NOT NULL,
|
||||
|
||||
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
|
||||
|
||||
UNIQUE (
|
||||
`profile_identity_gemini_id`,
|
||||
`is_active`,
|
||||
`url`
|
||||
)
|
||||
)",
|
||||
|
|
@ -54,11 +88,31 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn insert(
|
||||
tx: &Transaction,
|
||||
profile_identity_gemini_id: i64,
|
||||
url: &str,
|
||||
) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_identity_gemini_auth` (
|
||||
`profile_identity_gemini_id`,
|
||||
`url`
|
||||
) VALUES (?, ?)",
|
||||
(profile_identity_gemini_id, url),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"DELETE FROM `profile_identity_gemini_auth` WHERE `id` = ?",
|
||||
[id],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
`profile_identity_gemini_id`,
|
||||
`is_active`,
|
||||
`url`
|
||||
|
||||
FROM `profile_identity_gemini_auth`
|
||||
|
|
@ -67,10 +121,9 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
|
|||
|
||||
let result = stmt.query_map([url.unwrap_or("%")], |row| {
|
||||
Ok(Table {
|
||||
//id: row.get(0)?,
|
||||
id: row.get(0)?,
|
||||
profile_identity_gemini_id: row.get(1)?,
|
||||
is_active: row.get(2)?,
|
||||
url: row.get(3)?,
|
||||
url: row.get(2)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
|
|
@ -83,3 +136,7 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
|
|||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
DatabaseIndex,
|
||||
DatabaseRecordCreate(i64, String),
|
||||
DatabaseRecordDelete(i64),
|
||||
DatabaseRecordsRead(String),
|
||||
MemoryIndex,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,27 @@ impl Database {
|
|||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Create new record in database
|
||||
pub fn add(&self, pem: &str, name: Option<&str>) -> Result<i64, Error> {
|
||||
// Begin new transaction
|
||||
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||
let tx = writable.transaction()?;
|
||||
|
||||
// Create new record
|
||||
insert(&tx, *self.profile_identity_id, pem, name)?;
|
||||
|
||||
// Hold insert ID for result
|
||||
let id = last_insert_id(&tx);
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(id),
|
||||
Err(reason) => Err(reason),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get all records match current `profile_identity_id`
|
||||
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
||||
let readable = self.connection.read().unwrap(); // @TODO
|
||||
|
|
@ -56,6 +77,22 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn insert(
|
||||
tx: &Transaction,
|
||||
profile_identity_id: i64,
|
||||
pem: &str,
|
||||
name: Option<&str>,
|
||||
) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_identity_gemini` (
|
||||
`profile_identity_id`,
|
||||
`pem`,
|
||||
`name`
|
||||
) VALUES (?, ?, ?)",
|
||||
(profile_identity_id, pem, name),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn select(tx: &Transaction, profile_identity_id: i64) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
|
|
@ -84,3 +121,7 @@ pub fn select(tx: &Transaction, profile_identity_id: i64) -> Result<Vec<Table>,
|
|||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue