Skip to content

Beautiful Open-Source Form Builder

Refactor OAuth drivers to improve error handling and validation

JhumanJ70e3e557bac3
diff --git a/api/app/Exceptions/OAuth/InvalidWidgetDataException.php b/api/app/Exceptions/OAuth/InvalidWidgetDataException.php
new file mode 100644
index 000000000..f83b4fe81
--- /dev/null
+++ b/api/app/Exceptions/OAuth/InvalidWidgetDataException.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Exceptions\OAuth;
+
+use Exception;
+
+class InvalidWidgetDataException extends Exception
+{
+}
diff --git a/api/app/Integrations/OAuth/Drivers/OAuthGoogleOneTapDriver.php b/api/app/Integrations/OAuth/Drivers/OAuthGoogleOneTapDriver.php
index 956487384..340f66605 100644
--- a/api/app/Integrations/OAuth/Drivers/OAuthGoogleOneTapDriver.php
+++ b/api/app/Integrations/OAuth/Drivers/OAuthGoogleOneTapDriver.php
@@ -2,6 +2,7 @@
namespace App\Integrations\OAuth\Drivers;
+use App\Exceptions\OAuth\InvalidWidgetDataException;
use App\Integrations\OAuth\Drivers\Contracts\WidgetOAuthDriver;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
@@ -53,14 +54,16 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
public function verifyWidgetData(array $data): bool
{
if (!isset($data['credential'])) {
- return false;
+ throw new InvalidWidgetDataException('Missing Google One Tap credential');
}
try {
$this->verifyAndDecodeJWT($data['credential']);
return true;
- } catch (\Exception $e) {
- return false;
+ } catch (InvalidWidgetDataException $e) {
+ throw $e;
+ } catch (\Throwable $e) {
+ throw new InvalidWidgetDataException('Invalid Google One Tap credential: ' . $e->getMessage());
}
}
@@ -85,7 +88,7 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
// Split JWT into parts
$parts = explode('.', $jwt);
if (count($parts) !== 3) {
- throw new \Exception('Invalid JWT format');
+ throw new InvalidWidgetDataException('Invalid JWT format');
}
[$headerB64, $payloadB64, $signatureB64] = $parts;
@@ -98,7 +101,7 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
$payloadData = json_decode($payload, true);
if (!$headerData || !$payloadData) {
- throw new \Exception('Invalid JWT data');
+ throw new InvalidWidgetDataException('Invalid JWT data');
}
// Basic payload validation
@@ -128,39 +131,39 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
{
// Check expiration
if (!isset($payload['exp']) || $payload['exp'] < time()) {
- throw new \Exception('JWT has expired');
+ throw new InvalidWidgetDataException('JWT has expired');
}
// Check issued at (not too far in future)
if (!isset($payload['iat']) || $payload['iat'] > time() + 300) {
- throw new \Exception('JWT issued in future');
+ throw new InvalidWidgetDataException('JWT issued in future');
}
// Verify audience (your Google Client ID)
$expectedClientId = config('services.google.client_id');
if (!isset($payload['aud']) || $payload['aud'] !== $expectedClientId) {
- throw new \Exception('Invalid audience');
+ throw new InvalidWidgetDataException('Invalid audience');
}
// Verify issuer
if (!isset($payload['iss']) || !in_array($payload['iss'], ['https://accounts.google.com', 'accounts.google.com'])) {
- throw new \Exception('Invalid issuer');
+ throw new InvalidWidgetDataException('Invalid issuer');
}
// Check required fields
if (!isset($payload['sub']) || !isset($payload['email'])) {
- throw new \Exception('Missing required fields');
+ throw new InvalidWidgetDataException('Missing required fields');
}
}
private function verifySignature(string $data, string $signature, array $header): void
{
if (!isset($header['kid']) || !isset($header['alg'])) {
- throw new \Exception('Missing header information');
+ throw new InvalidWidgetDataException('Missing header information');
}
if ($header['alg'] !== 'RS256') {
- throw new \Exception('Unsupported algorithm');
+ throw new InvalidWidgetDataException('Unsupported algorithm');
}
// Get Google's public keys
@@ -171,7 +174,7 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
$verified = openssl_verify($data, $signatureRaw, $publicKey, OPENSSL_ALGO_SHA256);
if ($verified !== 1) {
- throw new \Exception('Invalid signature');
+ throw new InvalidWidgetDataException('Invalid signature');
}
}
@@ -182,13 +185,13 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
$response = Http::get('https://www.googleapis.com/oauth2/v3/certs');
if (!$response->successful()) {
- throw new \Exception('Failed to fetch Google public keys');
+ throw new InvalidWidgetDataException('Failed to fetch Google public keys');
}
$keys = $response->json();
if (!isset($keys['keys'])) {
- throw new \Exception('Invalid keys response');
+ throw new InvalidWidgetDataException('Invalid keys response');
}
foreach ($keys['keys'] as $key) {
@@ -198,14 +201,14 @@ class OAuthGoogleOneTapDriver implements WidgetOAuthDriver
}
}
- throw new \Exception('Key not found');
+ throw new InvalidWidgetDataException('Key not found');
});
}
private function jwkToPem(array $jwk): string
{
if (!isset($jwk['n']) || !isset($jwk['e'])) {
- throw new \Exception('Invalid JWK format');
+ throw new InvalidWidgetDataException('Invalid JWK format');
}
// Decode base64url modulus and exponent
diff --git a/api/app/Integrations/OAuth/Drivers/OAuthTelegramDriver.php b/api/app/Integrations/OAuth/Drivers/OAuthTelegramDriver.php
index d34554937..395c82a90 100644
--- a/api/app/Integrations/OAuth/Drivers/OAuthTelegramDriver.php
+++ b/api/app/Integrations/OAuth/Drivers/OAuthTelegramDriver.php
@@ -2,6 +2,7 @@
namespace App\Integrations\OAuth\Drivers;
+use App\Exceptions\OAuth\InvalidWidgetDataException;
use App\Integrations\OAuth\Drivers\Contracts\WidgetOAuthDriver;
use Laravel\Socialite\Contracts\User;
@@ -59,7 +60,7 @@ class OAuthTelegramDriver implements WidgetOAuthDriver
// Ignore any extra fields we might add client-side (e.g. intent, utm, invite_token, etc.).
$checkHash = $data['hash'] ?? null;
if (!$checkHash) {
- return false;
+ throw new InvalidWidgetDataException('Missing Telegram hash');
}
// Whitelist allowed keys from Telegram Login Widget
@@ -87,10 +88,17 @@ class OAuthTelegramDriver implements WidgetOAuthDriver
sort($dataCheckArr);
$dataCheckString = implode("\n", $dataCheckArr);
- $secretKey = hash('sha256', config('services.telegram.bot_token'), true);
+ $botToken = config('services.telegram.bot_token');
+ if (!$botToken) {
+ throw new InvalidWidgetDataException('Telegram bot token not configured');
+ }
+ $secretKey = hash('sha256', $botToken, true);
$hash = hash_hmac('sha256', $dataCheckString, $secretKey);
+ if (!hash_equals($hash, $checkHash)) {
+ throw new InvalidWidgetDataException('Invalid Telegram signature');
+ }
- return hash_equals($hash, $checkHash);
+ return true;
}
public function getUserFromWidgetData(array $data): array
diff --git a/api/app/Service/OAuth/OAuthUserDataService.php b/api/app/Service/OAuth/OAuthUserDataService.php
index aec83dfc7..f2e20ae87 100644
--- a/api/app/Service/OAuth/OAuthUserDataService.php
+++ b/api/app/Service/OAuth/OAuthUserDataService.php
@@ -2,6 +2,7 @@
namespace App\Service\OAuth;
+use App\Exceptions\OAuth\InvalidWidgetDataException;
use App\Integrations\OAuth\OAuthProviderService;
use App\Integrations\OAuth\Drivers\Contracts\WidgetOAuthDriver;
use Illuminate\Http\Request;
@@ -39,8 +40,10 @@ class OAuthUserDataService
abort(400, 'This provider does not support widget authentication');
}
- if (!$driver->verifyWidgetData($request->all())) {
- abort(400, 'Invalid widget data');
+ try {
+ $driver->verifyWidgetData($request->all());
+ } catch (InvalidWidgetDataException $e) {
+ abort(400, $e->getMessage());
}
return $this->normalizeUserData(
diff --git a/api/config/vapor-ui.php b/api/config/vapor-ui.php
deleted file mode 100644
index ec294735c..000000000
--- a/api/config/vapor-ui.php
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-
-use Laravel\VaporUi\Http\Middleware\EnsureEnvironmentVariables;
-use Laravel\VaporUi\Http\Middleware\EnsureUpToDateAssets;
-use Laravel\VaporUi\Http\Middleware\EnsureUserIsAuthorized;
-
-return [
-
- /*
- |--------------------------------------------------------------------------
- | Vapor UI Route Middleware
- |--------------------------------------------------------------------------
- |
- | These middleware will be assigned to every Vapor UI route - giving you
- | the chance to add your own middleware to this list or change any of
- | the existing middleware. Or, you can simply stick with this list.
- |
- */
-
- 'middleware' => [
- 'api',
- EnsureUserIsAuthorized::class,
- EnsureEnvironmentVariables::class,
- EnsureUpToDateAssets::class,
- ],
-
- /*
- |--------------------------------------------------------------------------
- | Vapor UI Domain
- |--------------------------------------------------------------------------
- |
- | This is the subdomain where Vapor UI will be accessible from. If this
- | setting is null, Vapor UI will reside under the same domain as the
- | application. Otherwise this value should serve as the subdomain.
- |
- */
-
- 'domain' => env('VAPOR_UI_DOMAIN', null),
-
- /*
- |--------------------------------------------------------------------------
- | Vapor UI Path
- |--------------------------------------------------------------------------
- |
- | This is the URI path where Vapor UI will be accessible from. Feel free
- | to change the path to anything you like. Note that the URI will not
- | affect the paths of its internal API that isn't exposed to users.
- |
- */
-
- 'path' => env('VAPOR_UI_PATH', 'vapor-ui'),
-
- /*
- |--------------------------------------------------------------------------
- | Vapor UI Queues
- |--------------------------------------------------------------------------
- |
- | Typically, queues that should be monitored will be determined for you
- | by Vapor UI. However, you are free to add additional queues to the
- | list below in order to monitor queues that Vapor doesn't manage.
- |
- */
-
- 'queues' => [
-
- ],
-
-];