Skip to content

A free open source IT asset/license management system

Better handle unhandled client exception when user clicks “deny” on google auth

snipedc2429c4e597
diff --git a/app/Http/Controllers/GoogleAuthController.php b/app/Http/Controllers/GoogleAuthController.php
index 115ee4ae35..626d1a73d5 100644
--- a/app/Http/Controllers/GoogleAuthController.php
+++ b/app/Http/Controllers/GoogleAuthController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Setting;
use App\Models\User;
+use GuzzleHttp\Exception\ClientException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
@@ -32,20 +33,26 @@ class GoogleAuthController extends Controller
public function handleGoogleCallback(): RedirectResponse
{
+ // Bail before Socialite hits the token endpoint when the callback
+ // was reached without a fresh code: user hit Deny, browser back-button
+ // replay, bookmarked callback URL, or Google returned an OAuth error.
+ // Otherwise Socialite POSTs an empty code and gets a 400 that bubbles
+ // as an unhandled ClientException 500.
+ if (request()->has('error') || ! request()->has('code')) {
+ Log::debug('Google callback hit without a code (error='.request('error', 'none').')');
+
+ return redirect()->route('login')
+ ->withErrors(['username' => [trans('auth/general.google_login_failed')]]);
+ }
+
try {
$socialUser = Socialite::driver('google')->user();
Log::debug('Google user found in Google Workspace');
- } catch (InvalidStateException $exception) {
- Log::debug('Google user NOT found in Google Workspace');
+ } catch (InvalidStateException|ClientException $exception) {
+ Log::debug('Google callback error: '.$exception->getMessage());
return redirect()->route('login')
- ->withErrors(
- [
- 'username' => [
- trans('auth/general.google_login_failed'),
- ],
- ]
- );
+ ->withErrors(['username' => [trans('auth/general.google_login_failed')]]);
}
$user = User::where('username', $socialUser->getEmail())
@@ -63,7 +70,7 @@ class GoogleAuthController extends Controller
}
Log::debug('Google user '.$socialUser->getEmail().' found in Snipe-IT');
-
+
$user->avatar = $socialUser->avatar;
$user->last_login = \Carbon::now();
$user->save();
diff --git a/tests/Feature/Auth/GoogleLoginTest.php b/tests/Feature/Auth/GoogleLoginTest.php
index 7a401939e6..d33c0239dc 100644
--- a/tests/Feature/Auth/GoogleLoginTest.php
+++ b/tests/Feature/Auth/GoogleLoginTest.php
@@ -3,6 +3,9 @@
namespace Tests\Feature\Auth;
use App\Models\User;
+use GuzzleHttp\Exception\ClientException;
+use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
@@ -29,9 +32,59 @@ class GoogleLoginTest extends TestCase
$driver->shouldReceive('user')->andReturn($socialUser);
Socialite::shouldReceive('driver')->with('google')->andReturn($driver);
- $this->get(route('google.callback'))->assertRedirect(route('home'));
+ $this->get(route('google.callback', ['code' => 'google-oauth-code']))
+ ->assertRedirect(route('home'));
$this->assertNotNull($user->fresh()->last_login, 'last_login must be stamped after a successful Google callback.');
$this->assertTrue(Auth::check(), 'User must be authenticated after a successful Google callback.');
}
+
+ #[Test]
+ public function callback_without_code_redirects_to_login_without_hitting_google(): void
+ {
+ // Satisfies Setting::setupCompleted() so CheckForSetup lets the request through.
+ User::factory()->create();
+ Socialite::shouldReceive('driver')->never();
+
+ $this->get(route('google.callback'))
+ ->assertRedirect(route('login'))
+ ->assertSessionHasErrors('username');
+
+ $this->assertFalse(Auth::check());
+ }
+
+ #[Test]
+ public function callback_with_error_param_redirects_to_login_without_hitting_google(): void
+ {
+ User::factory()->create();
+ Socialite::shouldReceive('driver')->never();
+
+ $this->get(route('google.callback', ['error' => 'access_denied']))
+ ->assertRedirect(route('login'))
+ ->assertSessionHasErrors('username');
+
+ $this->assertFalse(Auth::check());
+ }
+
+ #[Test]
+ public function callback_client_exception_from_token_endpoint_redirects_to_login(): void
+ {
+ User::factory()->create();
+
+ $clientException = new ClientException(
+ 'Client error: 400 Bad Request',
+ new Request('POST', 'https://www.googleapis.com/oauth2/v4/token'),
+ new Response(400),
+ );
+
+ $driver = Mockery::mock();
+ $driver->shouldReceive('user')->andThrow($clientException);
+ Socialite::shouldReceive('driver')->with('google')->andReturn($driver);
+
+ $this->get(route('google.callback', ['code' => 'stale-or-reused']))
+ ->assertRedirect(route('login'))
+ ->assertSessionHasErrors('username');
+
+ $this->assertFalse(Auth::check());
+ }
}