Skip to content
This repository was archived by the owner on Mar 14, 2026. It is now read-only.

Commit e560d81

Browse files
committed
fix: parsing
1 parent e6426de commit e560d81

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ base64 = { version = "0.22.0", optional = true }
3333
ring = { version = "0.17.8", optional = true }
3434
rand = { version = "0.8.5", optional = true }
3535
hex = { version = "0.4.3", optional = true }
36+
url = { version = "2.5.0", optional = true}
3637
displaydoc = "0.2.4"
3738
thiserror = "1.0.58"
38-
url = "2.5.0"
3939

4040
[dev-dependencies]
4141
dotenv = { version = "0.15.0" }
@@ -48,6 +48,7 @@ custom-auth = [
4848
"dep:reqwest",
4949
"dep:serde",
5050
"dep:rand",
51+
"dep:url"
5152
]
5253
custom-launch = []
5354
minecraft-auth = ["dep:ring", "dep:hex"]

src/custom/oauth.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,50 @@ pub fn server(port: u16) -> Result<impl AsyncSendSync<Result<Info, OAuthError>>,
105105
})
106106
}
107107

108+
108109
fn parse_info(data: &[u8]) -> Result<Info, OAuthError> {
109-
// Assuming the data is in a format that can be directly deserialized into Info
110-
// For demonstration, let's assume the data is a JSON string
111110
let data_str = std::str::from_utf8(data)
112111
.map_err(|_| OAuthError::ParseError("Invalid UTF-8".to_string()))?;
113-
println!("Data: {} \n", data_str);
114-
serde_json::from_str::<Info>(data_str)
115-
.map_err(|_| OAuthError::ParseError("Failed to parse Info".to_string()))
112+
113+
// Extract the query string from the HTTP request
114+
let query_start = data_str.find('?').ok_or_else(|| OAuthError::ParseError("No query string found".to_string()))?;
115+
let query_end = data_str.find('#').unwrap_or_else(|| data_str.len());
116+
let query_string = &data_str[query_start + 1..query_end];
117+
118+
// Parse the query string directly
119+
let query_params: Vec<(String, String)> = url::form_urlencoded::parse(query_string.as_bytes())
120+
.into_owned()
121+
.collect();
122+
123+
// Extract the 'code', 'state', 'error', and 'error_description' parameters
124+
let code = query_params.iter().find_map(|(k, v)| if k == "code" { Some(v.clone()) } else { None });
125+
let state = query_params.iter().find_map(|(k, v)| {
126+
if k == "state" {
127+
// Find the position of "HTTP/1.1\r\n" in the state value
128+
let http_start = v.find(" HTTP/1.1\r\n");
129+
// If found, slice the string up to that position
130+
http_start.map(|pos| v[..pos].to_string())
131+
} else {
132+
None
133+
}
134+
});
135+
let error = query_params.iter().find_map(|(k, v)| if k == "error" { Some(v.clone()) } else { None });
136+
let error_description = query_params.iter().find_map(|(k, v)| if k == "error_description" { Some(v.clone()) } else { None });
137+
138+
// Construct the Info struct
139+
let info = Info {
140+
code,
141+
state,
142+
error,
143+
error_description,
144+
};
145+
println!("{:?}", info);
146+
147+
Ok(info)
116148
}
117149

150+
151+
118152
pub fn token(
119153
code: &str,
120154
client_id: &str,

0 commit comments

Comments
 (0)