use base64::engine::general_purpose::STANDARD as ENGINE; use base64::Engine; use bytes::Bytes;
use util::HeaderValueString; use HeaderValue;
/// `Authorization` header, defined in [RFC7235](https://tools.ietf.org/html/rfc7235#section-4.2) /// /// The `Authorization` header field allows a user agent to authenticate /// itself with an origin server -- usually, but not necessarily, after /// receiving a 401 (Unauthorized) response. Its value consists of /// credentials containing the authentication information of the user /// agent for the realm of the resource being requested. /// /// # ABNF /// /// ```text /// Authorization = credentials /// ``` /// /// # Example values /// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==` /// * `Bearer fpKL54jvWmEGVoRdCNjG` /// /// # Examples /// /// ``` /// # extern crate headers; /// use headers::Authorization; /// /// let basic = Authorization::basic("Aladdin", "open sesame"); /// let bearer = Authorization::bearer("some-opaque-token").unwrap(); /// ``` /// #[derive(Clone, PartialEq, Debug)] pubstruct Authorization<C: Credentials>(pub C);
impl Authorization<Basic> { /// Create a `Basic` authorization header. pubfn basic(username: &str, password: &str) -> Self { let colon_pos = username.len(); let decoded = format!("{}:{}", username, password);
fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { letmut value = self.0.encode();
value.set_sensitive(true);
debug_assert!(
value.as_bytes().starts_with(C::SCHEME.as_bytes()), "Credentials::encode should include its scheme: scheme = {:?}, encoded = {:?}",
C::SCHEME,
value,
);
values.extend(::std::iter::once(value));
}
}
/// Credentials to be used in the `Authorization` header. pubtrait Credentials: Sized { /// The scheme identify the format of these credentials. /// /// This is the static string that always prefixes the actual credentials, /// like `"Basic"` in basic authorization. const SCHEME: &'static str;
/// Try to decode the credentials from the `HeaderValue`. /// /// The `SCHEME` will be the first part of the `value`. fn decode(value: &HeaderValue) -> Option<Self>;
/// Encode the credentials to a `HeaderValue`. /// /// The `SCHEME` must be the first part of the `value`. fn encode(&self) -> HeaderValue;
}
#[cfg(test)] mod tests { usesuper::super::{test_decode, test_encode}; usesuper::{Authorization, Basic, Bearer}; use http::header::HeaderMap; use HeaderMapExt;
#[test] fn basic_encode() { let auth = Authorization::basic("Aladdin", "open sesame"); let headers = test_encode(auth);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.