yggdrasil/src/util/structs/TexturedObject.rs

77 lines
2.6 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 std::time::UNIX_EPOCH;
use serde::{Deserialize, Serialize};
use json::{object, JsonValue};
use structs::Profile::Profile;
use crate::*;
#[derive(Deserialize, Serialize, Debug)]
pub struct TexturedObject {}
impl TexturedObject {
pub async fn from_profile(db: &Database, profile: &Profile) -> JsonValue {
let mut object = object! {
timestamp: std::time::SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards?!").as_millis() as u64,
profile_id: profile.uuid.to_owned(),
profile_name: profile.name.to_owned(),
textures: object!{}
};
if profile.skin_variant != "NONE" {
let skin_url = profile.get_skin(db).await;
if skin_url.is_some() {
object["textures"]["SKIN"] = object! { url: skin_url };
}
}
if profile.active_cape.is_some() {
let cape_url = profile.get_cape(db).await;
if cape_url.is_some() {
object["textures"]["CAPE"] = object! { url: cape_url };
}
}
object! {
id: profile.uuid.replace("-", ""),
name: profile.name.to_owned(),
properties: [
// TODO: signing textures
// unsigned ? encode : sign
Self::encode_textures(&object)
// Self::sign_textures(object)
]
}
}
pub fn encode_textures(textures: &JsonValue) -> JsonValue {
use base64::{Engine, engine::general_purpose::URL_SAFE as base64};
let serialized = textures.to_string();
let mut encoded = String::new();
base64.encode_string(serialized, &mut encoded);
object! {
name: "textures",
value: encoded
}
}
pub fn sign_textures(textures: &JsonValue) -> JsonValue {
// TODO: signing textures
unimplemented!()
}
}