File tree Expand file tree Collapse file tree 2 files changed +55
-4
lines changed
Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -163,4 +163,52 @@ impl GithubClient {
163163 ApiError :: InternalError
164164 } ) ?)
165165 }
166+
167+ pub async fn get_installation ( & self , token : & str ) -> Result < GitHubFetchedUser , ApiError > {
168+ let client = Client :: new ( ) ;
169+ let resp = match client
170+ . get ( "https://api.github.com/installation/repositories" )
171+ . header ( "Accept" , HeaderValue :: from_str ( "application/json" ) . unwrap ( ) )
172+ . header ( "User-Agent" , "geode_index" )
173+ . bearer_auth ( token)
174+ . send ( )
175+ . await
176+ {
177+ Err ( e) => {
178+ log:: info!( "{}" , e) ;
179+ return Err ( ApiError :: InternalError ) ;
180+ }
181+ Ok ( r) => r,
182+ } ;
183+
184+ if !resp. status ( ) . is_success ( ) {
185+ return Err ( ApiError :: InternalError ) ;
186+ }
187+
188+ let body = match resp. json :: < serde_json:: Value > ( ) . await {
189+ Err ( e) => {
190+ log:: error!( "{}" , e) ;
191+ return Err ( ApiError :: InternalError ) ;
192+ }
193+ Ok ( b) => b,
194+ } ;
195+
196+ let repos = match body. get ( "repositories" ) . and_then ( |r| r. as_array ( ) ) {
197+ None => {
198+ return Err ( ApiError :: InternalError ) ;
199+ } ,
200+ Some ( r) => r,
201+ } ;
202+
203+ if repos. len ( ) != 1 {
204+ return Err ( ApiError :: InternalError ) ;
205+ }
206+
207+ let owner = repos[ 0 ] . get ( "owner" ) . ok_or ( ApiError :: InternalError ) ?. clone ( ) ;
208+
209+ serde_json:: from_value ( owner) . map_err ( |e| {
210+ log:: error!( "Failed to create GitHubFetchedUser: {}" , e) ;
211+ ApiError :: InternalError
212+ } )
213+ }
166214}
Original file line number Diff line number Diff line change @@ -139,10 +139,13 @@ pub async fn github_token_login(
139139 data. github ( ) . client_secret ( ) . to_string ( ) ,
140140 ) ;
141141
142- let user = client
143- . get_user ( & json. token )
144- . await
145- . map_err ( |_| ApiError :: BadRequest ( format ! ( "Invalid access token: {}" , json. token) ) ) ?;
142+ let user = match client. get_user ( & json. token ) . await {
143+ Err ( _) => client. get_installation ( & json. token ) . await . map_err ( |_|
144+ ApiError :: BadRequest ( format ! ( "Invalid access token: {}" , json. token) )
145+ ) ?,
146+
147+ Ok ( u) => u
148+ } ;
146149
147150 let mut pool = data. db ( ) . acquire ( ) . await . or ( Err ( ApiError :: DbAcquireError ) ) ?;
148151 let mut tx = pool. begin ( ) . await . or ( Err ( ApiError :: TransactionError ) ) ?;
You can’t perform that action at this time.
0 commit comments