usecrate::b64; usecrate::error::*; usecrate::mac::Mac; use base64::Engine; use std::borrow::Cow; use std::str; use std::str::FromStr; use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// A Bewit is a piece of data attached to a GET request that functions in place of a Hawk /// Authentication header. It contains an id, a timestamp, a MAC, and an optional `ext` value. /// These are available using accessor functions. #[derive(Clone, Debug, PartialEq)] pubstruct Bewit<'a> {
id: Cow<'a, str>,
exp: SystemTime,
mac: Cow<'a, Mac>,
ext: Option<Cow<'a, str>>,
}
impl<'a> Bewit<'a> { /// Create a new Bewit with the given values. /// /// See Request.make_bewit for an easier way to make a Bewit pubfn new(id: &'a str, exp: SystemTime, mac: Mac, ext: Option<&'>a str>) -> Bewit<'a> {
Bewit {
id: Cow::Borrowed(id),
exp,
mac: Cow::Owned(mac),
ext: ext.map(Cow::Borrowed),
}
}
/// Generate the fully-encoded string for this Bewit pubfn to_str(&self) -> String { let raw = format!( "{}\\{}\\{}\\{}", self.id, self.exp
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
b64::STANDARD_ENGINE.encode(self.mac.as_ref()), matchself.ext {
Some(ref cow) => cow.as_ref(),
None => "",
}
);
b64::BEWIT_ENGINE.encode(raw)
}
/// Get the Bewit's client identifier pubfn id(&self) -> &str { self.id.as_ref()
}
/// Get the expiration time of the bewit pubfn exp(&self) -> SystemTime { self.exp
}
/// Get the MAC included in the Bewit pubfn mac(&self) -> &Mac { self.mac.as_ref()
}
impl<'a> FromStr for Bewit<'a> { type Err = Error; fn from_str(bewit: &str) -> Result<Bewit<'a>> { let bewit = b64::BEWIT_ENGINE
.decode(bewit)
.map_err(Error::from_base64_error)?;
let parts: Vec<&[u8]> = bewit.split(|c| *c == BACKSLASH).collect(); if parts.len() != 4 { return Err(InvalidBewit::Format.into());
}
let id = String::from_utf8(parts[0].to_vec()).map_err(|_| InvalidBewit::Id)?;
let exp = str::from_utf8(parts[1]).map_err(|_| InvalidBewit::Exp)?; let exp = u64::from_str(exp).map_err(|_| InvalidBewit::Exp)?; let exp = UNIX_EPOCH + Duration::new(exp, 0);
let mac = str::from_utf8(parts[2]).map_err(|_| InvalidBewit::Mac)?; let mac = Mac::from(
b64::STANDARD_ENGINE
.decode(mac)
.map_err(|_| InvalidBewit::Mac)?,
);
let ext = match parts[3].len() { 0 => None,
_ => Some(Cow::Owned(
String::from_utf8(parts[3].to_vec()).map_err(|_| InvalidBewit::Ext)?,
)),
};
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.