oauth2/
basic.rs

1use std::fmt::Error as FormatterError;
2use std::fmt::{Debug, Display, Formatter};
3
4use super::{
5    Client, EmptyExtraTokenFields, ErrorResponseType, RequestTokenError, StandardErrorResponse,
6    StandardTokenResponse, TokenType,
7};
8use crate::{
9    revocation::{RevocationErrorResponseType, StandardRevocableToken},
10    StandardTokenIntrospectionResponse,
11};
12
13///
14/// Basic OAuth2 client specialization, suitable for most applications.
15///
16pub type BasicClient = Client<
17    BasicErrorResponse,
18    BasicTokenResponse,
19    BasicTokenType,
20    BasicTokenIntrospectionResponse,
21    StandardRevocableToken,
22    BasicRevocationErrorResponse,
23>;
24
25///
26/// Basic OAuth2 authorization token types.
27///
28#[derive(Clone, Debug, PartialEq)]
29pub enum BasicTokenType {
30    ///
31    /// Bearer token
32    /// ([OAuth 2.0 Bearer Tokens - RFC 6750](https://tools.ietf.org/html/rfc6750)).
33    ///
34    Bearer,
35    ///
36    /// MAC ([OAuth 2.0 Message Authentication Code (MAC)
37    /// Tokens](https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-05)).
38    ///
39    Mac,
40    ///
41    /// An extension not defined by RFC 6749.
42    ///
43    Extension(String),
44}
45impl BasicTokenType {
46    fn from_str(s: &str) -> Self {
47        match s {
48            "bearer" => BasicTokenType::Bearer,
49            "mac" => BasicTokenType::Mac,
50            ext => BasicTokenType::Extension(ext.to_string()),
51        }
52    }
53}
54impl AsRef<str> for BasicTokenType {
55    fn as_ref(&self) -> &str {
56        match *self {
57            BasicTokenType::Bearer => "bearer",
58            BasicTokenType::Mac => "mac",
59            BasicTokenType::Extension(ref ext) => ext.as_str(),
60        }
61    }
62}
63impl<'de> serde::Deserialize<'de> for BasicTokenType {
64    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
65    where
66        D: serde::de::Deserializer<'de>,
67    {
68        let variant_str = String::deserialize(deserializer)?;
69        Ok(Self::from_str(&variant_str))
70    }
71}
72impl serde::ser::Serialize for BasicTokenType {
73    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
74    where
75        S: serde::ser::Serializer,
76    {
77        serializer.serialize_str(self.as_ref())
78    }
79}
80impl TokenType for BasicTokenType {}
81
82///
83/// Basic OAuth2 token response.
84///
85pub type BasicTokenResponse = StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>;
86
87///
88/// Basic OAuth2 token introspection response.
89///
90pub type BasicTokenIntrospectionResponse =
91    StandardTokenIntrospectionResponse<EmptyExtraTokenFields, BasicTokenType>;
92
93///
94/// Basic access token error types.
95///
96/// These error types are defined in
97/// [Section 5.2 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-5.2).
98///
99#[derive(Clone, PartialEq)]
100pub enum BasicErrorResponseType {
101    ///
102    /// Client authentication failed (e.g., unknown client, no client authentication included,
103    /// or unsupported authentication method).
104    ///
105    InvalidClient,
106    ///
107    /// The provided authorization grant (e.g., authorization code, resource owner credentials)
108    /// or refresh token is invalid, expired, revoked, does not match the redirection URI used
109    /// in the authorization request, or was issued to another client.
110    ///
111    InvalidGrant,
112    ///
113    /// The request is missing a required parameter, includes an unsupported parameter value
114    /// (other than grant type), repeats a parameter, includes multiple credentials, utilizes
115    /// more than one mechanism for authenticating the client, or is otherwise malformed.
116    ///
117    InvalidRequest,
118    ///
119    /// The requested scope is invalid, unknown, malformed, or exceeds the scope granted by the
120    /// resource owner.
121    ///
122    InvalidScope,
123    ///
124    /// The authenticated client is not authorized to use this authorization grant type.
125    ///
126    UnauthorizedClient,
127    ///
128    /// The authorization grant type is not supported by the authorization server.
129    ///
130    UnsupportedGrantType,
131    ///
132    /// An extension not defined by RFC 6749.
133    ///
134    Extension(String),
135}
136impl BasicErrorResponseType {
137    pub(crate) fn from_str(s: &str) -> Self {
138        match s {
139            "invalid_client" => BasicErrorResponseType::InvalidClient,
140            "invalid_grant" => BasicErrorResponseType::InvalidGrant,
141            "invalid_request" => BasicErrorResponseType::InvalidRequest,
142            "invalid_scope" => BasicErrorResponseType::InvalidScope,
143            "unauthorized_client" => BasicErrorResponseType::UnauthorizedClient,
144            "unsupported_grant_type" => BasicErrorResponseType::UnsupportedGrantType,
145            ext => BasicErrorResponseType::Extension(ext.to_string()),
146        }
147    }
148}
149impl AsRef<str> for BasicErrorResponseType {
150    fn as_ref(&self) -> &str {
151        match *self {
152            BasicErrorResponseType::InvalidClient => "invalid_client",
153            BasicErrorResponseType::InvalidGrant => "invalid_grant",
154            BasicErrorResponseType::InvalidRequest => "invalid_request",
155            BasicErrorResponseType::InvalidScope => "invalid_scope",
156            BasicErrorResponseType::UnauthorizedClient => "unauthorized_client",
157            BasicErrorResponseType::UnsupportedGrantType => "unsupported_grant_type",
158            BasicErrorResponseType::Extension(ref ext) => ext.as_str(),
159        }
160    }
161}
162impl<'de> serde::Deserialize<'de> for BasicErrorResponseType {
163    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
164    where
165        D: serde::de::Deserializer<'de>,
166    {
167        let variant_str = String::deserialize(deserializer)?;
168        Ok(Self::from_str(&variant_str))
169    }
170}
171impl serde::ser::Serialize for BasicErrorResponseType {
172    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
173    where
174        S: serde::ser::Serializer,
175    {
176        serializer.serialize_str(self.as_ref())
177    }
178}
179impl ErrorResponseType for BasicErrorResponseType {}
180impl Debug for BasicErrorResponseType {
181    fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
182        Display::fmt(self, f)
183    }
184}
185
186impl Display for BasicErrorResponseType {
187    fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
188        write!(f, "{}", self.as_ref())
189    }
190}
191
192///
193/// Error response specialization for basic OAuth2 implementation.
194///
195pub type BasicErrorResponse = StandardErrorResponse<BasicErrorResponseType>;
196
197///
198/// Token error specialization for basic OAuth2 implementation.
199///
200pub type BasicRequestTokenError<RE> = RequestTokenError<RE, BasicErrorResponse>;
201
202///
203/// Revocation error response specialization for basic OAuth2 implementation.
204///
205pub type BasicRevocationErrorResponse = StandardErrorResponse<RevocationErrorResponseType>;