Compare commits
4 Commits
c9c30b7720
...
03be1055d5
Author | SHA1 | Date |
---|---|---|
![]() |
03be1055d5 | |
![]() |
d875abc27e | |
![]() |
89868c6e16 | |
![]() |
628874f4e9 |
|
@ -8,3 +8,9 @@ edition = "2021"
|
|||
[dependencies]
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
base64 = "0.21.5"
|
||||
crypto = { version = "0.5.1", features = ["digest"] }
|
||||
sha1 = "0.10.6"
|
||||
num-bigint = "0.4.5"
|
||||
|
|
46
README.md
46
README.md
|
@ -1,2 +1,48 @@
|
|||
# Purple Cello Mojang API Interface
|
||||
Purple Cello interface for the Mojang API in rust
|
||||
|
||||
## Implemented endpoints
|
||||
|
||||
### Accounts
|
||||
- [x] [Username to UUID](https://wiki.vg/Mojang_API#Username_to_UUID)
|
||||
- [ ] [Usernames to UUIDs](https://wiki.vg/Mojang_API#Usernames_to_UUIDs)
|
||||
- [ ] [~~UUID to Name History~~](https://wiki.vg/Mojang_API#UUID_to_Name_History_.28Removed.29)
|
||||
- [x] [UUID to Profile and Skin/Cape](https://wiki.vg/Mojang_API#UUID_to_Profile_and_Skin.2FCape)
|
||||
- [ ] [Profile Information](https://wiki.vg/Mojang_API#Profile_Information)
|
||||
- [ ] [Player Attributes](https://wiki.vg/Mojang_API#Player_Attributes)
|
||||
- [ ] [Player Blocklist](https://wiki.vg/Mojang_API#Player_Blocklist)
|
||||
- [ ] [Player Certificates](https://wiki.vg/Mojang_API#Player_Certificates)
|
||||
- [ ] [Profile Name Change Information](https://wiki.vg/Mojang_API#Profile_Name_Change_Information)
|
||||
- [ ] [Name Availability](https://wiki.vg/Mojang_API#Name_Availability)
|
||||
- [ ] [Change Name](https://wiki.vg/Mojang_API#Change_Name)
|
||||
- [ ] [Change Skin](https://wiki.vg/Mojang_API#Change_Skin)
|
||||
- [ ] [Upload Skin](https://wiki.vg/Mojang_API#Upload_Skin)
|
||||
- [ ] [Reset Skin](https://wiki.vg/Mojang_API#Reset_Skin)
|
||||
- [ ] [Hide Cape](https://wiki.vg/Mojang_API#Hide_Cape)
|
||||
- [ ] [Show Cape](https://wiki.vg/Mojang_API#Show_Cape)
|
||||
|
||||
### Mojang
|
||||
- [ ] [~~API Status~~](https://wiki.vg/Mojang_API#API_Status_.28Removed.29)
|
||||
- [ ] [~~Statistics~~](https://wiki.vg/Mojang_API#Statistics)
|
||||
- [ ] [Blocked Servers](https://wiki.vg/Mojang_API#Blocked_Servers)
|
||||
- [ ] [Check Product Voucher](https://wiki.vg/Mojang_API#Check_Product_Voucher)
|
||||
- [ ] [Verify Security Location](https://wiki.vg/Mojang_API#Verify_Security_Location)
|
||||
- [ ] [Get Security Questions](https://wiki.vg/Mojang_API#Get_Security_Questions)
|
||||
- [ ] [Send Security Answers](https://wiki.vg/Mojang_API#Send_Security_Answers)
|
||||
|
||||
### Account Migration
|
||||
- [ ] [Get Account Migration Information](https://wiki.vg/Mojang_API#Get_Account_Migration_Information)
|
||||
- [ ] [Account Migration OTP](https://wiki.vg/Mojang_API#Account_Migration_OTP)
|
||||
- [ ] [Verify Account Migration OTP](https://wiki.vg/Mojang_API#Verify_Account_Migration_OTP)
|
||||
- [ ] [Submit Migration Token](https://wiki.vg/Mojang_API#Submit_Migration_Token)
|
||||
- [ ] [Connect Xbox Live](https://wiki.vg/Mojang_API#Connect_Xbox_Live)
|
||||
|
||||
### Multiplayer Authentication
|
||||
- [x] [Client Authentication](https://wiki.vg/Protocol_Encryption#Client)
|
||||
- [x] [Server Authentication](https://wiki.vg/Protocol_Encryption#Server)
|
||||
|
||||
### Game Files
|
||||
- [ ] [Game Versions](https://wiki.vg/Game_files#Game)
|
||||
- [ ] [Libraries](https://wiki.vg/Game_files#Libraries)
|
||||
- [ ] [Assets](https://wiki.vg/Game_files#Assets)
|
||||
- [ ] [Java Version](https://wiki.vg/Game_files#Java_version)
|
||||
|
|
|
@ -0,0 +1,171 @@
|
|||
// Yeahbut January 2024
|
||||
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use reqwest;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use base64::{Engine as _, engine::general_purpose};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UsernameToUuid {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legacy: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub demo: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub path: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub errorMessage: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn username_to_uuid(username: &str)
|
||||
-> Result<UsernameToUuid, Box<dyn std::error::Error>>
|
||||
{
|
||||
let url = format!(
|
||||
"https://api.mojang.com/users/profiles/minecraft/{}",
|
||||
username
|
||||
);
|
||||
|
||||
let resp = reqwest::get(url)
|
||||
.await?
|
||||
.json::<UsernameToUuid>()
|
||||
.await?;
|
||||
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProfileTextureMetadata {
|
||||
pub model: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProfileTexture {
|
||||
pub url: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub metadata: Option<ProfileTextureMetadata>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProfileTextures {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub SKIN: Option<ProfileTexture>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub CAPE: Option<ProfileTexture>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProfileValue {
|
||||
pub timestamp: i64,
|
||||
pub profileId: String,
|
||||
pub profileName: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub signatureRequired: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub textures: Option<ProfileTextures>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProfileProperty {
|
||||
pub name: String,
|
||||
pub value: ProfileValue,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub(crate) struct ProfilePropertyPrivate {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UUIDToProfile {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub properties: Vec<ProfileProperty>,
|
||||
pub profileActions: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legacy: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub demo: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub path: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub errorMessage: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct UUIDToProfilePrivate {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub properties: Vec<ProfilePropertyPrivate>,
|
||||
pub profileActions: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legacy: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub demo: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub path: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub errorMessage: Option<String>,
|
||||
}
|
||||
|
||||
pub(crate) fn get_profile_value(properties: Vec<ProfilePropertyPrivate>)
|
||||
-> Result<Vec<ProfileProperty>, Box<dyn std::error::Error>>
|
||||
{
|
||||
let mut output: Vec<ProfileProperty> = Vec::new();
|
||||
|
||||
for property in properties {
|
||||
output.push(ProfileProperty {
|
||||
name: property.name,
|
||||
value: serde_json::from_slice(
|
||||
&general_purpose::STANDARD_NO_PAD.decode(property.value)?
|
||||
)?,
|
||||
signature: property.signature,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub async fn uuid_to_profile(uuid: &str)
|
||||
-> Result<UUIDToProfile, Box<dyn std::error::Error>>
|
||||
{
|
||||
let url = format!(
|
||||
"https://sessionserver.mojang.com/session/minecraft/profile/{}",
|
||||
uuid
|
||||
);
|
||||
|
||||
let resp = reqwest::get(url)
|
||||
.await?
|
||||
.json::<UUIDToProfilePrivate>()
|
||||
.await?;
|
||||
|
||||
let output = UUIDToProfile {
|
||||
id: resp.id,
|
||||
name: resp.name,
|
||||
properties: get_profile_value(resp.properties)?,
|
||||
profileActions: resp.profileActions,
|
||||
legacy: resp.legacy,
|
||||
demo: resp.demo,
|
||||
path: resp.path,
|
||||
error: resp.error,
|
||||
errorMessage: resp.errorMessage,
|
||||
};
|
||||
|
||||
Ok(output)
|
||||
}
|
16
src/lib.rs
16
src/lib.rs
|
@ -1,14 +1,4 @@
|
|||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
// Yeahbut January 2024
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
pub mod accounts;
|
||||
pub mod multiplayer_auth;
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
// Yeahbut June 2024
|
||||
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::fmt;
|
||||
use reqwest;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crypto::digest::Digest;
|
||||
use sha1::Sha1;
|
||||
use num_bigint::BigInt;
|
||||
|
||||
use crate::accounts::{
|
||||
ProfileProperty,
|
||||
ProfilePropertyPrivate,
|
||||
get_profile_value,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AuthError {
|
||||
AuthError,
|
||||
}
|
||||
|
||||
impl fmt::Display for AuthError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
AuthError::AuthError => write!(f, "AuthError"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for AuthError {}
|
||||
|
||||
pub async fn server_id_hash(
|
||||
server_id: &[u8],
|
||||
shared_secret: &[u8],
|
||||
public_key: &[u8],
|
||||
) -> String {
|
||||
let hash_data = [server_id,shared_secret,public_key].concat();
|
||||
let hash = BigInt::from_signed_bytes_be(
|
||||
&Sha1::digest(hash_data)).to_str_radix(16);
|
||||
hash
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PlayerJoining {
|
||||
pub accessToken: String,
|
||||
pub selectedProfile: String,
|
||||
pub serverId: String,
|
||||
}
|
||||
|
||||
pub async fn join(
|
||||
accessToken: String,
|
||||
selectedProfile: String,
|
||||
serverId: String,
|
||||
) -> Result<reqwest::Response, Box<dyn std::error::Error>> {
|
||||
let url = "https://sessionserver.mojang.com/session/minecraft/join";
|
||||
|
||||
let resp = reqwest::Client::new()
|
||||
.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&PlayerJoining {accessToken,selectedProfile,serverId})
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct JoinedPlayer {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub properties: Vec<ProfileProperty>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct JoinedPlayerPrivate {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub properties: Vec<ProfilePropertyPrivate>,
|
||||
}
|
||||
|
||||
pub async fn joined(
|
||||
username: &str,
|
||||
server_id: &str,
|
||||
ip: Option<&str>,
|
||||
) -> Result<JoinedPlayer, Box<dyn std::error::Error>> {
|
||||
let url = match ip {
|
||||
Some(ip) => format!(
|
||||
"https://sessionserver.mojang.com/session/minecraft/hasJoined?\
|
||||
username={}&serverId={}&ip={}",
|
||||
username,
|
||||
server_id,
|
||||
ip,
|
||||
),
|
||||
None => format!(
|
||||
"https://sessionserver.mojang.com/session/minecraft/hasJoined?\
|
||||
username={}&serverId={}",
|
||||
username,
|
||||
server_id,
|
||||
)
|
||||
};
|
||||
|
||||
let resp = reqwest::get(url)
|
||||
.await?;
|
||||
|
||||
if resp.status() == 200 {
|
||||
let data = resp.json::<JoinedPlayerPrivate>().await?;
|
||||
Ok(JoinedPlayer {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
properties: get_profile_value(data.properties)?
|
||||
})
|
||||
} else {
|
||||
Err(Box::new(AuthError::AuthError))
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue