style: cargo clippy and format
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
This commit is contained in:
parent
5d0d666094
commit
97f7595cc5
10 changed files with 289 additions and 159 deletions
|
@ -1,13 +1,16 @@
|
|||
use std::{str::FromStr, time::{SystemTime, UNIX_EPOCH}};
|
||||
use std::{
|
||||
str::FromStr,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use actix_web::{web, HttpResponse, Scope};
|
||||
use actix_web::{HttpResponse, Scope, web};
|
||||
use log::error;
|
||||
use sqlx::Postgres;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod register;
|
||||
mod login;
|
||||
mod refresh;
|
||||
mod register;
|
||||
mod revoke;
|
||||
|
||||
pub fn web() -> Scope {
|
||||
|
@ -18,24 +21,33 @@ pub fn web() -> Scope {
|
|||
.service(revoke::res)
|
||||
}
|
||||
|
||||
pub async fn check_access_token<'a>(access_token: String, pool: &'a sqlx::Pool<Postgres>) -> Result<Uuid, HttpResponse> {
|
||||
match sqlx::query_as("SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1")
|
||||
.bind(&access_token)
|
||||
.fetch_one(&*pool)
|
||||
.await {
|
||||
pub async fn check_access_token(
|
||||
access_token: String,
|
||||
pool: &sqlx::Pool<Postgres>,
|
||||
) -> Result<Uuid, HttpResponse> {
|
||||
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 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())
|
||||
return Err(HttpResponse::Unauthorized().finish());
|
||||
}
|
||||
|
||||
|
||||
Ok(Uuid::from_str(&uuid).unwrap())
|
||||
},
|
||||
}
|
||||
Err(error) => {
|
||||
error!("{}", error);
|
||||
Err(HttpResponse::InternalServerError().finish())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue