diff --git a/src/api/v1/auth/mod.rs b/src/api/v1/auth/mod.rs index c7efc91..4cd06e7 100644 --- a/src/api/v1/auth/mod.rs +++ b/src/api/v1/auth/mod.rs @@ -1,4 +1,8 @@ -use actix_web::{Scope, web}; +use std::{str::FromStr, time::{SystemTime, UNIX_EPOCH}}; + +use actix_web::{web, HttpResponse, Scope}; +use sqlx::Postgres; +use uuid::Uuid; mod register; mod login; @@ -10,3 +14,28 @@ pub fn web() -> Scope { .service(login::response) .service(refresh::res) } + +pub async fn check_access_token(access_token: String, pool: sqlx::Pool) -> Result { + match sqlx::query_as("SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1") + .bind(&access_token) + .fetch_one(&pool) + .await { + Ok(row) => { + let (uuid, created): (String, i64) = row; + + let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64; + + let lifetime = current_time - created; + + if lifetime > 3600 { + return Err(HttpResponse::Unauthorized().finish()) + } + + Ok(Uuid::from_str(&uuid).unwrap()) + }, + Err(error) => { + eprintln!("{}", error); + Err(HttpResponse::InternalServerError().finish()) + } + } +}