diff --git a/auth/Cargo.toml b/auth/Cargo.toml index be380e3..b653122 100644 --- a/auth/Cargo.toml +++ b/auth/Cargo.toml @@ -11,4 +11,5 @@ tokio = { version = "1.0", features = ["macros", "net", "rt-multi-thread"] } tracing = "0.1.40" serde = { version = "1.0.203", features = ["derive"] } chrono = { version = "0.4.24", features = ["serde"] } -dotenvy = "0.15" \ No newline at end of file +dotenvy = "0.15" +sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] } \ No newline at end of file diff --git a/auth/src/main.rs b/auth/src/main.rs index f1269ad..11652bc 100644 --- a/auth/src/main.rs +++ b/auth/src/main.rs @@ -3,6 +3,7 @@ use std::future::Future; use std::net::{IpAddr, Ipv6Addr}; use dotenvy::dotenv; use futures::{future, StreamExt}; +use sqlx::mysql::MySqlPoolOptions; use tarpc::server::{BaseChannel, Channel}; use tarpc::server::incoming::Incoming; use tarpc::tokio_serde::formats::Json; @@ -17,6 +18,28 @@ async fn spawn(fut: impl Future + Send + 'static) { async fn main() -> anyhow::Result<()> { dotenv().ok(); + let db_pool = MySqlPoolOptions::new() + .max_connections(64) + .connect(env::var("DATABASE_URL").expect("DATABASE_URL must be set").as_str()).await?; + + sqlx::query( + "CREATE DATABASE IF NOT EXISTS realmauth; USE realmauth;" + ).fetch_one(&db_pool).await?; + + sqlx::query( + "CREATE TABLE IF NOT EXISTS user ( + id SERIAL, + username VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + login_code INT(6), + tokens TEXT, + google_oauth VARCHAR(255), + apple_oauth VARCHAR(255), + github_oauth VARCHAR(255), + discord_oauth VARCHAR(255) + );" + ).execute(&db_pool).await?; + let server_addr = (IpAddr::V6(Ipv6Addr::LOCALHOST), env::var("PORT").expect("PORT must be set").parse::().unwrap()); // JSON transport is provided by the json_transport tarpc module. It makes it easy diff --git a/auth/src/types.rs b/auth/src/types.rs index a4478c0..943b089 100644 --- a/auth/src/types.rs +++ b/auth/src/types.rs @@ -1,5 +1,35 @@ +use serde::{Deserialize, Serialize}; #[tarpc::service] pub trait RealmAuth { async fn test(name: String) -> String; + async fn server_token_validation(username: String, server_id: String, domain: String, tarpc_port: u16) -> bool; + async fn create_account(username: String, email: String, avatar: String) -> Result; + async fn create_login_flow(username: String) -> ErrorCode; + async fn create_token_from_login(username: String, login_code: u16) -> Result; + + //NOTE: Need to be the user + async fn change_email_flow(token: String) -> ErrorCode; + async fn resolve_email_flow(token: String, login_code: u16, new_email: String) -> ErrorCode; + async fn change_username(token: String, new_username: String) -> ErrorCode; + async fn change_avatar(token: String, avatar: String) -> ErrorCode; + //TODO: + // Create account + // Change email + // Change username + // Change/Upload/Delete avatar + // OAuth login, check against email, store token, take avatar + // Google, Apple, GitHub, Discord + // Get avatar + // Get all userdata if you are the user + // Server token validation +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum ErrorCode { + None, + Error, + EmailTaken, + UsernameTaken, + InvalidLoginCode, } \ No newline at end of file