Added multiplayer authentication end points

This commit is contained in:
Kyler 2024-06-02 17:47:48 -06:00
parent d875abc27e
commit 03be1055d5
5 changed files with 124 additions and 4 deletions

View File

@ -11,3 +11,6 @@ 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"

View File

@ -38,8 +38,8 @@ Purple Cello interface for the Mojang API in rust
- [ ] [Connect Xbox Live](https://wiki.vg/Mojang_API#Connect_Xbox_Live)
### Multiplayer Authentication
- [ ] [Client Authentication](https://wiki.vg/Protocol_Encryption#Client)
- [ ] [Server Authentication](https://wiki.vg/Protocol_Encryption#Server)
- [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)

View File

@ -80,7 +80,7 @@ pub struct ProfileProperty {
}
#[derive(Serialize, Deserialize)]
struct ProfilePropertyPrivate {
pub(crate) struct ProfilePropertyPrivate {
pub name: String,
pub value: String,
#[serde(skip_serializing_if = "Option::is_none")]
@ -124,7 +124,7 @@ struct UUIDToProfilePrivate {
pub errorMessage: Option<String>,
}
fn get_profile_value(properties: Vec<ProfilePropertyPrivate>)
pub(crate) fn get_profile_value(properties: Vec<ProfilePropertyPrivate>)
-> Result<Vec<ProfileProperty>, Box<dyn std::error::Error>>
{
let mut output: Vec<ProfileProperty> = Vec::new();

View File

@ -1,3 +1,4 @@
// Yeahbut January 2024
pub mod accounts;
pub mod multiplayer_auth;

116
src/multiplayer_auth.rs Normal file
View File

@ -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))
}
}