yggdrasil/src/util/structs/Session.rs

58 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 serde::{Deserialize, Serialize};
use structs::Profile::Profile;
use crate::*;
#[derive(Deserialize, Serialize, Debug)]
pub struct Session {
pub id: i64,
pub profile: Profile,
pub server_id: String,
pub ip_addr: String,
}
impl Session {
pub async fn from_id(db: &Database, id: i64) -> Option<Self> {
let record = sqlx::query_as!(RawSession, "SELECT * FROM sessions WHERE id = $1", id)
.fetch_one(&db.pool)
.await;
match record {
Ok(r) => Some(r.complete(db).await),
Err(_) => None,
}
}
}
#[derive(Deserialize, Serialize, Debug)]
pub struct RawSession {
pub id: i64,
pub profile: i64,
pub server_id: String,
pub ip_addr: String
}
impl RawSession {
pub async fn complete(self, db: &Database) -> Session {
Session {
id: self.id,
profile: Profile::from_id(db, self.profile).await.expect("Couldn't resolve session profile"),
server_id: self.server_id,
ip_addr: self.ip_addr,
}
}
}