Skip to main content

keycloak_oauth/client/
device_authorization_response.rs

1use std::str::FromStr;
2
3use oauth2::{DeviceCodeErrorResponseType, StandardErrorResponse};
4
5pub enum PollDeviceCodeEvent {
6    AuthorizationPending,
7    AuthorizationDeclined,
8    BadVerificationCode,
9    ExpiredToken,
10    AccessDenied,
11    SlowDown,
12}
13
14impl PollDeviceCodeEvent {
15    pub fn as_str(&self) -> &'static str {
16        match self {
17            PollDeviceCodeEvent::AuthorizationPending => "authorization_pending",
18            PollDeviceCodeEvent::AuthorizationDeclined => "authorization_declined",
19            PollDeviceCodeEvent::BadVerificationCode => "bad_verification_code",
20            PollDeviceCodeEvent::ExpiredToken => "expired_token",
21            PollDeviceCodeEvent::AccessDenied => "access_denied",
22            PollDeviceCodeEvent::SlowDown => "slow_down",
23        }
24    }
25    pub fn as_message(&self) {
26        match self {
27            PollDeviceCodeEvent::AuthorizationPending => {
28                println!("authorization_pending, continuing")
29            }
30            PollDeviceCodeEvent::AuthorizationDeclined => {
31                println!("authorization_declined! exiting loop")
32            }
33            PollDeviceCodeEvent::BadVerificationCode => {
34                println!("bad_verification_code! continuing")
35            }
36            PollDeviceCodeEvent::ExpiredToken => println!("expired_token, exiting loop"),
37            PollDeviceCodeEvent::AccessDenied => println!("access_denied, exiting loop"),
38            PollDeviceCodeEvent::SlowDown => println!("slow_down! adding 5 sec to interval"),
39        }
40    }
41}
42
43impl From<StandardErrorResponse<DeviceCodeErrorResponseType>> for PollDeviceCodeEvent {
44    fn from(value: StandardErrorResponse<DeviceCodeErrorResponseType>) -> Self {
45        let resp = match value.error() {
46            DeviceCodeErrorResponseType::AccessDenied => PollDeviceCodeEvent::AccessDenied,
47            DeviceCodeErrorResponseType::AuthorizationPending => {
48                PollDeviceCodeEvent::AuthorizationPending
49            }
50            DeviceCodeErrorResponseType::SlowDown => PollDeviceCodeEvent::SlowDown,
51            DeviceCodeErrorResponseType::ExpiredToken => PollDeviceCodeEvent::ExpiredToken,
52            DeviceCodeErrorResponseType::Basic(basic_error_response_type) => {
53                PollDeviceCodeEvent::from_str(basic_error_response_type.to_string().as_str())
54                    .expect("parsed")
55            }
56        };
57
58        resp
59    }
60}
61
62impl FromStr for PollDeviceCodeEvent {
63    type Err = ();
64
65    fn from_str(s: &str) -> Result<Self, Self::Err> {
66        match s {
67            "authorization_pending" => Ok(PollDeviceCodeEvent::AuthorizationPending),
68            "authorization_declined" => Ok(PollDeviceCodeEvent::AuthorizationDeclined),
69            "bad_verification_code" => Ok(PollDeviceCodeEvent::BadVerificationCode),
70            "expired_token" => Ok(PollDeviceCodeEvent::ExpiredToken),
71            "access_denied" => Ok(PollDeviceCodeEvent::AccessDenied),
72            "slow_down" => Ok(PollDeviceCodeEvent::SlowDown),
73            _ => Err(()),
74        }
75    }
76}