From 3fadbbba3d15a2d88b95531e22439b1a9c72c3da Mon Sep 17 00:00:00 2001 From: alemidev Date: Sun, 22 Jan 2023 16:27:03 +0100 Subject: [PATCH] feat: added profile session route --- src/main.rs | 12 ++++++++---- src/proto.rs | 8 ++++++++ src/routes/session.rs | 19 ++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 04f0197..f6c9316 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,10 @@ use tokio::sync::Mutex; use uuid::Uuid; use tracing::info; -use crate::routes::auth::{authenticate, validate, refresh}; -use crate::routes::session::{join, has_joined}; +use crate::routes::{ + auth::{authenticate, validate, refresh}, + session::{join, has_joined, profile}, +}; /// Reimplementation of legacy auth server for minecraft #[derive(Parser, Debug, Clone)] @@ -62,13 +64,15 @@ async fn main() -> Result<(), Box> { let addr = cfg.bind_addr.parse()?; let app = Router::new() + // AUTH .route("/auth/authenticate", post(authenticate)) //in: username, pass; out: token .route("/auth/validate", post(validate)) //in: token; out: boolean valid .route("/auth/refresh", post(refresh)) //in: token out: refreshed token - // .route("/signout", post(signout)) - // .route("/invalidate", post(invalidate)) + // SESSION .route("/session/minecraft/join", post(join)) .route("/session/minecraft/hasJoined", get(has_joined)) + .route("/session/minecraft/profile/:user_id", get(profile)) + // CUSTOM .route("/register", post(register_tmp)) .fallback(fallback_route) .with_state(AppState { store, db, cfg }); diff --git a/src/proto.rs b/src/proto.rs index 2a7417a..0e22214 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -30,6 +30,14 @@ pub struct User { pub properties: Option>, } +/// Damn Mojang! Same as above, except for 'username' -> 'name' +#[derive(Serialize, Deserialize)] +pub struct SessionUser { + pub id: Uuid, + pub name: String, + pub properties: Option>, +} + #[derive(Serialize, Deserialize, Clone)] pub struct Profile { pub name: String, diff --git a/src/routes/session.rs b/src/routes/session.rs index c55d6c7..b84b79c 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -1,9 +1,10 @@ use std::collections::HashMap; -use axum::{extract::{State, Query}, http::StatusCode, Json}; +use axum::{extract::{State, Query, Path}, http::StatusCode, Json}; use chrono::{Duration, Utc}; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use tracing::{info, warn}; +use uuid::Uuid; use crate::{AppState, proto, JoinAttempt, entities}; @@ -71,3 +72,19 @@ pub async fn has_joined(State(state): State, Query(query): Query, Path(user_id): Path) -> Result, StatusCode> { + let user = entities::user::Entity::find().filter( + entities::user::Column::Uuid.eq(user_id) + ).one(&state.db).await.unwrap(); + + if let Some(u) = user { + Ok(Json(proto::SessionUser { + id: u.uuid, + name: u.name, + properties: Some(vec![]), + })) + } else { + Err(StatusCode::NOT_FOUND) + } +}