@@ -103,11 +103,7 @@ impl OAuth2ProviderConfig {
103103 let token_url = std:: env:: var ( format ! ( "OAUTH2_{prefix}_TOKEN_URL" ) )
104104 . unwrap_or_else ( |_| provider. default_token_url ( ) . to_string ( ) ) ;
105105
106- let scopes = provider
107- . default_scopes ( )
108- . into_iter ( )
109- . map ( String :: from)
110- . collect ( ) ;
106+ let scopes = provider. default_scopes ( ) . into_iter ( ) . map ( String :: from) . collect ( ) ;
111107
112108 Some ( Self {
113109 provider,
@@ -125,8 +121,7 @@ impl OAuth2ProviderConfig {
125121 let client = BasicClient :: new (
126122 ClientId :: new ( self . client_id . clone ( ) ) ,
127123 Some ( ClientSecret :: new ( self . client_secret . clone ( ) ) ) ,
128- AuthUrl :: new ( self . auth_url . clone ( ) )
129- . map_err ( |e| format ! ( "Invalid auth URL: {e}" ) ) ?,
124+ AuthUrl :: new ( self . auth_url . clone ( ) ) . map_err ( |e| format ! ( "Invalid auth URL: {e}" ) ) ?,
130125 Some (
131126 TokenUrl :: new ( self . token_url . clone ( ) )
132127 . map_err ( |e| format ! ( "Invalid token URL: {e}" ) ) ?,
@@ -144,10 +139,7 @@ impl OAuth2ProviderConfig {
144139#[ derive( Debug , Clone ) ]
145140pub enum OAuth2Result {
146141 /// Successfully verified — provider confirmed the account.
147- Verified {
148- provider : OAuth2Provider ,
149- account_id : String ,
150- } ,
142+ Verified { provider : OAuth2Provider , account_id : String } ,
151143 /// Verification failed (e.g., invalid token, denied).
152144 Failed ( String ) ,
153145 /// Provider is unavailable (credentials missing or service unreachable).
@@ -187,11 +179,7 @@ pub fn generate_auth_url(
187179/// This function performs the full OAuth2 authorization code exchange using
188180/// the `oauth2` crate, then queries the provider's user-info endpoint to
189181/// obtain the account identifier.
190- pub fn exchange_code (
191- provider : OAuth2Provider ,
192- redirect_uri : & str ,
193- code : & str ,
194- ) -> OAuth2Result {
182+ pub fn exchange_code ( provider : OAuth2Provider , redirect_uri : & str , code : & str ) -> OAuth2Result {
195183 let config = match OAuth2ProviderConfig :: from_env ( provider, redirect_uri) {
196184 Some ( c) => c,
197185 None => {
@@ -211,9 +199,8 @@ pub fn exchange_code(
211199
212200 // Exchange code for token using the oauth2 crate's built-in blocking HTTP client
213201 let http_client = oauth2:: reqwest:: http_client;
214- let token_result = client
215- . exchange_code ( oauth2:: AuthorizationCode :: new ( code. to_string ( ) ) )
216- . request ( http_client) ;
202+ let token_result =
203+ client. exchange_code ( oauth2:: AuthorizationCode :: new ( code. to_string ( ) ) ) . request ( http_client) ;
217204
218205 let token_response = match token_result {
219206 Ok ( t) => t,
@@ -224,10 +211,7 @@ pub fn exchange_code(
224211
225212 // Fetch user info from provider-specific endpoint
226213 match fetch_account_id ( provider, & access_token) {
227- Ok ( account_id) => OAuth2Result :: Verified {
228- provider,
229- account_id,
230- } ,
214+ Ok ( account_id) => OAuth2Result :: Verified { provider, account_id } ,
231215 Err ( e) => OAuth2Result :: Failed ( format ! ( "Failed to fetch user info: {e}" ) ) ,
232216 }
233217}
@@ -238,15 +222,9 @@ fn fetch_account_id(provider: OAuth2Provider, access_token: &str) -> Result<Stri
238222
239223 let ( url, id_field) = match provider {
240224 OAuth2Provider :: GitHub => ( "https://api.github.com/user" , "id" ) ,
241- OAuth2Provider :: Google => (
242- "https://www.googleapis.com/oauth2/v2/userinfo" ,
243- "id" ,
244- ) ,
225+ OAuth2Provider :: Google => ( "https://www.googleapis.com/oauth2/v2/userinfo" , "id" ) ,
245226 OAuth2Provider :: Twitter => ( "https://api.twitter.com/2/users/me" , "id" ) ,
246- OAuth2Provider :: Email => (
247- "https://www.googleapis.com/oauth2/v2/userinfo" ,
248- "email" ,
249- ) ,
227+ OAuth2Provider :: Email => ( "https://www.googleapis.com/oauth2/v2/userinfo" , "email" ) ,
250228 } ;
251229
252230 let response = http_client
@@ -258,22 +236,15 @@ fn fetch_account_id(provider: OAuth2Provider, access_token: &str) -> Result<Stri
258236 . map_err ( |e| format ! ( "HTTP request failed: {e}" ) ) ?;
259237
260238 if !response. status ( ) . is_success ( ) {
261- return Err ( format ! (
262- "Provider returned HTTP {}" ,
263- response. status( )
264- ) ) ;
239+ return Err ( format ! ( "Provider returned HTTP {}" , response. status( ) ) ) ;
265240 }
266241
267- let body: serde_json:: Value = response
268- . json ( )
269- . map_err ( |e| format ! ( "Failed to parse response: {e}" ) ) ?;
242+ let body: serde_json:: Value =
243+ response. json ( ) . map_err ( |e| format ! ( "Failed to parse response: {e}" ) ) ?;
270244
271245 // Twitter nests user data under "data"
272- let user_data = if provider == OAuth2Provider :: Twitter {
273- body. get ( "data" ) . unwrap_or ( & body)
274- } else {
275- & body
276- } ;
246+ let user_data =
247+ if provider == OAuth2Provider :: Twitter { body. get ( "data" ) . unwrap_or ( & body) } else { & body } ;
277248
278249 user_data
279250 . get ( id_field)
@@ -324,9 +295,7 @@ pub fn verify_oauth2(provider: OAuth2Provider, redirect_uri: &str) -> OAuth2Resu
324295 // Return as "Failed" with the auth URL — the caller needs to
325296 // redirect the user and then call exchange_code() with the code.
326297 // In a non-interactive context, we cannot complete the flow.
327- OAuth2Result :: Failed ( format ! (
328- "Authorization required. Visit: {url}"
329- ) )
298+ OAuth2Result :: Failed ( format ! ( "Authorization required. Visit: {url}" ) )
330299 }
331300 }
332301 }
@@ -348,10 +317,8 @@ mod tests {
348317 #[ test]
349318 fn config_from_env_returns_none_when_missing ( ) {
350319 // With no env vars set, config should be None
351- let config = OAuth2ProviderConfig :: from_env (
352- OAuth2Provider :: GitHub ,
353- "https://localhost/callback" ,
354- ) ;
320+ let config =
321+ OAuth2ProviderConfig :: from_env ( OAuth2Provider :: GitHub , "https://localhost/callback" ) ;
355322 // This will be None unless someone has OAUTH2_GITHUB_CLIENT_ID set
356323 if std:: env:: var ( "OAUTH2_GITHUB_CLIENT_ID" ) . is_err ( ) {
357324 assert ! ( config. is_none( ) ) ;
@@ -425,11 +392,7 @@ mod tests {
425392 #[ test]
426393 fn exchange_code_returns_unavailable_without_credentials ( ) {
427394 if std:: env:: var ( "OAUTH2_GITHUB_CLIENT_ID" ) . is_err ( ) {
428- match exchange_code (
429- OAuth2Provider :: GitHub ,
430- "https://localhost/callback" ,
431- "fake-code" ,
432- ) {
395+ match exchange_code ( OAuth2Provider :: GitHub , "https://localhost/callback" , "fake-code" ) {
433396 OAuth2Result :: ProviderUnavailable ( msg) => {
434397 assert ! ( msg. contains( "credentials not configured" ) ) ;
435398 }
@@ -441,10 +404,7 @@ mod tests {
441404 #[ test]
442405 fn generate_auth_url_returns_error_without_credentials ( ) {
443406 if std:: env:: var ( "OAUTH2_GITHUB_CLIENT_ID" ) . is_err ( ) {
444- let result = generate_auth_url (
445- OAuth2Provider :: GitHub ,
446- "https://localhost/callback" ,
447- ) ;
407+ let result = generate_auth_url ( OAuth2Provider :: GitHub , "https://localhost/callback" ) ;
448408 assert ! ( result. is_err( ) ) ;
449409 assert ! ( result. unwrap_err( ) . contains( "credentials not configured" ) ) ;
450410 }
0 commit comments