1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::error;
use http_api_client::{
Client, ClientRespondEndpointError, RetryableClient,
RetryableClientRespondEndpointUntilDoneError,
};
use oauth2_core::{
device_authorization_grant::{
device_access_token_response::{
ErrorBody as DAT_RES_ErrorBody, SuccessfulBody as DAT_RES_SuccessfulBody,
},
device_authorization_response::{
ErrorBody as DA_RES_ErrorBody, UserCode, VerificationUri, VerificationUriComplete,
},
},
serde::{de::DeserializeOwned, Serialize},
types::Scope,
};
use crate::ProviderExtDeviceAuthorizationGrant;
use super::{
DeviceAccessTokenEndpoint, DeviceAccessTokenEndpointError, DeviceAuthorizationEndpoint,
DeviceAuthorizationEndpointError,
};
#[derive(Debug, Clone)]
pub struct Flow<C1, C2>
where
C1: Client,
C2: RetryableClient,
{
pub client_with_auth: C1,
pub client_with_token: C2,
}
impl<C1, C2> Flow<C1, C2>
where
C1: Client,
C2: RetryableClient,
{
pub fn new(client_with_auth: C1, client_with_token: C2) -> Self {
Self {
client_with_auth,
client_with_token,
}
}
}
impl<C1, C2> Flow<C1, C2>
where
C1: Client + Send + Sync,
C2: RetryableClient + Send + Sync,
{
pub async fn execute<SCOPE, UI>(
&self,
provider: &(dyn ProviderExtDeviceAuthorizationGrant<Scope = SCOPE> + Send + Sync),
scopes: impl Into<Option<Vec<SCOPE>>>,
user_interaction: UI,
) -> Result<DAT_RES_SuccessfulBody<SCOPE>, FlowExecuteError>
where
SCOPE: Scope + Serialize + DeserializeOwned + Send + Sync,
UI: FnOnce(UserCode, VerificationUri, Option<VerificationUriComplete>),
{
let scopes = scopes.into().or_else(|| provider.scopes_default());
let device_authorization_endpoint = DeviceAuthorizationEndpoint::new(provider, scopes);
let device_authorization_ret = self
.client_with_auth
.respond_endpoint(&device_authorization_endpoint)
.await
.map_err(|err| match err {
ClientRespondEndpointError::RespondFailed(err) => {
FlowExecuteError::DeviceAuthorizationEndpointRespondFailed(Box::new(err))
}
ClientRespondEndpointError::EndpointRenderRequestFailed(err) => {
FlowExecuteError::DeviceAuthorizationEndpointError(err)
}
ClientRespondEndpointError::EndpointParseResponseFailed(err) => {
FlowExecuteError::DeviceAuthorizationEndpointError(err)
}
})?;
let device_authorization_successful_body =
device_authorization_ret.map_err(FlowExecuteError::DeviceAuthorizationFailed)?;
user_interaction(
device_authorization_successful_body.user_code.to_owned(),
device_authorization_successful_body
.verification_uri
.to_owned(),
device_authorization_successful_body
.verification_uri_complete
.to_owned(),
);
let device_access_token_endpoint =
DeviceAccessTokenEndpoint::new(provider, device_authorization_successful_body);
let device_access_token_ret = self
.client_with_token
.respond_endpoint_until_done(&device_access_token_endpoint)
.await
.map_err(|err| match err {
RetryableClientRespondEndpointUntilDoneError::RespondFailed(err) => {
FlowExecuteError::DeviceAccessTokenEndpointRespondFailed(Box::new(err))
}
RetryableClientRespondEndpointUntilDoneError::EndpointRenderRequestFailed(err) => {
FlowExecuteError::DeviceAccessTokenEndpointError(err)
}
RetryableClientRespondEndpointUntilDoneError::EndpointParseResponseFailed(err) => {
FlowExecuteError::DeviceAccessTokenEndpointError(err)
}
RetryableClientRespondEndpointUntilDoneError::ReachedMaxRetries => {
FlowExecuteError::DeviceAccessTokenEndpointErrorWithReachedMaxRetries
}
})?;
let device_access_token_successful_body =
device_access_token_ret.map_err(FlowExecuteError::DeviceAccessTokenFailed)?;
Ok(device_access_token_successful_body)
}
}
#[derive(thiserror::Error, Debug)]
pub enum FlowExecuteError {
#[error("DeviceAuthorizationEndpointRespondFailed {0}")]
DeviceAuthorizationEndpointRespondFailed(Box<dyn error::Error + Send + Sync>),
#[error("DeviceAuthorizationEndpointError {0}")]
DeviceAuthorizationEndpointError(DeviceAuthorizationEndpointError),
#[error("DeviceAuthorizationFailed {0:?}")]
DeviceAuthorizationFailed(DA_RES_ErrorBody),
#[error("DeviceAccessTokenEndpointRespondFailed {0}")]
DeviceAccessTokenEndpointRespondFailed(Box<dyn error::Error + Send + Sync>),
#[error("DeviceAccessTokenEndpointError {0}")]
DeviceAccessTokenEndpointError(DeviceAccessTokenEndpointError),
#[error("DeviceAccessTokenEndpointErrorWithReachedMaxRetries")]
DeviceAccessTokenEndpointErrorWithReachedMaxRetries,
#[error("DeviceAccessTokenFailed {0:?}")]
DeviceAccessTokenFailed(DAT_RES_ErrorBody),
}