mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add basic auth support to get Cardlife working in non-offline mode
This commit is contained in:
12
auth/Cargo.toml
Normal file
12
auth/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "auth"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rocket.workspace = true
|
||||
libfj.workspace = true
|
||||
log.workspace = true
|
||||
env_logger.workspace = true
|
||||
uuid = { version = "1.12", features = [ "v4", "fast-rng" ] }
|
||||
rocket-client-addr = "0.5"
|
||||
3
auth/run_debug.sh
Executable file
3
auth/run_debug.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
RUST_LOG=debug cargo run
|
||||
12
auth/src/cardlife/email.rs
Normal file
12
auth/src/cardlife/email.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use rocket::{post, routes, serde::json::Json};
|
||||
|
||||
#[post("/api/auth/authenticate", data = "<body>")]
|
||||
pub fn email_password_auth(body: Json<libfj::cardlife::AuthenticationPayload>) -> Json<libfj::cardlife::AuthenticationInfo> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("CardLife Email/Password", |rocket| async {
|
||||
rocket.mount("/", routes![email_password_auth])
|
||||
})
|
||||
}
|
||||
15
auth/src/cardlife/mod.rs
Normal file
15
auth/src/cardlife/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
#[allow(unused_variables)]
|
||||
mod email;
|
||||
mod steam;
|
||||
mod temporary_get_user_id;
|
||||
#[allow(unused_variables)]
|
||||
mod token;
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("JSON", |rocket| async {
|
||||
rocket.attach(email::stage())
|
||||
.attach(steam::stage())
|
||||
.attach(temporary_get_user_id::stage())
|
||||
.attach(token::stage())
|
||||
})
|
||||
}
|
||||
58
auth/src/cardlife/steam.rs
Normal file
58
auth/src/cardlife/steam.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use std::sync::RwLock;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::{post, routes, serde::json::Json};
|
||||
|
||||
// FIXME don't have global state
|
||||
static AUTH_MAP: RwLock<Option<HashMap<String, libfj::cardlife::AuthenticationInfo>>> = RwLock::new(None);
|
||||
|
||||
fn generate_auth(ticket: &str, addr: &str) -> libfj::cardlife::AuthenticationInfo {
|
||||
let public_guid = uuid::Uuid::new_v4().to_string();
|
||||
log::warn!("assigning GUID {} to IP address {} (steam ticket {})", public_guid, addr, ticket);
|
||||
libfj::cardlife::AuthenticationInfo {
|
||||
public_id: public_guid,
|
||||
email_address: "nobody@openjamgames.com".to_string(),
|
||||
display_name: "gaben".to_string(),
|
||||
purchases: vec![1, 2, 3],
|
||||
flags: vec![],
|
||||
/*flags: //vec![
|
||||
"Dev".to_string(),
|
||||
"GiveInv".to_string(),
|
||||
"NoDrop".to_string(),
|
||||
"DekStruct".to_string(),
|
||||
"BucketA".to_string(),
|
||||
"BucketB".to_string(),
|
||||
"userlogged".to_string(),
|
||||
],*/
|
||||
confirmed: true,
|
||||
token: "1234567890-qwertyuiop-asdfghjkl-zxcvbnm".to_string(),
|
||||
steam_id: Some("gaben".to_string()),
|
||||
id: 123456, // FIXME
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/api/auth/steamauthenticate", data = "<body>")]
|
||||
pub fn steam_auth(addr: &rocket_client_addr::ClientAddr, body: Json<libfj::cardlife::SteamAuthenticationPayload>) -> Json<libfj::cardlife::AuthenticationInfo> {
|
||||
let addr_str = addr.get_ipv4_string().unwrap_or_else(|| addr.get_ipv6_string());
|
||||
if AUTH_MAP.read().unwrap().is_none() {
|
||||
let new_info = generate_auth(&body.steam_ticket, &addr_str);
|
||||
let mut new_map = HashMap::new();
|
||||
new_map.insert(addr_str, new_info.clone());
|
||||
*AUTH_MAP.write().unwrap() = Some(new_map);
|
||||
Json(new_info)
|
||||
} else {
|
||||
if let Some(auth_info) = AUTH_MAP.read().unwrap().as_ref().unwrap().get(&addr_str).map(|x| x.to_owned()) {
|
||||
Json(auth_info)
|
||||
} else {
|
||||
let new_info = generate_auth(&body.steam_ticket, &addr_str);
|
||||
AUTH_MAP.write().unwrap().as_mut().unwrap().insert(addr_str, new_info.clone());
|
||||
Json(new_info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("CardLife Steam", |rocket| async {
|
||||
rocket.mount("/", routes![steam_auth])
|
||||
})
|
||||
}
|
||||
17
auth/src/cardlife/temporary_get_user_id.rs
Normal file
17
auth/src/cardlife/temporary_get_user_id.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
//! Not a temporary file, this is the name of the endpoint
|
||||
use rocket::{post, routes, serde::json::Json};
|
||||
|
||||
#[post("/api/auth/temporarygetuserid", data = "<body>")]
|
||||
pub fn temp_migration(body: Json<libfj::cardlife::TempGetUserIdPayload>) -> Json<libfj::cardlife::TempGetUserIdResponse> {
|
||||
Json(libfj::cardlife::TempGetUserIdResponse {
|
||||
public_id: body.public_id.to_owned(),
|
||||
token: body.token.to_owned(),
|
||||
user_id: 123456, // FIXME
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("CardLife Temp UserId", |rocket| async {
|
||||
rocket.mount("/", routes![temp_migration])
|
||||
})
|
||||
}
|
||||
12
auth/src/cardlife/token.rs
Normal file
12
auth/src/cardlife/token.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use rocket::{post, routes, serde::json::Json};
|
||||
|
||||
#[post("/api/auth/token", data = "<body>")]
|
||||
pub fn token_auth(body: Json<libfj::cardlife::TokenPayload>) -> Json<libfj::cardlife::AuthenticationInfo> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("CardLife Token", |rocket| async {
|
||||
rocket.mount("/", routes![token_auth])
|
||||
})
|
||||
}
|
||||
14
auth/src/main.rs
Normal file
14
auth/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
mod cardlife;
|
||||
|
||||
#[rocket::get("/")]
|
||||
fn index() -> &'static str {
|
||||
"Hello, world!"
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
fn rocket() -> _ {
|
||||
env_logger::init();
|
||||
rocket::build().mount("/", rocket::routes![index])
|
||||
.attach(cardlife::stage())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user