Skip to content

Commit

Permalink
Address clippy lints and clean up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ramosbugs committed Feb 29, 2024
1 parent c3a94d3 commit 9aadf67
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 65 deletions.
54 changes: 22 additions & 32 deletions examples/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ fn main() {
let issuer_url = IssuerUrl::new("https://gitlab.com".to_string()).expect("Invalid issuer URL");

// Fetch GitLab's OpenID Connect discovery document.
//
let provider_metadata = CoreProviderMetadata::discover(&issuer_url, http_client)
.unwrap_or_else(|err| {
handle_error(&err, "Failed to discover OpenID Provider");
Expand Down Expand Up @@ -97,16 +96,15 @@ fn main() {
.add_scope(Scope::new("profile".to_string()))
.url();

println!("Open this URL in your browser:\n{}\n", authorize_url);
println!("Open this URL in your browser:\n{authorize_url}\n");

// A very naive implementation of the redirect server.
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let (code, state) = {
// A very naive implementation of the redirect server.
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();

// Accept one connection
let (mut stream, _) = listener.accept().unwrap();

// Accept one connection
let (mut stream, _) = listener.accept().unwrap();
let code;
let state;
{
let mut reader = BufReader::new(&stream);

let mut request_line = String::new();
Expand All @@ -115,36 +113,28 @@ fn main() {
let redirect_url = request_line.split_whitespace().nth(1).unwrap();
let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap();

let code_pair = url
let code = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "code"
})
.find(|(key, _)| key == "code")
.map(|(_, code)| AuthorizationCode::new(code.into_owned()))
.unwrap();

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

let state_pair = url
let state = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "state"
})
.find(|(key, _)| key == "state")
.map(|(_, state)| CsrfToken::new(state.into_owned()))
.unwrap();

let (_, value) = state_pair;
state = CsrfToken::new(value.into_owned());
}
let message = "Go back to your terminal :)";
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
message.len(),
message
);
stream.write_all(response.as_bytes()).unwrap();

let message = "Go back to your terminal :)";
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
message.len(),
message
);
stream.write_all(response.as_bytes()).unwrap();
(code, state)
};

println!("GitLab returned the following code:\n{}\n", code.secret());
println!(
Expand Down
50 changes: 20 additions & 30 deletions examples/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,13 @@ fn main() {

println!("Open this URL in your browser:\n{}\n", authorize_url);

// A very naive implementation of the redirect server.
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let (code, state) = {
// A very naive implementation of the redirect server.
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();

// Accept one connection
let (mut stream, _) = listener.accept().unwrap();
// Accept one connection
let (mut stream, _) = listener.accept().unwrap();

let code;
let state;
{
let mut reader = BufReader::new(&stream);

let mut request_line = String::new();
Expand All @@ -157,36 +155,28 @@ fn main() {
let redirect_url = request_line.split_whitespace().nth(1).unwrap();
let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap();

let code_pair = url
let code = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "code"
})
.find(|(key, _)| key == "code")
.map(|(_, code)| AuthorizationCode::new(code.into_owned()))
.unwrap();

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

let state_pair = url
let state = url
.query_pairs()
.find(|pair| {
let &(ref key, _) = pair;
key == "state"
})
.find(|(key, _)| key == "state")
.map(|(_, state)| CsrfToken::new(state.into_owned()))
.unwrap();

let (_, value) = state_pair;
state = CsrfToken::new(value.into_owned());
}
let message = "Go back to your terminal :)";
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
message.len(),
message
);
stream.write_all(response.as_bytes()).unwrap();

let message = "Go back to your terminal :)";
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
message.len(),
message
);
stream.write_all(response.as_bytes()).unwrap();
(code, state)
};

println!("Google returned the following code:\n{}\n", code.secret());
println!(
Expand Down
2 changes: 1 addition & 1 deletion src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ mod tests {
);
assert_eq!(
Some(
&vec![
&[
"email",
"phone",
"profile",
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ where
AuthenticationFlow::Implicit(include_token) => {
if include_token {
OAuth2ResponseType::new(
vec![
[
core::CoreResponseType::IdToken,
core::CoreResponseType::Token,
]
Expand Down
2 changes: 1 addition & 1 deletion tests/rp_certification_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn rp_response_type_code() {
#[test]
#[ignore]
fn rp_scope_userinfo_claims() {
let user_info_scopes = vec!["profile", "email", "address", "phone"]
let user_info_scopes = ["profile", "email", "address", "phone"]
.iter()
.map(|scope| Scope::new((*scope).to_string()))
.collect::<Vec<_>>();
Expand Down

0 comments on commit 9aadf67

Please sign in to comment.