yggdrasil/src/server/profile/profile.rs

68 lines
2.4 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use anyhow::anyhow;
use log::{debug, info};
use serde_json::Value;
use tide::{prelude::*, Request, Result};
use yggdrasil::Database;
use yggdrasil::errors::YggdrasilError;
use yggdrasil::structs::game_profile::GameProfile;
use yggdrasil::structs::profile::Profile;
use yggdrasil::structs::token::Token;
pub async fn profile(mut req: Request<Database>) -> Result {
// TODO: unsigned?
let Ok(uuid) = req.param("uuid") else {
// No uuid
debug!("No uuid");
return Err(YggdrasilError::new_bad_request("One or more required fields was missing.").into())
};
// Re-hyphenate if needed
let uuid = match uuid.find("-") {
None => Token::rehyphenate(uuid.to_string()),
Some(_) => uuid.to_string(),
};
let Some(profile) = Profile::from_uuid(req.state(), uuid).await else {
return Err(YggdrasilError::new_bad_request("Profile does not exist").into())
};
Ok(GameProfile::from_profile(req.state(), &profile).await.into())
}
pub async fn profiles(mut req: Request<Database>) -> Result {
let Ok(body) = req.body_json::<Vec<String>>().await else {
return Err(YggdrasilError::new_bad_request("One or more required fields was missing.").into())
};
let mut response: Vec<Value> = Vec::new();
for uuid in body {
let profile = Profile::from_uuid(req.state(), uuid).await;
match profile {
None => continue,
Some(p) => {
response.push(GameProfile::from_profile(req.state(), &p).await)
}
}
}
Ok(serde_json::to_string(&response)?.into())
}
pub async fn name_history(req: Request<Database>) -> Result {
Err(tide::Error::new(501, anyhow!("Not implemented yet")).into())
}