WebSockets and tokio working
This commit is contained in:
252
src/usimp/mod.rs
252
src/usimp/mod.rs
@ -1,25 +1,25 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
mod handler;
|
||||
|
||||
use crate::subscription;
|
||||
use crate::database;
|
||||
use crate::error::*;
|
||||
use crypto::digest::Digest;
|
||||
use rand;
|
||||
use rand::Rng;
|
||||
pub use handler::endpoint;
|
||||
|
||||
pub struct Envelope {
|
||||
use serde_json::Value;
|
||||
use crate::error::{Error, ErrorClass, ErrorKind};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct InputEnvelope {
|
||||
pub endpoint: String,
|
||||
pub from_domain: Option<String>,
|
||||
pub to_domain: String,
|
||||
pub token: Option<String>,
|
||||
pub data: serde_json::Value,
|
||||
pub request_nr: Option<u64>,
|
||||
pub data: Value,
|
||||
}
|
||||
|
||||
pub struct Account {
|
||||
id: String,
|
||||
name: String,
|
||||
domain: String,
|
||||
pub struct OutputEnvelope {
|
||||
pub error: Option<Error>,
|
||||
pub request_nr: Option<u64>,
|
||||
pub data: Value,
|
||||
}
|
||||
|
||||
pub struct Session {
|
||||
@ -28,214 +28,22 @@ pub struct Session {
|
||||
account: Option<Account>,
|
||||
}
|
||||
|
||||
pub struct Account {
|
||||
|
||||
}
|
||||
|
||||
impl InputEnvelope {
|
||||
pub fn respond(&self, data: Value) -> OutputEnvelope {
|
||||
OutputEnvelope {
|
||||
error: None,
|
||||
request_nr: self.request_nr,
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn from_token(token: &str) -> Result<Self, Error> {
|
||||
let backend = database::client()?;
|
||||
|
||||
let mut session;
|
||||
match backend {
|
||||
database::Client::Postgres(mut client) => {
|
||||
let res = client.query(
|
||||
"SELECT session_id, session_nr, a.account_id, account_name, domain_id \
|
||||
FROM accounts a JOIN sessions s ON a.account_id = s.account_id \
|
||||
WHERE session_token = $1",
|
||||
&[&token]
|
||||
)?;
|
||||
|
||||
if res.len() == 0 {
|
||||
return Err(Error::new(Kind::InvalidSessionError, Class::ClientError));
|
||||
}
|
||||
|
||||
session = Session {
|
||||
id: res[0].get(0),
|
||||
nr: res[0].get(1),
|
||||
account: Some(Account {
|
||||
id: res[0].get(2),
|
||||
name: res[0].get(3),
|
||||
domain: res[0].get(4),
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Ok(session)
|
||||
pub async fn from_token(token: &str) -> Self {
|
||||
todo!("session")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn endpoint(envelope: Envelope) -> Result<serde_json::Value, Error> {
|
||||
if envelope.from_domain != None {
|
||||
// TODO
|
||||
return Err(Error::new(Kind::NotImplementedError, Class::ServerError));
|
||||
}
|
||||
|
||||
let mut session = None;
|
||||
if let Some(token) = &envelope.token {
|
||||
session = Some(Session::from_token(token)?);
|
||||
}
|
||||
|
||||
let out = match envelope.endpoint.as_str() {
|
||||
"echo" => serde_json::to_value(echo(session, serde_json::from_value(envelope.data)?)?)?,
|
||||
"authenticate" => serde_json::to_value(authenticate(session, serde_json::from_value(envelope.data)?)?)?,
|
||||
"subscribe" => serde_json::to_value(subscribe(session, serde_json::from_value(envelope.data)?)?)?,
|
||||
"send_event" => serde_json::to_value(send_event(session, serde_json::from_value(envelope.data)?)?)?,
|
||||
_ => return Err(Error::new(Kind::InvalidEndpointError, Class::ClientProtocolError)),
|
||||
};
|
||||
|
||||
let mut envelope = serde_json::Value::Object(serde_json::Map::new());
|
||||
envelope["status"] = serde_json::Value::String("success".to_string());
|
||||
envelope["message"] = serde_json::Value::Null;
|
||||
envelope["data"] = out;
|
||||
|
||||
Ok(envelope)
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EchoOutput {
|
||||
message: String,
|
||||
database: Option<i32>,
|
||||
}
|
||||
|
||||
pub fn echo(session: Option<Session>, input: EchoInput) -> Result<EchoOutput, Error> {
|
||||
let backend = database::client()?;
|
||||
let mut output = EchoOutput {
|
||||
message: input.message,
|
||||
database: None,
|
||||
};
|
||||
match backend {
|
||||
database::Client::Postgres(mut client) => {
|
||||
let res = client.query("SELECT * FROM test", &[])?;
|
||||
for row in res {
|
||||
output.database = Some(row.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AuthenticateInput {
|
||||
r#type: String,
|
||||
name: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AuthenticateOutput {
|
||||
token: String,
|
||||
}
|
||||
|
||||
pub fn authenticate(session: Option<Session>, input: AuthenticateInput) -> Result<AuthenticateOutput, Error> {
|
||||
let backend = database::client()?;
|
||||
|
||||
let mut token: String;
|
||||
match backend {
|
||||
database::Client::Postgres(mut client) => {
|
||||
let res = client.query(
|
||||
"SELECT account_id FROM accounts WHERE account_name = $1",
|
||||
&[&input.name]
|
||||
)?;
|
||||
if res.len() == 0 {
|
||||
return Err(Error::new(Kind::AuthenticationError, Class::ClientError));
|
||||
}
|
||||
let account_id: String = res[0].get(0);
|
||||
|
||||
// TODO password check
|
||||
if !input.password.eq("MichaelScott") {
|
||||
return Err(Error::new(Kind::AuthenticationError, Class::ClientError));
|
||||
}
|
||||
|
||||
token = rand::thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
.take(256)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
|
||||
let session_id: String = rand::thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
.take(43)
|
||||
.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],
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(AuthenticateOutput { 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(session: Option<Session>, 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)?;
|
||||
let session = session.unwrap();
|
||||
|
||||
match backend {
|
||||
database::Client::Postgres(mut client) => {
|
||||
|
||||
let res = client.query(
|
||||
"SELECT member_id FROM members \
|
||||
WHERE (room_id, account_id) = ($1, $2)",
|
||||
&[&input.room_id, &session.account.unwrap().id])?;
|
||||
let member_id: String = res[0].get(0);
|
||||
|
||||
client.execute(
|
||||
"INSERT INTO events (event_id, room_id, from_member_id, from_session_id, data) \
|
||||
VALUES ($1, $2, $3, $4, to_jsonb($5::text))",
|
||||
&[&event_id, &input.room_id, &member_id, &session.id, &data])?;
|
||||
}
|
||||
}
|
||||
|
||||
subscription::notify(subscription::Event {
|
||||
data: input.data
|
||||
});
|
||||
|
||||
Ok(SendEventOutput { event_id })
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SubscribeInput {
|
||||
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SubscribeOutput {
|
||||
event: subscription::Event,
|
||||
}
|
||||
|
||||
pub fn subscribe(session: Option<Session>, input: SubscribeInput) -> Result<SubscribeOutput, Error> {
|
||||
let rx = subscription::subscribe();
|
||||
let event = rx.recv().unwrap();
|
||||
subscription::unsubscribe(rx);
|
||||
Ok(SubscribeOutput { event })
|
||||
}
|
||||
|
Reference in New Issue
Block a user