@@ -7,43 +7,48 @@ use std::time::Duration;
77use anyhow:: Result ;
88use oauth2:: basic:: BasicClient ;
99use oauth2:: basic:: { BasicErrorResponseType , BasicTokenType } ;
10- use oauth2:: devicecode:: { DeviceCodeErrorResponseType , StandardDeviceAuthorizationResponse } ;
11- use oauth2:: reqwest:: async_http_client;
1210use oauth2:: {
13- AuthType , AuthUrl , ClientId , DeviceAuthorizationUrl , EmptyExtraTokenFields , RequestTokenError ,
14- Scope , StandardErrorResponse , StandardTokenResponse , TokenUrl ,
11+ AuthType , AuthUrl , ClientId , DeviceAuthorizationUrl , EmptyExtraTokenFields , EndpointNotSet ,
12+ EndpointSet , RequestTokenError , Scope , StandardErrorResponse , StandardTokenResponse , TokenUrl ,
1513} ;
14+ use oauth2:: { DeviceCodeErrorResponseType , StandardDeviceAuthorizationResponse } ;
1615use rfd_sdk:: types:: OAuthProviderInfo ;
1716
18- type DeviceAuthorizationCodeError = RequestTokenError <
19- oauth2:: reqwest:: Error < reqwest:: Error > ,
20- StandardErrorResponse < BasicErrorResponseType > ,
21- > ;
22- type DeviceCodeExchangeError = RequestTokenError <
23- oauth2:: reqwest:: Error < reqwest:: Error > ,
24- StandardErrorResponse < DeviceCodeErrorResponseType > ,
17+ type DeviceClient = BasicClient <
18+ // HasAuthUrl
19+ EndpointSet ,
20+ // HasDeviceAuthUrl
21+ EndpointSet ,
22+ // HasIntrospectionUrl
23+ EndpointNotSet ,
24+ // HasRevocationUrl
25+ EndpointNotSet ,
26+ // HasTokenUrl
27+ EndpointSet ,
2528> ;
2629
2730pub struct DeviceOAuth {
28- client : BasicClient ,
31+ client : DeviceClient ,
32+ http : reqwest:: Client ,
2933 scopes : Vec < String > ,
3034}
3135
3236impl DeviceOAuth {
3337 pub fn new ( provider : OAuthProviderInfo ) -> Result < Self > {
3438 let device_auth_url = DeviceAuthorizationUrl :: new ( provider. device_code_endpoint ) ?;
3539
36- let client = BasicClient :: new (
37- ClientId :: new ( provider. client_id ) ,
38- None ,
39- AuthUrl :: new ( provider. auth_url_endpoint ) ?,
40- Some ( TokenUrl :: new ( provider. token_endpoint ) ?) ,
41- )
42- . set_auth_type ( AuthType :: RequestBody )
43- . set_device_authorization_url ( device_auth_url) ;
40+ let client = BasicClient :: new ( ClientId :: new ( provider. client_id ) )
41+ . set_auth_uri ( AuthUrl :: new ( provider. auth_url_endpoint ) ?)
42+ . set_auth_type ( AuthType :: RequestBody )
43+ . set_token_uri ( TokenUrl :: new ( provider. token_endpoint ) ?)
44+ . set_device_authorization_url ( device_auth_url) ;
4445
4546 Ok ( Self {
4647 client,
48+ http : reqwest:: ClientBuilder :: new ( )
49+ . redirect ( reqwest:: redirect:: Policy :: none ( ) )
50+ . build ( )
51+ . unwrap ( ) ,
4752 scopes : provider. scopes ,
4853 } )
4954 }
@@ -56,29 +61,20 @@ impl DeviceOAuth {
5661 . client
5762 . exchange_device_access_token ( & details)
5863 . set_max_backoff_interval ( details. interval ( ) )
59- . request_async (
60- async_http_client,
61- tokio:: time:: sleep,
62- Some ( details. expires_in ( ) ) ,
63- )
64+ . request_async ( & self . http , tokio:: time:: sleep, Some ( details. expires_in ( ) ) )
6465 . await ;
6566
6667 Ok ( token?)
6768 }
6869
6970 pub async fn get_device_authorization ( & self ) -> Result < StandardDeviceAuthorizationResponse > {
70- let mut req = self
71- . client
72- . exchange_device_code ( )
73- // This can be unwrapped as the the device url is configured during the creation of a
74- // GoogleDeviceAuth
75- . unwrap ( ) ;
71+ let mut req = self . client . exchange_device_code ( ) ;
7672
7773 for scope in & self . scopes {
7874 req = req. add_scope ( Scope :: new ( scope. to_string ( ) ) ) ;
7975 }
8076
81- let res = req. request_async ( async_http_client ) . await ;
77+ let res = req. request_async ( & self . http ) . await ;
8278
8379 Ok ( res?)
8480 }
0 commit comments