cargo fmt

This commit is contained in:
2021-06-05 14:29:09 +02:00
parent 473b553662
commit 01e9b9c6ae
11 changed files with 341 additions and 169 deletions

View File

@ -1,10 +1,10 @@
use crate::database;
use crate::usimp;
use crate::usimp::*;
use crate::database;
use serde_json::{Value, from_value, to_value};
use serde::{Serialize, Deserialize};
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::{from_value, to_value, Value};
#[derive(Serialize, Deserialize, Clone)]
struct Input {
@ -19,7 +19,9 @@ struct Output {
}
pub async fn handle(input: &InputEnvelope, session: Option<Session>) -> Result<Value, Error> {
Ok(to_value(authenticate(from_value(input.data.clone())?, session).await?)?)
Ok(to_value(
authenticate(from_value(input.data.clone())?, session).await?,
)?)
}
async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output, Error> {
@ -28,14 +30,20 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
let session_id;
match backend {
database::Client::Postgres(client) => {
let res = client.query(
"SELECT account_id, domain_id \
FROM accounts \
WHERE account_name = $1",
&[&input.name]
).await?;
let res = client
.query(
"SELECT account_id, domain_id \
FROM accounts \
WHERE account_name = $1",
&[&input.name],
)
.await?;
if res.len() == 0 {
return Err(Error::new(ErrorKind::AuthenticationError, ErrorClass::ClientError, None));
return Err(Error::new(
ErrorKind::AuthenticationError,
ErrorClass::ClientError,
None,
));
}
let row = &res[0];
let account_id: String = row.get(0);
@ -43,7 +51,11 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
// TODO password check
if !input.password.eq("MichaelScott") {
return Err(Error::new(ErrorKind::AuthenticationError, ErrorClass::ClientError, None));
return Err(Error::new(
ErrorKind::AuthenticationError,
ErrorClass::ClientError,
None,
));
}
session_id = usimp::get_id(&[domain_id.as_str(), account_id.as_str()]);
@ -53,17 +65,16 @@ async fn authenticate(input: Input, _session: Option<Session>) -> Result<Output,
.map(char::from)
.collect();
client.execute(
"INSERT INTO sessions (account_id, session_nr, session_id, session_token) \
VALUES ($1, COALESCE((SELECT MAX(session_nr) + 1 \
FROM sessions \
WHERE account_id = $1), 1), $2, $3);",
&[&account_id, &session_id, &token],
).await?;
client
.execute(
"INSERT INTO sessions (account_id, session_nr, session_id, session_token) \
VALUES ($1, COALESCE((SELECT MAX(session_nr) + 1 \
FROM sessions \
WHERE account_id = $1), 1), $2, $3);",
&[&account_id, &session_id, &token],
)
.await?;
}
}
Ok(Output {
session_id,
token,
})
Ok(Output { session_id, token })
}