Skip to content

Commit

Permalink
Added PKey for return
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaileke committed Sep 5, 2024
1 parent 594fea1 commit 4e04130
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ edition = "2021"
[dependencies]
reqwest = { version = "0.12.7", features = ["native-tls", "json"] }
native-tls = "0.2.12"
tokio = { version = "1.39.3", features = ["full"] }
serde_json = "1.0.125"
serde = { version = "1.0.208", features = ["derive"] }
tokio = { version = "1.40.0", features = ["full"] }
serde_json = "1.0.128"
serde = { version = "1.0.209", features = ["derive"] }
uuid = { version = "1.10.0", features = ["v4"] }
openssl = "0.10.66"
base64 = "0.22.1"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use minecraft_auth::bedrock;

#[tokio::main]
async fn main() {
let mut bedrock = bedrock::new(false); // debug = false
let mut bedrock = bedrock::new("1.21.2".to_string(), false); // (client version, debug mode)
if bedrock.auth().await {
let chain = bedrock.get_chain_data();
let pkey = bedrock.get_pkey().unwrap(); // When sending the Login Packet we will need this
println!("Chain 1: {}\nChain 2: {}", chain[0], chain[1]);
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use serde::de::StdError;
pub struct Bedrock {
client: Client,
client_id: &'static str,
client_version: &'static str,
client_version: String,
chain_data: Vec<String>,
pkey: Option<PKey<Private>>,
debug: bool
}

Expand Down Expand Up @@ -77,13 +78,14 @@ pub struct ChainData {
chain: Value
}

pub fn new(debug: bool) -> Bedrock {
pub fn new(client_version: String, debug: bool) -> Bedrock {
let client = Client::new();
Bedrock {
client,
client_id: "0000000048183522",
client_version: "1.21.2",
client_version: client_version.clone(),
chain_data: vec!["".to_string(), "".to_string()],
pkey: None,
debug,
}
}
Expand Down Expand Up @@ -239,6 +241,10 @@ impl Bedrock {
self.chain_data.to_vec()
}

pub fn get_pkey(&self) -> Option<PKey<Private>> {
self.pkey.clone()
}

pub async fn oauth20_connect(&self) -> Result<(Option<OAuth20Connect>, Option<ErrorResponse>), Error> {
let mut body = HashMap::new();
body.insert("client_id", self.client_id);
Expand Down Expand Up @@ -528,9 +534,11 @@ impl Bedrock {
}
}

pub async fn minecraft_authentication(&self, xbox_user_id: String, authorization_token: String) -> Result<(Option<ChainData>, Option<ErrorResponse>), Error> {
pub async fn minecraft_authentication(&mut self, xbox_user_id: String, authorization_token: String) -> Result<(Option<ChainData>, Option<ErrorResponse>), Error> {
let group = EcGroup::from_curve_name(Nid::SECP384R1).expect("EC Group Error");
let ec_key = EcKey::generate(&group).expect("Private Key Error");
let pkey = PKey::from_ec_key(ec_key.clone()).expect("PKey Error");
self.pkey = Option::from(pkey);

let public_key_pem = ec_key.public_key_to_pem().expect("Public Key PEM Error");
let public_key_der = pem_to_der(&public_key_pem).expect("Public Key Der Error");
Expand All @@ -543,7 +551,7 @@ impl Bedrock {
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());
headers.insert(USER_AGENT, "MCPE/Android".parse().unwrap());
headers.insert("Client-Version", HeaderValue::from_static(self.client_version));
headers.insert("Client-Version", HeaderValue::from_str(self.client_version.as_str()).unwrap());
headers.insert(AUTHORIZATION, format!("XBL3.0 x={};{}", xbox_user_id, authorization_token).as_str().parse().unwrap());

let response = self
Expand Down
3 changes: 2 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ mod tests {

#[tokio::test]
async fn test_work_function() {
let mut bedrock = bedrock::new(false);
let mut bedrock = bedrock::new("1.21.2".to_string(), false);
if bedrock.auth().await {
let chain = bedrock.get_chain_data();
let _pkey = bedrock.get_pkey().unwrap();
println!("Chain 1: {}\n\nChain 2: {}", chain[0], chain[1]);
}
}
Expand Down

0 comments on commit 4e04130

Please sign in to comment.