eio_okta_api/authorization/
ssws.rs

1use headers::{authorization::Credentials, Authorization};
2use http::HeaderValue;
3use std::str::FromStr;
4
5#[derive(Clone)]
6pub struct SSWS {
7  token: HeaderValueString,
8}
9
10impl FromStr for SSWS {
11  type Err = <HeaderValue as FromStr>::Err;
12  fn from_str(src: &str) -> Result<Self, Self::Err> {
13    let src = src.strip_prefix(<Self as Credentials>::SCHEME).unwrap_or(src).trim();
14
15    let mut token = HeaderValueString::from_str(src)?;
16    token.set_sensitive(true);
17
18    Ok(Self { token })
19  }
20}
21
22impl Credentials for SSWS {
23  const SCHEME: &'static str = "SSWS";
24
25  fn decode(value: &HeaderValue) -> Option<Self> {
26    match value.to_str() {
27      Ok(s) if s.is_ascii() && s.starts_with(Self::SCHEME) => Self::from_str(s).ok(),
28      _ => None,
29    }
30  }
31
32  fn encode(&self) -> HeaderValue {
33    HeaderValue::from_str(&[Self::SCHEME, self.token.as_ref()].join(" ")).unwrap()
34  }
35}
36
37impl From<SSWS> for Authorization<SSWS> {
38  fn from(ssws: SSWS) -> Self {
39    Self(ssws)
40  }
41}
42
43#[derive(Clone)]
44struct HeaderValueString(HeaderValue);
45
46impl HeaderValueString {
47  fn set_sensitive(&mut self, val: bool) {
48    self.0.set_sensitive(val)
49  }
50}
51
52impl FromStr for HeaderValueString {
53  type Err = <HeaderValue as FromStr>::Err;
54  fn from_str(s: &str) -> Result<Self, Self::Err> {
55    HeaderValue::from_str(s).map(Self)
56  }
57}
58
59impl TryFrom<HeaderValue> for HeaderValueString {
60  type Error = http::header::ToStrError;
61  fn try_from(value: HeaderValue) -> Result<Self, Self::Error> {
62    value.to_str()?;
63    Ok(Self(value))
64  }
65}
66
67impl From<HeaderValueString> for HeaderValue {
68  fn from(value: HeaderValueString) -> Self {
69    value.0
70  }
71}
72
73impl AsRef<str> for HeaderValueString {
74  fn as_ref(&self) -> &str {
75    self.0.to_str().unwrap()
76  }
77}