Expand description
HTTP authentication. Currently meant for clients; to be extended for servers.
As described in the following documents and specifications:
- MDN documentation.
- RFC 7235: Hypertext Transfer Protocol (HTTP/1.1): Authentication.
- RFC 7617: The ‘Basic’ HTTP Authentication Scheme
- RFC 7616: HTTP Digest Access Authentication
This framework is primarily used with HTTP, as suggested by the name. It is also used by some other protocols such as RTSP.
§Cargo Features
feature | default? | description |
---|---|---|
basic-scheme | yes | support for the Basic auth scheme |
digest-scheme | yes | support for the Digest auth scheme |
http | no | convenient conversion from http crate types, version 0.2 |
http10 | no | convenient conversion from http crate types, version 1.0 |
§Example
In most cases, callers only need to use PasswordClient
and
PasswordParams
to handle Basic
and Digest
authentication schemes.
use std::convert::TryFrom as _;
use http_auth::PasswordClient;
let WWW_AUTHENTICATE_VAL = "UnsupportedSchemeA, Basic realm=\"foo\", UnsupportedSchemeB";
let mut pw_client = http_auth::PasswordClient::try_from(WWW_AUTHENTICATE_VAL).unwrap();
assert!(matches!(pw_client, http_auth::PasswordClient::Basic(_)));
let response = pw_client.respond(&http_auth::PasswordParams {
username: "Aladdin",
password: "open sesame",
uri: "/",
method: "GET",
body: Some(&[]),
}).unwrap();
assert_eq!(response, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
The http
or http10
features allow parsing all WWW-Authenticate
headers within a
http::HeaderMap
in one call.
use http::header::{HeaderMap, WWW_AUTHENTICATE};
let mut headers = HeaderMap::new();
headers.append(WWW_AUTHENTICATE, "UnsupportedSchemeA".parse().unwrap());
headers.append(WWW_AUTHENTICATE, "Basic realm=\"foo\", UnsupportedSchemeB".parse().unwrap());
let mut pw_client = PasswordClient::try_from(headers.get_all(WWW_AUTHENTICATE)).unwrap();
assert!(matches!(pw_client, http_auth::PasswordClient::Basic(_)));
Re-exports§
pub use parser::ChallengeParser;
pub use crate::basic::BasicClient;
basic-scheme
pub use crate::digest::DigestClient;
digest-scheme
Modules§
- basic
basic-scheme
Basic
authentication scheme as in RFC 7617.- digest
digest-scheme
Digest
authentication scheme, as in RFC 7616.- parser
- Parses as in RFC 7235.
Structs§
- Challenge
Ref - Parsed challenge (scheme and body) using references to the original header value.
Produced by
crate::parser::ChallengeParser
. - Param
Value - Parsed challenge parameter value used within
ChallengeRef
. - Password
Client Builder - Builds a
PasswordClient
from the supplied challenges; create viaPasswordClient::builder
. - Password
Params - Parameters for responding to a password challenge.
- ToStr
Error - An error returned by
HeaderValue::to_str
.
Enums§
- Password
Client - Client for responding to a password challenge.
Traits§
- Header
Value - A trait for the parts needed from http crate 0.2 or 1.0’s
HeaderValue
type.
Functions§
- parse_
challenges - Parses a list of challenges into a
Vec
.