From c0f5908089d0852ca63a9d4fc65a4a034339cac9 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 22 Dec 2020 21:30:35 -0800 Subject: [PATCH] Tiny auth module for literally just redis and jwt things Also we're doing a really budget version of sessions here so we might change things later but this is fine for now --- server-api/db/src/auth.rs | 28 ++++++++++++++++++++++++++++ server-api/db/src/lib.rs | 3 +++ 2 files changed, 31 insertions(+) create mode 100644 server-api/db/src/auth.rs diff --git a/server-api/db/src/auth.rs b/server-api/db/src/auth.rs new file mode 100644 index 0000000..aeba8ec --- /dev/null +++ b/server-api/db/src/auth.rs @@ -0,0 +1,28 @@ +use crate::UBigInt; + +use redis::{ + Client, AsyncCommands, + RedisResult +}; + +lazy_static! { + static ref REDIS_URL: String = { + std::env::var("REDIS_URL").unwrap() + }; +} + +pub async fn add_jwt(id: UBigInt, token: &str) -> RedisResult<()> { + let client = Client::open(REDIS_URL.as_ref())?; + let mut conn = client.get_tokio_connection().await?; + + let _ = conn.set(id, token).await?; + Ok(()) +} + +pub async fn active_jwt(id: UBigInt, token: &str) -> RedisResult { + let client = Client::open(REDIS_URL.as_ref())?; + let mut conn = client.get_tokio_connection().await?; + + let val: String = conn.get(id).await?; + Ok(val == token) +} \ No newline at end of file diff --git a/server-api/db/src/lib.rs b/server-api/db/src/lib.rs index 914b576..37310e4 100644 --- a/server-api/db/src/lib.rs +++ b/server-api/db/src/lib.rs @@ -1,9 +1,12 @@ +#[macro_use] extern crate lazy_static; + extern crate serde; pub mod member; pub mod common; pub mod invites; pub mod channels; pub mod messages; +pub mod auth; use std::vec::Vec;