add edit/delete game capabilities
This commit is contained in:
+67
-1
@@ -2,7 +2,7 @@ use crate::items;
|
||||
use crate::proto_utils::Proto;
|
||||
use rocket::State;
|
||||
use rocket::futures::lock::Mutex;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct AuthState {
|
||||
@@ -24,6 +24,24 @@ impl Default for AuthState {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AdminState {
|
||||
pub admins: Mutex<HashSet<String>>,
|
||||
}
|
||||
|
||||
impl AdminState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
admins: Mutex::new(crate::store::load_admins()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AdminState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Token {
|
||||
@@ -62,6 +80,48 @@ impl<'r> rocket::request::FromRequest<'r> for Token {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AdminToken {
|
||||
pub token: String,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> rocket::request::FromRequest<'r> for AdminToken {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(
|
||||
request: &'r rocket::Request<'_>,
|
||||
) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
let token = request.headers().get_one("Authorization");
|
||||
|
||||
match token {
|
||||
Some(token) => {
|
||||
if let Some(token) = token.strip_prefix("Bearer ") {
|
||||
let auth_state = request.guard::<&State<AuthState>>().await.unwrap();
|
||||
let tokens = auth_state.tokens.lock().await;
|
||||
|
||||
if let Some(username) = tokens.get(token) {
|
||||
let admin_state =
|
||||
request.guard::<&State<crate::AdminState>>().await.unwrap();
|
||||
let admins = admin_state.admins.lock().await;
|
||||
|
||||
if crate::store::is_admin(username, &admins) {
|
||||
return rocket::request::Outcome::Success(AdminToken {
|
||||
token: token.to_string(),
|
||||
username: username.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rocket::request::Outcome::Error((rocket::http::Status::Forbidden, ()))
|
||||
}
|
||||
None => rocket::request::Outcome::Error((rocket::http::Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/login", data = "<request>")]
|
||||
pub async fn login(
|
||||
state: &State<AuthState>,
|
||||
@@ -118,22 +178,28 @@ pub async fn logout(
|
||||
#[post("/get_auth_status", data = "<request>")]
|
||||
pub async fn get_auth_status(
|
||||
state: &State<AuthState>,
|
||||
admin_state: &State<AdminState>,
|
||||
request: Proto<items::AuthStatusRequest>,
|
||||
) -> items::AuthStatusResponse {
|
||||
let req = request.into_inner();
|
||||
let tokens = state.tokens.lock().await;
|
||||
|
||||
if let Some(username) = tokens.get(&req.token) {
|
||||
let admins = admin_state.admins.lock().await;
|
||||
let is_admin = crate::store::is_admin(username, &admins);
|
||||
|
||||
items::AuthStatusResponse {
|
||||
authenticated: true,
|
||||
username: username.clone(),
|
||||
message: "Authenticated".to_string(),
|
||||
is_admin,
|
||||
}
|
||||
} else {
|
||||
items::AuthStatusResponse {
|
||||
authenticated: false,
|
||||
username: "".to_string(),
|
||||
message: "Not authenticated".to_string(),
|
||||
is_admin: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user