oauth2/
basic.rs

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