[WIP] authenticate working

This commit is contained in:
2021-05-22 20:24:44 +02:00
parent faa8c8e0a5
commit baec902243
7 changed files with 144 additions and 9 deletions

View File

@ -3,14 +3,38 @@ use serde_json;
use crate::database;
use crate::error::*;
use crypto::digest::Digest;
pub fn endpoint(endpoint: &str, input: serde_json::Value) -> Result<serde_json::Value, Error> {
match endpoint {
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(input)?)?)?),
_ => Err(Error::new(Kind::InvalidEndpointError, Class::ClientError)),
pub struct Envelope {
pub endpoint: String,
pub from_domain: String,
pub to_domain: String,
pub authorization: Option<String>,
pub data: serde_json::Value,
}
pub fn endpoint(envelope: Envelope) -> Result<serde_json::Value, Error> {
// TODO check authorization
// TODO domain_check
match envelope.endpoint.as_str() {
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(envelope.data)?)?)?),
"authorize" => Ok(serde_json::to_value(authorize(serde_json::from_value(envelope.data)?)?)?),
"notify" => Ok(serde_json::to_value(notify(serde_json::from_value(envelope.data)?)?)?),
_ => return Err(Error::new(Kind::InvalidEndpointError, Class::ClientError)),
}
}
pub fn get_id(input: &str) -> String {
let mut hasher = crypto::sha2::Sha256::new();
hasher.input_str(chrono::Utc::now().timestamp_millis().to_string().as_str());
hasher.input_str(" ");
hasher.input_str(input);
let mut result = [0u8; 32];
hasher.result(&mut result);
base64_url::encode(&result)
}
#[derive(Serialize, Deserialize)]
pub struct EchoInput {
message: String,
@ -38,3 +62,71 @@ pub fn echo(input: EchoInput) -> Result<EchoOutput, Error> {
}
Ok(output)
}
#[derive(Serialize, Deserialize)]
pub struct AuthorizeInput {
r#type: String,
name: String,
password: String,
}
#[derive(Serialize, Deserialize)]
pub struct AuthorizeOutput {
token: String,
}
pub fn authorize(input: AuthorizeInput) -> Result<AuthorizeOutput, Error> {
let backend = database::client()?;
let mut token;
match backend {
database::Client::Postgres(mut client) => {
let res = client.query("SELECT account_id FROM accounts WHERE name = $1", &[&input.name])?;
if res.len() == 0 {
return Err(Error::new(Kind::AuthenticationError, Class::ClientError));
}
token = res.get(0).unwrap().get(0);
}
}
Ok(AuthorizeOutput { token })
}
#[derive(Serialize, Deserialize)]
pub struct SendEventInput {
room_id: String,
data: serde_json::Value,
}
#[derive(Serialize, Deserialize)]
pub struct SendEventOutput {
event_id: String,
}
pub fn send_event(input: SendEventInput) -> Result<SendEventOutput, Error> {
let backend = database::client()?;
let event_id = get_id("hermann"); // TODO fix id generation
let data = serde_json::to_string(&input.data)?;
match backend {
database::Client::Postgres(mut client) => {
client.execute("INSERT INTO events (event_id, room_id, data) VALUES ($1, $2, $3)", &[&event_id, &input.room_id, &data])?;
}
}
Ok(SendEventOutput { event_id })
}
#[derive(Serialize, Deserialize)]
pub struct NotifyInput {
}
#[derive(Serialize, Deserialize)]
pub struct NotifyOutput {
}
pub fn notify(input: NotifyInput) -> Result<NotifyOutput, Error> {
Ok(NotifyOutput {})
}