@@ -4,25 +4,24 @@ extern crate failure;
4
4
extern crate serde_derive;
5
5
extern crate log;
6
6
use azure_sdk_core:: errors:: AzureError ;
7
- use futures:: future:: { done, ok, Future } ;
8
7
use log:: debug;
9
8
use oauth2:: basic:: BasicClient ;
10
- use oauth2:: reqwest:: http_client ;
9
+ use oauth2:: reqwest:: async_http_client ;
11
10
use oauth2:: {
12
- AuthType , AuthUrl , AuthorizationCode , ClientId , ClientSecret , CsrfToken , PkceCodeChallenge , PkceCodeVerifier , RedirectUrl , TokenUrl ,
11
+ AuthType , AuthUrl , AuthorizationCode , ClientId , ClientSecret , CsrfToken , PkceCodeChallenge ,
12
+ PkceCodeVerifier , RedirectUrl , TokenUrl ,
13
13
} ;
14
14
use std:: str:: FromStr ;
15
15
use url:: form_urlencoded;
16
16
use url:: Url ;
17
17
mod login_response;
18
- use azure_sdk_core:: perform_http_request;
19
- use http:: status:: StatusCode ;
20
- use hyper:: { Body , Client , Request } ;
21
18
pub use login_response:: * ;
22
19
use std:: sync:: Arc ;
23
20
pub mod errors;
24
21
mod naive_server;
22
+ use futures:: compat:: Future01CompatExt ;
25
23
pub use naive_server:: naive_server;
24
+ use reqwest;
26
25
27
26
#[ derive( Debug ) ]
28
27
pub struct AuthObj {
@@ -32,13 +31,26 @@ pub struct AuthObj {
32
31
pub pkce_code_verifier : PkceCodeVerifier ,
33
32
}
34
33
35
- pub fn authorize_delegate ( client_id : ClientId , client_secret : ClientSecret , tenant_id : & str , redirect_url : Url , resource : & str ) -> AuthObj {
34
+ pub fn authorize_delegate (
35
+ client_id : ClientId ,
36
+ client_secret : ClientSecret ,
37
+ tenant_id : & str ,
38
+ redirect_url : Url ,
39
+ resource : & str ,
40
+ ) -> AuthObj {
36
41
let auth_url = AuthUrl :: new (
37
- Url :: parse ( & format ! ( "https://login.microsoftonline.com/{}/oauth2/authorize" , tenant_id) )
38
- . expect ( "Invalid authorization endpoint URL" ) ,
42
+ Url :: parse ( & format ! (
43
+ "https://login.microsoftonline.com/{}/oauth2/authorize" ,
44
+ tenant_id
45
+ ) )
46
+ . expect ( "Invalid authorization endpoint URL" ) ,
39
47
) ;
40
48
let token_url = TokenUrl :: new (
41
- Url :: parse ( & format ! ( "https://login.microsoftonline.com/{}/oauth2/v2.0/token" , tenant_id) ) . expect ( "Invalid token endpoint URL" ) ,
49
+ Url :: parse ( & format ! (
50
+ "https://login.microsoftonline.com/{}/oauth2/v2.0/token" ,
51
+ tenant_id
52
+ ) )
53
+ . expect ( "Invalid token endpoint URL" ) ,
42
54
) ;
43
55
44
56
// Set up the config for the Microsoft Graph OAuth2 process.
@@ -67,56 +79,59 @@ pub fn authorize_delegate(client_id: ClientId, client_secret: ClientSecret, tena
67
79
}
68
80
}
69
81
70
- pub fn exchange (
82
+ pub async fn exchange (
71
83
auth_obj : AuthObj ,
72
84
code : AuthorizationCode ,
73
85
) -> Result <
74
86
oauth2:: StandardTokenResponse < oauth2:: EmptyExtraTokenFields , oauth2:: basic:: BasicTokenType > ,
75
- oauth2:: RequestTokenError < oauth2:: reqwest:: Error , oauth2:: StandardErrorResponse < oauth2:: basic:: BasicErrorResponseType > > ,
87
+ oauth2:: RequestTokenError <
88
+ oauth2:: reqwest:: Error ,
89
+ oauth2:: StandardErrorResponse < oauth2:: basic:: BasicErrorResponseType > ,
90
+ > ,
76
91
> {
77
92
// Exchange the code with a token.
78
93
let token = auth_obj
79
94
. client
80
95
. exchange_code ( code)
81
96
// Send the PKCE code verifier in the token request
82
97
. set_pkce_verifier ( auth_obj. pkce_code_verifier )
83
- . request ( http_client) ;
98
+ . request_async ( async_http_client)
99
+ . compat ( )
100
+ . await ?;
84
101
85
102
debug ! ( "MS Graph returned the following token:\n {:?}\n " , token) ;
86
- token
103
+ Ok ( token)
87
104
}
88
105
89
- pub fn authorize_non_interactive (
90
- client : Arc < Client < hyper_rustls :: HttpsConnector < hyper :: client :: HttpConnector > > > ,
106
+ pub async fn authorize_non_interactive (
107
+ client : Arc < reqwest :: Client > ,
91
108
// grant_type: &str, fixed on "client_credentials",
92
109
client_id : & oauth2:: ClientId ,
93
110
client_secret : & oauth2:: ClientSecret ,
94
111
resource : & str ,
95
112
tenant_id : & str ,
96
- ) -> impl Future < Item = LoginResponse , Error = AzureError > {
113
+ ) -> Result < LoginResponse , AzureError > {
97
114
let encoded: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
98
115
. append_pair ( "grant_type" , "client_credentials" )
99
116
. append_pair ( "client_id" , client_id. as_str ( ) )
100
117
. append_pair ( "client_secret" , client_secret. secret ( ) )
101
118
. append_pair ( "resource" , resource)
102
119
. finish ( ) ;
103
120
104
- let uri = format ! ( "https://login.microsoftonline.com/{}/oauth2/token" , tenant_id) ;
121
+ let url = url:: Url :: parse ( & format ! (
122
+ "https://login.microsoftonline.com/{}/oauth2/token" ,
123
+ tenant_id
124
+ ) )
125
+ . map_err ( |error| AzureError :: GenericErrorWithText ( error. to_string ( ) ) ) ?;
126
+
127
+ let resp = client
128
+ . post ( url)
129
+ . header ( "ContentType" , "Application / WwwFormUrlEncoded" )
130
+ . body ( encoded)
131
+ . send ( )
132
+ . await ?
133
+ . text ( )
134
+ . await ?;
105
135
106
- done (
107
- Request :: builder ( )
108
- . method ( "POST" )
109
- . header ( "ContentType" , "Application / WwwFormUrlEncoded" )
110
- . uri ( uri)
111
- . body ( Body :: from ( encoded) ) ,
112
- )
113
- . from_err ( )
114
- . and_then ( move |request| {
115
- perform_http_request ( & client, request, StatusCode :: OK ) . and_then ( |resp| {
116
- done ( LoginResponse :: from_str ( & resp) ) . from_err ( ) . and_then ( |r| {
117
- debug ! ( "{:?}" , r) ;
118
- ok ( r)
119
- } )
120
- } )
121
- } )
136
+ Ok ( LoginResponse :: from_str ( & resp) ?)
122
137
}
0 commit comments