Skip to main content

saml_rs/browser/
bindings.rs

1//! Browser binding enum wrappers.
2//!
3//! References: SAML Bindings 2.0 <https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf>.
4
5use crate::constants::Binding;
6use crate::error::SamlError;
7
8/// Browser SSO request bindings supported by the typed API.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum SsoRequestBinding {
11    /// HTTP-Redirect binding.
12    Redirect,
13    /// HTTP-POST binding.
14    Post,
15    /// HTTP-POST-SimpleSign binding.
16    SimpleSign,
17}
18
19impl SsoRequestBinding {
20    /// Convert to the raw compatibility binding.
21    pub fn as_binding(self) -> Binding {
22        match self {
23            Self::Redirect => Binding::Redirect,
24            Self::Post => Binding::Post,
25            Self::SimpleSign => Binding::SimpleSign,
26        }
27    }
28}
29
30impl From<SsoRequestBinding> for Binding {
31    fn from(value: SsoRequestBinding) -> Self {
32        value.as_binding()
33    }
34}
35
36impl TryFrom<Binding> for SsoRequestBinding {
37    type Error = SamlError;
38
39    fn try_from(value: Binding) -> Result<Self, Self::Error> {
40        match value {
41            Binding::Redirect => Ok(Self::Redirect),
42            Binding::Post => Ok(Self::Post),
43            Binding::SimpleSign => Ok(Self::SimpleSign),
44            Binding::Artifact => Err(SamlError::UndefinedBinding),
45        }
46    }
47}
48
49/// Browser SSO response bindings supported by the typed API.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub enum SsoResponseBinding {
52    /// HTTP-POST binding.
53    Post,
54    /// HTTP-POST-SimpleSign binding.
55    SimpleSign,
56}
57
58impl SsoResponseBinding {
59    /// Convert to the raw compatibility binding.
60    pub fn as_binding(self) -> Binding {
61        match self {
62            Self::Post => Binding::Post,
63            Self::SimpleSign => Binding::SimpleSign,
64        }
65    }
66}
67
68impl From<SsoResponseBinding> for Binding {
69    fn from(value: SsoResponseBinding) -> Self {
70        value.as_binding()
71    }
72}
73
74impl TryFrom<Binding> for SsoResponseBinding {
75    type Error = SamlError;
76
77    fn try_from(value: Binding) -> Result<Self, Self::Error> {
78        match value {
79            Binding::Post => Ok(Self::Post),
80            Binding::SimpleSign => Ok(Self::SimpleSign),
81            Binding::Redirect | Binding::Artifact => Err(SamlError::UndefinedBinding),
82        }
83    }
84}
85
86/// Single Logout bindings supported by the typed API.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88pub enum LogoutBinding {
89    /// HTTP-Redirect binding.
90    Redirect,
91    /// HTTP-POST binding.
92    Post,
93    /// HTTP-POST-SimpleSign binding.
94    SimpleSign,
95}
96
97impl LogoutBinding {
98    /// Convert to the raw compatibility binding.
99    pub fn as_binding(self) -> Binding {
100        match self {
101            Self::Redirect => Binding::Redirect,
102            Self::Post => Binding::Post,
103            Self::SimpleSign => Binding::SimpleSign,
104        }
105    }
106}
107
108impl From<LogoutBinding> for Binding {
109    fn from(value: LogoutBinding) -> Self {
110        value.as_binding()
111    }
112}
113
114impl TryFrom<Binding> for LogoutBinding {
115    type Error = SamlError;
116
117    fn try_from(value: Binding) -> Result<Self, Self::Error> {
118        match value {
119            Binding::Redirect => Ok(Self::Redirect),
120            Binding::Post => Ok(Self::Post),
121            Binding::SimpleSign => Ok(Self::SimpleSign),
122            Binding::Artifact => Err(SamlError::UndefinedBinding),
123        }
124    }
125}