yggdrasil/src/server/session/has_joined.rs

58 lines
2.1 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 tide::{prelude::*, Request, Result};
use yggdrasil::Database;
use yggdrasil::errors::YggdrasilError;
use yggdrasil::structs::profile::Profile;
use yggdrasil::structs::session::Session;
use yggdrasil::structs::game_profile::GameProfile;
#[derive(Deserialize, Debug)]
struct HasJoinedBody {
pub username: String,
#[serde(rename = "serverId")]
pub server_id: String,
pub ip: Option<String>,
}
pub async fn has_joined(mut req: Request<Database>) -> Result {
let Ok(body) = req.body_json::<HasJoinedBody>().await else {
// No args
return Err(YggdrasilError::new_bad_request("One or more required fields was missing.").into())
};
// Get profile
let Some(profile) = Profile::from_name(req.state(), body.username).await else {
return Err(YggdrasilError::new_bad_request("Profile does not exist.").into())
};
// Get session
let Some(session) = Session::from_profile(req.state(), &profile).await else {
return Err(YggdrasilError::new_bad_request("Session does not exist.").into())
};
// Check IP if requested
if let Some(ip) = body.ip {
if ip != session.ip_addr {
return Err(YggdrasilError::new_forbidden("IP address does not match.").into())
}
}
// Remove session
session.delete(req.state()).await?;
Ok(GameProfile::from_profile(req.state(), &profile).await.into())
}