2016-03-16 14:25:35 -04:00
|
|
|
// Copyright 2016 Matthew Collins
|
2015-09-17 11:21:56 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-09-17 11:04:25 -04:00
|
|
|
|
2016-04-05 14:35:58 -04:00
|
|
|
use openssl::crypto::hash;
|
2015-09-17 11:04:25 -04:00
|
|
|
use serde_json;
|
2016-03-20 19:43:31 -04:00
|
|
|
use serde_json::builder::ObjectBuilder;
|
2015-09-17 11:04:25 -04:00
|
|
|
use hyper;
|
2015-09-10 06:49:41 -04:00
|
|
|
|
2016-03-20 19:43:31 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2015-09-10 06:49:41 -04:00
|
|
|
pub struct Profile {
|
2015-09-10 06:58:42 -04:00
|
|
|
pub username: String,
|
|
|
|
pub id: String,
|
2015-10-07 14:36:59 -04:00
|
|
|
pub access_token: String,
|
2015-09-10 06:49:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const JOIN_URL: &'static str = "https://sessionserver.mojang.com/session/minecraft/join";
|
2016-03-20 19:43:31 -04:00
|
|
|
const LOGIN_URL: &'static str = "https://authserver.mojang.com/authenticate";
|
|
|
|
const REFRESH_URL: &'static str = "https://authserver.mojang.com/refresh";
|
|
|
|
const VALIDATE_URL: &'static str = "https://authserver.mojang.com/validate";
|
2015-09-10 06:49:41 -04:00
|
|
|
|
|
|
|
impl Profile {
|
2016-03-20 19:43:31 -04:00
|
|
|
pub fn login(username: &str, password: &str, token: &str) -> Result<Profile, super::Error> {
|
|
|
|
let req_msg = ObjectBuilder::new()
|
|
|
|
.insert("username", username)
|
|
|
|
.insert("password", password)
|
|
|
|
.insert("clientToken", token)
|
|
|
|
.insert_object("agent", |b| b
|
|
|
|
.insert("name", "Minecraft")
|
|
|
|
.insert("version", 1)
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let req = try!(serde_json::to_string(&req_msg));
|
|
|
|
|
|
|
|
let client = hyper::Client::new();
|
|
|
|
let res = try!(client.post(LOGIN_URL)
|
|
|
|
.body(&req)
|
|
|
|
.header(hyper::header::ContentType("application/json".parse().unwrap()))
|
|
|
|
.send());
|
|
|
|
|
|
|
|
let ret: serde_json::Value = try!(serde_json::from_reader(res));
|
|
|
|
if let Some(error) = ret.find("error").and_then(|v| v.as_string()) {
|
|
|
|
return Err(super::Error::Err(format!(
|
|
|
|
"{}: {}",
|
|
|
|
error,
|
|
|
|
ret.find("errorMessage").and_then(|v| v.as_string()).unwrap())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(Profile {
|
|
|
|
username: ret.lookup("selectedProfile.name").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
id: ret.lookup("selectedProfile.id").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
access_token: ret.find("accessToken").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn refresh(self, token: &str) -> Result<Profile, super::Error> {
|
|
|
|
let req_msg = ObjectBuilder::new()
|
|
|
|
.insert("accessToken", self.access_token.clone())
|
|
|
|
.insert("clientToken", token)
|
|
|
|
.unwrap();
|
|
|
|
let req = try!(serde_json::to_string(&req_msg));
|
|
|
|
|
|
|
|
let client = hyper::Client::new();
|
|
|
|
let res = try!(client.post(VALIDATE_URL)
|
|
|
|
.body(&req)
|
|
|
|
.header(hyper::header::ContentType("application/json".parse().unwrap()))
|
|
|
|
.send());
|
|
|
|
|
|
|
|
if res.status != hyper::status::StatusCode::NoContent {
|
|
|
|
// Refresh needed
|
|
|
|
let res = try!(client.post(REFRESH_URL)
|
|
|
|
.body(&req)
|
|
|
|
.header(hyper::header::ContentType("application/json".parse().unwrap()))
|
|
|
|
.send());
|
|
|
|
|
|
|
|
let ret: serde_json::Value = try!(serde_json::from_reader(res));
|
|
|
|
if let Some(error) = ret.find("error").and_then(|v| v.as_string()) {
|
|
|
|
return Err(super::Error::Err(format!(
|
|
|
|
"{}: {}",
|
|
|
|
error,
|
|
|
|
ret.find("errorMessage").and_then(|v| v.as_string()).unwrap())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return Ok(Profile {
|
|
|
|
username: ret.lookup("selectedProfile.name").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
id: ret.lookup("selectedProfile.id").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
access_token: ret.find("accessToken").and_then(|v| v.as_string()).unwrap().to_owned(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2016-03-26 10:24:26 -04:00
|
|
|
pub fn join_server(&self, server_id: &str, shared_key: &[u8], public_key: &[u8]) -> Result<(), super::Error> {
|
2016-04-05 14:35:58 -04:00
|
|
|
use std::io::Write;
|
|
|
|
let mut sha1 = hash::Hasher::new(hash::Type::SHA1);
|
|
|
|
sha1.write_all(server_id.as_bytes()).unwrap();
|
|
|
|
sha1.write_all(shared_key).unwrap();
|
|
|
|
sha1.write_all(public_key).unwrap();
|
|
|
|
let mut hash = sha1.finish();
|
2015-09-10 06:49:41 -04:00
|
|
|
|
|
|
|
// Mojang uses a hex method which allows for
|
|
|
|
// negatives so we have to account for that.
|
2016-03-23 18:58:09 -04:00
|
|
|
let negative = (hash[0] & 0x80) == 0x80;
|
2015-09-10 06:49:41 -04:00
|
|
|
if negative {
|
|
|
|
twos_compliment(&mut hash);
|
|
|
|
}
|
2016-03-23 18:58:09 -04:00
|
|
|
let hash_str = hash.iter().map(|b| format!("{:02x}", b)).collect::<Vec<String>>().join("");
|
2016-03-28 17:10:33 -04:00
|
|
|
let hash_val = hash_str.trim_left_matches('0');
|
2015-09-10 06:49:41 -04:00
|
|
|
let hash_str = if negative {
|
|
|
|
"-".to_owned() + &hash_val[..]
|
|
|
|
} else {
|
|
|
|
hash_val.to_owned()
|
|
|
|
};
|
|
|
|
|
2016-03-20 19:43:31 -04:00
|
|
|
let join_msg = ObjectBuilder::new()
|
2015-10-07 14:36:59 -04:00
|
|
|
.insert("accessToken", &self.access_token)
|
|
|
|
.insert("selectedProfile", &self.id)
|
|
|
|
.insert("serverId", hash_str)
|
|
|
|
.unwrap();
|
2015-09-10 06:49:41 -04:00
|
|
|
let join = serde_json::to_string(&join_msg).unwrap();
|
|
|
|
|
|
|
|
let client = hyper::Client::new();
|
2016-03-21 06:55:31 -04:00
|
|
|
let res = try!(client.post(JOIN_URL)
|
2015-10-07 14:36:59 -04:00
|
|
|
.body(&join)
|
|
|
|
.header(hyper::header::ContentType("application/json".parse().unwrap()))
|
2016-03-21 06:55:31 -04:00
|
|
|
.send());
|
2015-09-10 06:49:41 -04:00
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
if res.status == hyper::status::StatusCode::NoContent {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(super::Error::Err("Failed to auth with server".to_owned()))
|
|
|
|
}
|
2015-09-10 06:49:41 -04:00
|
|
|
}
|
2016-03-20 19:43:31 -04:00
|
|
|
|
|
|
|
pub fn is_complete(&self) -> bool {
|
|
|
|
!self.username.is_empty() && !self.id.is_empty() && !self.access_token.is_empty()
|
|
|
|
}
|
2015-09-10 06:49:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn twos_compliment(data: &mut Vec<u8>) {
|
2015-09-10 06:58:42 -04:00
|
|
|
let mut carry = true;
|
2015-10-07 14:36:59 -04:00
|
|
|
for i in (0..data.len()).rev() {
|
2015-09-10 06:58:42 -04:00
|
|
|
data[i] = !data[i];
|
|
|
|
if carry {
|
|
|
|
carry = data[i] == 0xFF;
|
2016-03-23 18:58:09 -04:00
|
|
|
data[i] = data[i].wrapping_add(1);
|
2015-09-10 06:58:42 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 06:49:41 -04:00
|
|
|
}
|