Add Auth
This commit is contained in:
@@ -1 +0,0 @@
|
||||
target/
|
||||
Generated
+75
@@ -104,6 +104,7 @@ dependencies = [
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bcrypt",
|
||||
"bytes",
|
||||
"prost",
|
||||
"prost-build",
|
||||
@@ -113,6 +114,25 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bcrypt"
|
||||
version = "0.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e65938ed058ef47d92cf8b346cc76ef48984572ade631927e9937b5ffc7662c7"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"blowfish",
|
||||
"getrandom 0.2.16",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "binascii"
|
||||
version = "0.1.4"
|
||||
@@ -125,6 +145,16 @@ version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "blowfish"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e412e2cd0f2b2d93e02543ceae7917b3c70331573df19ee046bcbc35e45e87d7"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"cipher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.19.0"
|
||||
@@ -167,6 +197,16 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "cipher"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"inout",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cmake"
|
||||
version = "0.1.54"
|
||||
@@ -193,6 +233,16 @@ version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cuckoofilter"
|
||||
version = "0.5.0"
|
||||
@@ -423,6 +473,16 @@ dependencies = [
|
||||
"windows",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.16"
|
||||
@@ -618,6 +678,15 @@ version = "0.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb"
|
||||
|
||||
[[package]]
|
||||
name = "inout"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "intrusive-collections"
|
||||
version = "0.9.7"
|
||||
@@ -1894,6 +1963,12 @@ dependencies = [
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
|
||||
|
||||
[[package]]
|
||||
name = "ubyte"
|
||||
version = "0.10.4"
|
||||
|
||||
@@ -10,6 +10,7 @@ rocket = { git = "https://github.com/rwf2/Rocket", rev = "504efef179622df82ba1db
|
||||
bytes = "1"
|
||||
rocket_prost_responder_derive = { path = "rocket_prost_responder_derive" }
|
||||
uuid = { version = "1.10.0", features = ["v4"] }
|
||||
bcrypt = "0.17.1"
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.14.1"
|
||||
|
||||
+19
-16
@@ -1,9 +1,9 @@
|
||||
use crate::items;
|
||||
use crate::proto_utils::Proto;
|
||||
use rocket::State;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use uuid::Uuid;
|
||||
use crate::items;
|
||||
use crate::proto_utils::Proto;
|
||||
|
||||
pub struct AuthState {
|
||||
// Map token -> username
|
||||
@@ -21,26 +21,29 @@ impl AuthState {
|
||||
#[post("/login", data = "<request>")]
|
||||
pub fn login(
|
||||
state: &State<AuthState>,
|
||||
user_list: &State<Vec<crate::User>>,
|
||||
request: Proto<items::LoginRequest>,
|
||||
) -> items::LoginResponse {
|
||||
let req = request.into_inner();
|
||||
// Simple mock authentication: allow any non-empty username/password
|
||||
if !req.username.is_empty() && !req.password.is_empty() {
|
||||
|
||||
if let Some(user) = user_list.iter().find(|u| u.name == req.username)
|
||||
&& bcrypt::verify(&req.password, &user.password_hash).unwrap_or(false)
|
||||
{
|
||||
let token = Uuid::new_v4().to_string();
|
||||
let mut tokens = state.tokens.lock().unwrap();
|
||||
tokens.insert(token.clone(), req.username);
|
||||
|
||||
items::LoginResponse {
|
||||
|
||||
return items::LoginResponse {
|
||||
token,
|
||||
success: true,
|
||||
message: "Login successful".to_string(),
|
||||
}
|
||||
} else {
|
||||
items::LoginResponse {
|
||||
token: "".to_string(),
|
||||
success: false,
|
||||
message: "Invalid credentials".to_string(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
items::LoginResponse {
|
||||
token: "".to_string(),
|
||||
success: false,
|
||||
message: "Invalid credentials".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +54,7 @@ pub fn logout(
|
||||
) -> items::LogoutResponse {
|
||||
let req = request.into_inner();
|
||||
let mut tokens = state.tokens.lock().unwrap();
|
||||
|
||||
|
||||
if tokens.remove(&req.token).is_some() {
|
||||
items::LogoutResponse {
|
||||
success: true,
|
||||
@@ -72,7 +75,7 @@ pub fn get_auth_status(
|
||||
) -> items::AuthStatusResponse {
|
||||
let req = request.into_inner();
|
||||
let tokens = state.tokens.lock().unwrap();
|
||||
|
||||
|
||||
if let Some(username) = tokens.get(&req.token) {
|
||||
items::AuthStatusResponse {
|
||||
authenticated: true,
|
||||
@@ -86,4 +89,4 @@ pub fn get_auth_status(
|
||||
message: "Not authenticated".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
-24
@@ -10,21 +10,38 @@ pub mod items {
|
||||
mod auth;
|
||||
mod proto_utils;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct User {
|
||||
pub person: items::Person,
|
||||
pub password_hash: String,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for User {
|
||||
type Target = items::Person;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.person
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/<name>")]
|
||||
fn get_user(user_list: &rocket::State<Vec<items::Person>>, name: String) -> Option<items::Person> {
|
||||
user_list.iter().find(|user| user.name == name).cloned()
|
||||
fn get_user(user_list: &rocket::State<Vec<User>>, name: String) -> Option<items::Person> {
|
||||
user_list
|
||||
.iter()
|
||||
.find(|user| user.person.name == name)
|
||||
.map(|u| u.person.clone())
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn get_users(user_list: &rocket::State<Vec<items::Person>>) -> items::PersonList {
|
||||
fn get_users(user_list: &rocket::State<Vec<User>>) -> items::PersonList {
|
||||
items::PersonList {
|
||||
person: user_list
|
||||
.inner()
|
||||
.to_vec()
|
||||
.iter_mut()
|
||||
.map(|x| {
|
||||
x.opinion.clear();
|
||||
x.clone()
|
||||
.iter()
|
||||
.map(|u| {
|
||||
let mut p = u.person.clone();
|
||||
p.opinion.clear();
|
||||
p
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
@@ -44,22 +61,25 @@ async fn index_fallback() -> Option<rocket::fs::NamedFile> {
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
let mut user_list: Vec<items::Person> = Vec::new();
|
||||
let mut user_list: Vec<User> = Vec::new();
|
||||
|
||||
user_list.push(items::Person {
|
||||
name: "John".to_string(),
|
||||
opinion: vec![items::Opinion {
|
||||
game: Some(items::Game {
|
||||
title: "Naramo Nuclear Plant V2".to_string(),
|
||||
source: items::Source::Roblox.into(),
|
||||
multiplayer: true,
|
||||
min_players: 1,
|
||||
max_players: 90,
|
||||
price: 0,
|
||||
remote_id: 0,
|
||||
}),
|
||||
would_play: true,
|
||||
}],
|
||||
user_list.push(User {
|
||||
person: items::Person {
|
||||
name: "John".to_string(),
|
||||
opinion: vec![items::Opinion {
|
||||
game: Some(items::Game {
|
||||
title: "Naramo Nuclear Plant V2".to_string(),
|
||||
source: items::Source::Roblox.into(),
|
||||
multiplayer: true,
|
||||
min_players: 1,
|
||||
max_players: 90,
|
||||
price: 0,
|
||||
remote_id: 0,
|
||||
}),
|
||||
would_play: true,
|
||||
}],
|
||||
},
|
||||
password_hash: bcrypt::hash("password123", bcrypt::DEFAULT_COST).unwrap(),
|
||||
});
|
||||
|
||||
rocket::build()
|
||||
@@ -71,5 +91,5 @@ fn rocket() -> _ {
|
||||
routes![auth::login, auth::logout, auth::get_auth_status],
|
||||
)
|
||||
.mount("/", routes![index_fallback])
|
||||
.mount("/", FileServer::new("../frontend/dist"))
|
||||
.mount("/", FileServer::new("frontend/dist"))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use rocket::data::{Data, FromData, Outcome, ToByteUnit};
|
||||
use rocket::http::{Status, ContentType};
|
||||
use rocket::Request;
|
||||
use prost::Message;
|
||||
use rocket::Request;
|
||||
use rocket::data::{Data, FromData, Outcome, ToByteUnit};
|
||||
use rocket::http::{ContentType, Status};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub struct Proto<T>(pub T);
|
||||
@@ -31,8 +31,10 @@ impl<'r, T: Message + Default> FromData<'r> for Proto<T> {
|
||||
type Error = String;
|
||||
|
||||
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r, Self> {
|
||||
if req.content_type() != Some(&ContentType::new("application", "protobuf")) {
|
||||
return Outcome::Forward((data, Status::NotFound));
|
||||
if req.content_type() != Some(&ContentType::new("application", "protobuf"))
|
||||
&& req.content_type() != Some(&ContentType::new("application", "octet-stream"))
|
||||
{
|
||||
return Outcome::Forward((data, Status::NotFound));
|
||||
}
|
||||
|
||||
let limit = req.limits().get("protobuf").unwrap_or(1.mebibytes());
|
||||
@@ -47,4 +49,4 @@ impl<'r, T: Message + Default> FromData<'r> for Proto<T> {
|
||||
Err(e) => Outcome::Error((Status::UnprocessableEntity, e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user