Skip to content

Commit

Permalink
➖ Remove url dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Quarneti committed Dec 7, 2022
1 parent d99e1a5 commit 9e61136
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
29 changes: 27 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mclib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = "An unofficial launcher for Minecraft: Java Edition"
ureq = { version = "2", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
url = { version = "2", features = ["serde"] }
regex = "1"
anyhow = "1"
open = "3"
directories = "4"
Expand Down
8 changes: 8 additions & 0 deletions mclib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ pub mod runtime_manager;

const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

#[macro_export]
macro_rules! regex {
($re:literal $(,)?) => {{
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
RE.get_or_init(|| regex::Regex::new($re).unwrap())
}};
}

pub static BASE_DIR: Lazy<PathBuf> = Lazy::new(|| {
ProjectDirs::from("eu", "mq1", "ice-launcher")
.expect("Could not get project directories")
Expand Down
52 changes: 12 additions & 40 deletions mclib/src/msa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;

use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use arrayvec::ArrayString;
use base64ct::{Base64UrlUnpadded, Encoding};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Digest, Sha256};
use url::Url;

use super::HTTP_CLIENT;
use super::{regex, HTTP_CLIENT};

const MSA_AUTHORIZATION_ENDPOINT: &str =
"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize";
Expand Down Expand Up @@ -55,22 +54,13 @@ fn get_state() -> String {
.collect()
}

pub fn get_auth_url() -> (Url, String, String) {
pub fn get_auth_url() -> (String, String, String) {
let state = get_state();
let (code_verifier, code_challenge) = get_code_challenge();

let params = [
("client_id", CLIENT_ID),
("response_type", "code"),
("redirect_uri", REDIRECT_URI),
("response_mode", "query"),
("scope", SCOPE),
("state", &state),
("code_challenge", &code_challenge),
("code_challenge_method", "S256"),
];

let url = Url::parse_with_params(MSA_AUTHORIZATION_ENDPOINT, &params).unwrap();
let url = format!(
"{MSA_AUTHORIZATION_ENDPOINT}?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&response_mode=query&scope={SCOPE}&state={state}&code_challenge={code_challenge}&code_challenge_method=S256",
);

(url, state, code_verifier)
}
Expand Down Expand Up @@ -262,30 +252,12 @@ pub fn listen_login_callback(csrf_token: String, pkce_verifier: String) -> Resul
let mut request_line = String::new();
reader.read_line(&mut request_line)?;

let redirect_url = request_line.split_whitespace().nth(1).unwrap();
let url = Url::parse(&("http://localhost".to_string() + redirect_url))?;

let code_pair = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "code"
})
.ok_or(anyhow!("Code not found"))?;

let (_, value) = code_pair;
code = value.into_owned();

let state_pair = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "state"
})
.ok_or(anyhow!("State not found"))?;

let (_, value) = state_pair;
state = value.into_owned();
let caps = regex!(r"/login\?code=(?P<code>.*)&state=(?P<state>.*) ")
.captures(&request_line)
.unwrap();

code = caps["code"].to_string();
state = caps["state"].to_string();
}

if state != csrf_token {
Expand Down

0 comments on commit 9e61136

Please sign in to comment.