Kreu uzanton kun "admin" rajtoj se "savanni" ne ekzistas

This commit is contained in:
Savanni D'Gerinel 2022-02-04 23:36:00 -05:00
parent f4164a62c4
commit 612ad89b70
4 changed files with 56 additions and 6 deletions

View File

@ -1 +0,0 @@
[{"jti":"fa613049-e370-4576-b3f0-b7757c3e0cca","aud":"savanni","exp":1672379098,"iss":"savanni","iat":1640843098,"sub":"admin","perms":["admin"]}]

View File

@ -181,6 +181,8 @@ pub trait AŭtentigoDB: Send {
) -> Option<UzantIdentigilo>;
fn aŭtentigu_per_sesio(&self, ĵetono: &SesiaĴetono) -> Option<(UzantIdentigilo, Uzantnomo)>;
fn uzanto_ekzistas(&self, uzantnomo: Uzantnomo) -> bool;
}
pub struct MemAŭtentigo {
@ -244,6 +246,10 @@ impl AŭtentigoDB for MemAŭtentigo {
let uzantnomo = self.uzantoj.get(&identigilo).cloned()?;
Some((identigilo, uzantnomo))
}
fn uzanto_ekzistas(&self, uzantnomo: Uzantnomo) -> bool {
self.inversa_uzantoj.get(&uzantnomo).is_some()
}
}
pub struct DBAŭtentigo {
@ -386,6 +392,18 @@ impl AŭtentigoDB for DBAŭtentigo {
.optional()
.unwrap()
}
fn uzanto_ekzistas(&self, uzantnomo: Uzantnomo) -> bool {
let konekto = self.pool.konektu().unwrap();
let cnt: usize = konekto
.query_row(
"SELECT count(*) from uzantoj WHERE nomo = ?",
params![String::from(uzantnomo.clone())],
|row| row.get("count(*)"),
)
.unwrap();
cnt > 0
}
}
#[derive(Debug)]
@ -400,6 +418,7 @@ pub fn kun_aŭtentigo(
warp::header("authentication").and_then({
let auth_ctx = auth_ctx.clone();
move |text: SesiaĴetono| {
println!("teksto: {:?}", text);
let auth_ctx = auth_ctx.clone();
async move {
match auth_ctx.read().unwrap().aŭtentigu_per_sesio(&text) {
@ -428,4 +447,15 @@ mod test {
.aŭtentigu_per_pasvorto(&Uzantnomo::from("savanni"), &Pasvorto::from("abcdefg"));
assert_eq!(aŭtentita_identigilo, Some(identigilo));
}
#[test]
fn ĝi_scias_kiel_uzanto_ekzistas() {
let vojo = NamedTempFile::new().unwrap().into_temp_path();
let datumbazo = Datumbazo::kreu(vojo.to_path_buf()).unwrap();
let mut aŭtentigo = DBAŭtentigo::kreu(datumbazo);
assert!(!(aŭtentigo.uzanto_ekzistas(Uzantnomo::from("savanni"))));
let _ = aŭtentigo.kreu_uzanton(Uzantnomo::from("savanni"), Pasvorto::from("abcdefg"));
assert!(aŭtentigo.uzanto_ekzistas(Uzantnomo::from("savanni")));
}
}

View File

@ -6,7 +6,7 @@ use warp::{Filter, Rejection};
#[path = "aŭtentigo.rs"]
mod aŭtentigo;
use aŭtentigo::{kun_aŭtentigo, AŭtentigoDB, AŭtentigoPostulas, Uzantnomo};
use aŭtentigo::{kun_aŭtentigo, AŭtentigoDB, AŭtentigoPostulas, Pasvorto, Uzantnomo};
mod datumbazo;
use datumbazo::Datumbazo;
@ -51,12 +51,26 @@ pub async fn main() {
let db = Datumbazo::kreu(PathBuf::from("../servilo.db")).unwrap();
let auth_ctx = Arc::new(RwLock::new(DBAŭtentigo::kreu(db.clone())));
let rajtigo = Arc::new(RwLock::new(DBRajtigo::kreu(db)));
{
let mut auth_ctx = auth_ctx.write().unwrap();
let mut rajtigo = rajtigo.write().unwrap();
if !auth_ctx.uzanto_ekzistas(Uzantnomo::from("savanni")) {
let uzant_identigilo =
auth_ctx.kreu_uzanton(Uzantnomo::from("savanni"), Pasvorto::from("abcdefg"));
rajtigo.aldonu_rajtojn(
uzant_identigilo,
&mut [Rajto::from("admin")].iter().cloned(),
);
}
}
let ĵetono = auth_ctx
.write()
.unwrap()
.kreu_sesion(&Uzantnomo::from("savanni"));
println!("ĵetono: {}", String::from(ĵetono));
let rajtigo = Arc::new(RwLock::new(DBRajtigo::kreu(db)));
let listigu_ludantojn = {
let auth_ctx = auth_ctx.clone();

View File

@ -1,5 +1,5 @@
use crate::{aŭtentigo::UzantIdentigilo, datumbazo::Datumbazo};
use rusqlite::OptionalExtension;
use rusqlite::{params, OptionalExtension};
use std::collections::{HashMap, HashSet};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -22,7 +22,7 @@ pub trait Rajtigo {
where
F: FnOnce(&mut (dyn Iterator<Item = Rajto>)) -> bool;
fn rajtoj(&self, identigilo: &UzantIdentigilo) -> Vec<Rajto>;
fn aldonu_rajtoj(
fn aldonu_rajtojn(
&mut self,
identigilo: UzantIdentigilo,
rajtoj: &mut (dyn Iterator<Item = Rajto>),
@ -124,11 +124,18 @@ impl Rajtigo for DBRajtigo {
vec![]
}
fn aldonu_rajtoj(
fn aldonu_rajtojn(
&mut self,
identigilo: UzantIdentigilo,
rajtoj: &mut (dyn Iterator<Item = Rajto>),
) {
let konekto = self.pool.konektu().unwrap();
let mut stmt = konekto.prepare("INSERT INTO rajtoj VALUES(?, ?)").unwrap();
rajtoj.for_each(|r| {
stmt.execute(params![String::from(identigilo.clone()), String::from(r)])
.unwrap();
});
}
fn foriru_rajtoj(