Update crates and change to UUID

This commit is contained in:
2022-08-18 21:24:16 +02:00
parent 9227cdef9a
commit f2cdbe0638
11 changed files with 1835 additions and 144 deletions

View File

@ -5,6 +5,7 @@ use crate::usimp::*;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::{from_value, to_value, Value};
use uuid::Uuid;
#[derive(Serialize, Deserialize, Clone)]
struct Input {
@ -26,14 +27,14 @@ pub async fn handle(input: &InputEnvelope, session: Option<Session>) -> Result<V
async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output, Error> {
let backend = database::client().await?;
let token;
let token: String;
let session_id;
match backend {
database::Client::Postgres(client) => {
let res = client
.query(
"SELECT account_id, domain_id \
FROM accounts \
FROM account \
WHERE account_name = $1",
&[&input.name],
)
@ -46,8 +47,8 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
));
}
let row = &res[0];
let account_id: String = row.get(0);
let domain_id: String = row.get(1);
let account_id: Uuid = row.get(0);
let domain_id: Uuid = row.get(1);
// TODO password check
if !input.password.eq("MichaelScott") {
@ -58,7 +59,7 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
));
}
session_id = usimp::get_id(&[domain_id.as_str(), account_id.as_str()]);
session_id = Uuid::new_v4();
token = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(256)
@ -67,14 +68,14 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
client
.execute(
"INSERT INTO sessions (account_id, session_nr, session_id, session_token) \
"INSERT INTO session (account_id, session_nr, session_id, session_token) \
VALUES ($1, COALESCE((SELECT MAX(session_nr) + 1 \
FROM sessions \
FROM session \
WHERE account_id = $1), 1), $2, $3);",
&[&account_id, &session_id, &token],
)
.await?;
}
}
Ok(Output { session_id, token })
Ok(Output { session_id: session_id.to_string(), token })
}