summaryrefslogtreecommitdiffstats
path: root/vendor/http-auth/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/http-auth/examples
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/http-auth/examples')
-rw-r--r--vendor/http-auth/examples/reqwest.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/vendor/http-auth/examples/reqwest.rs b/vendor/http-auth/examples/reqwest.rs
new file mode 100644
index 000000000..f9173a509
--- /dev/null
+++ b/vendor/http-auth/examples/reqwest.rs
@@ -0,0 +1,67 @@
+// Copyright (C) 2021 Scott Lamb <slamb@slamb.org>
+// SPDX-License-Identifier: MIT OR Apache-2.0
+
+//! Verbose example of making authenticated requests with the `reqwest` crate.
+
+use std::convert::TryFrom;
+
+use reqwest::header::HeaderValue;
+
+fn main() {
+ let args: Vec<String> = std::env::args().collect();
+ let (url, username, password) = match &args[..] {
+ [_program, url, username, password] => (url, username, password),
+ [program, ..] => {
+ eprintln!("expected {} URL USERNAME PASSWORD", program);
+ std::process::exit(1);
+ }
+ [] => panic!("no commandline arguments, not even argv[0]"),
+ };
+ let url = reqwest::Url::try_from(url.as_str()).unwrap();
+
+ // Create a client which doesn't follow redirects. The URI used below won't
+ // be correct with reqwest's automatic redirect handling.
+ let client = reqwest::blocking::Client::builder()
+ .redirect(reqwest::redirect::Policy::none())
+ .build()
+ .unwrap();
+ let first_resp = client.get(url.clone()).send().unwrap();
+ if first_resp.status() != reqwest::StatusCode::UNAUTHORIZED {
+ eprintln!(
+ "Server returned status {} without authentication!",
+ first_resp.status()
+ );
+ std::process::exit(1);
+ }
+
+ let mut pw_client = http_auth::PasswordClient::try_from(
+ first_resp
+ .headers()
+ .get_all(reqwest::header::WWW_AUTHENTICATE),
+ )
+ .unwrap();
+ println!("Password challenge client: {:#?}", &pw_client);
+ let authorization = pw_client
+ .respond(&http_auth::PasswordParams {
+ username,
+ password,
+
+ // Note that URI is typically a path.
+ uri: url.path(),
+ method: reqwest::Method::GET.as_str(),
+ body: Some(&[]),
+ })
+ .unwrap();
+ println!("Authorization: {}", &authorization);
+ let mut authorization = HeaderValue::try_from(authorization).unwrap();
+ authorization.set_sensitive(true);
+ let second_resp = client
+ .get(url)
+ .header(reqwest::header::AUTHORIZATION, authorization)
+ .send()
+ .unwrap();
+ println!(
+ "After authorization, server returned status {}",
+ second_resp.status()
+ );
+}