Defined what auth server should do

This commit is contained in:
2024-06-30 19:34:28 -04:00
Unverified
parent eb261b2865
commit 0894a10021
3 changed files with 55 additions and 1 deletions

View File

@@ -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<Output = ()> + 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::<u16>().unwrap());
// JSON transport is provided by the json_transport tarpc module. It makes it easy

View File

@@ -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<String, ErrorCode>;
async fn create_login_flow(username: String) -> ErrorCode;
async fn create_token_from_login(username: String, login_code: u16) -> Result<String, ErrorCode>;
//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,
}