yggdrasil/src/server/auth/signout.rs

48 lines
1.8 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::account::Account;
use yggdrasil::structs::token::Token;
#[derive(Deserialize, Debug)]
struct SignoutBody {
pub username: String,
pub password: String
}
pub async fn signout(mut req: Request<Database>) -> Result {
let Ok(body) = req.body_json::<SignoutBody>().await else {
// No credentials
return Err(YggdrasilError::new_bad_request("Credentials can not be null.").into())
};
// Get account
let Some(account) = Account::from_email(req.state(), body.username).await else {
// Account doesn't exist
return Err(YggdrasilError::new_unauthorized("Invalid credentials. Invalid username or password.").into())
};
// Verify password
if !bcrypt::verify(body.password, &account.password_hash)? {
// Password incorrect
return Err(YggdrasilError::new_unauthorized("Invalid credentials. Invalid username or password.").into());
}
// Delete all tokens
Token::delete_all_from(req.state(), account).await?;
Ok("".into())
}