@@ -16,7 +16,8 @@ use dino_park_gate::scope::ScopeAndUser;
1616use failure:: Error ;
1717use log:: info;
1818use oauth2:: basic:: BasicClient ;
19- use oauth2:: prelude:: * ;
19+ use oauth2:: reqwest:: async_http_client;
20+ use oauth2:: AsyncCodeTokenRequest ;
2021use oauth2:: AuthUrl ;
2122use oauth2:: AuthorizationCode ;
2223use oauth2:: ClientId ;
@@ -26,11 +27,12 @@ use oauth2::RedirectUrl;
2627use oauth2:: TokenResponse ;
2728use oauth2:: TokenUrl ;
2829use reqwest:: Client ;
30+ use serde:: Deserialize ;
31+ use serde:: Serialize ;
2932use std:: sync:: Arc ;
3033use std:: sync:: RwLock ;
3134use std:: time:: Duration ;
3235use ttl_cache:: TtlCache ;
33- use url:: Url ;
3436
3537const AUTH_URL : & str = "https://github.com/login/oauth/authorize" ;
3638const TOKEN_URL : & str = "https://github.com/login/oauth/access_token" ;
@@ -59,7 +61,7 @@ pub struct GitHubUser {
5961
6062async fn id_to_username (
6163 id : web:: Path < String > ,
62- gtihub_auth_params : web:: Data < GitHub > ,
64+ github_auth_params : web:: Data < GitHub > ,
6365 cache : web:: Data < Arc < RwLock < TtlCache < String , String > > > > ,
6466) -> Result < HttpResponse , Error > {
6567 if let Some ( username) = cache. read ( ) . ok ( ) . and_then ( |c| c. get ( & * id) . cloned ( ) ) {
@@ -71,8 +73,8 @@ async fn id_to_username(
7173 let res = Client :: default ( )
7274 . get ( & format ! ( "{}/{}" , USER_URL , id) )
7375 . basic_auth (
74- & gtihub_auth_params . client_id ,
75- Some ( & gtihub_auth_params . client_secret ) ,
76+ & github_auth_params . client_id ,
77+ Some ( & github_auth_params . client_secret ) ,
7678 )
7779 . header ( http:: header:: USER_AGENT , "whoami" )
7880 . send ( )
@@ -90,7 +92,7 @@ async fn id_to_username(
9092}
9193
9294async fn redirect ( client : web:: Data < Arc < BasicClient > > , session : Session ) -> impl Responder {
93- let ( authorize_url, csrf_state) = client. authorize_url ( CsrfToken :: new_random) ;
95+ let ( authorize_url, csrf_state) = client. authorize_url ( CsrfToken :: new_random) . url ( ) ;
9496 info ! ( "settting: {}" , csrf_state. secret( ) ) ;
9597 session
9698 . set ( "csrf_state" , csrf_state. secret ( ) . clone ( ) )
@@ -123,7 +125,10 @@ async fn auth<T: AsyncCisClientTrait + 'static>(
123125 . header ( http:: header:: LOCATION , "/e?identityAdded=error" )
124126 . finish ( ) ) ;
125127 }
126- let token_res = client. exchange_code ( code) ;
128+ let token_res = client
129+ . exchange_code ( code)
130+ . request_async ( async_http_client)
131+ . await ;
127132
128133 if let Ok ( token) = token_res {
129134 let get = cis_client. clone ( ) ;
@@ -167,8 +172,10 @@ pub fn github_app<T: AsyncCisClientTrait + 'static>(
167172) -> impl HttpServiceFactory {
168173 let github_client_id = ClientId :: new ( github. client_id . clone ( ) ) ;
169174 let github_client_secret = ClientSecret :: new ( github. client_secret . clone ( ) ) ;
170- let auth_url = AuthUrl :: new ( Url :: parse ( AUTH_URL ) . expect ( "Invalid authorization endpoint URL" ) ) ;
171- let token_url = TokenUrl :: new ( Url :: parse ( TOKEN_URL ) . expect ( "Invalid token endpoint URL" ) ) ;
175+ let auth_url = AuthUrl :: new ( AUTH_URL . to_string ( ) ) . expect ( "Invalid authorization endpoint URL" ) ;
176+ let token_url = TokenUrl :: new ( TOKEN_URL . to_string ( ) ) . expect ( "Invalid token endpoint URL" ) ;
177+ let redirect_url = RedirectUrl :: new ( format ! ( "https://{}/whoami/github/auth" , whoami. domain) )
178+ . expect ( "Invalid redirect URL" ) ;
172179
173180 let client = Arc :: new (
174181 BasicClient :: new (
@@ -177,10 +184,7 @@ pub fn github_app<T: AsyncCisClientTrait + 'static>(
177184 auth_url,
178185 Some ( token_url) ,
179186 )
180- . set_redirect_url ( RedirectUrl :: new (
181- Url :: parse ( & format ! ( "https://{}/whoami/github/auth" , whoami. domain) )
182- . expect ( "Invalid redirect URL" ) ,
183- ) ) ,
187+ . set_redirect_url ( redirect_url) ,
184188 ) ;
185189
186190 web:: scope ( "/github/" )
0 commit comments