oauth2_broker/provider/descriptor/
grant.rs1use crate::_prelude::*;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum GrantType {
8 AuthorizationCode,
10 RefreshToken,
12 ClientCredentials,
14}
15impl GrantType {
16 pub fn as_str(self) -> &'static str {
18 match self {
19 GrantType::AuthorizationCode => "authorization_code",
20 GrantType::RefreshToken => "refresh_token",
21 GrantType::ClientCredentials => "client_credentials",
22 }
23 }
24}
25impl Display for GrantType {
26 fn fmt(&self, f: &mut Formatter) -> FmtResult {
27 f.write_str(self.as_str())
28 }
29}
30
31#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
33pub struct SupportedGrants {
34 pub authorization_code: bool,
36 pub refresh_token: bool,
38 pub client_credentials: bool,
40}
41impl SupportedGrants {
42 pub fn supports(self, grant: GrantType) -> bool {
44 match grant {
45 GrantType::AuthorizationCode => self.authorization_code,
46 GrantType::RefreshToken => self.refresh_token,
47 GrantType::ClientCredentials => self.client_credentials,
48 }
49 }
50
51 pub fn enable(mut self, grant: GrantType) -> Self {
53 match grant {
54 GrantType::AuthorizationCode => self.authorization_code = true,
55 GrantType::RefreshToken => self.refresh_token = true,
56 GrantType::ClientCredentials => self.client_credentials = true,
57 }
58
59 self
60 }
61
62 pub fn is_empty(self) -> bool {
64 !self.authorization_code && !self.refresh_token && !self.client_credentials
65 }
66}