Skip to content

Commit

Permalink
Merge pull request #96 from LittleHans8/main
Browse files Browse the repository at this point in the history
Compatible with Stateless Authentication
  • Loading branch information
bramr94 authored Jun 4, 2024
2 parents 633d7bf + 7ecc2e9 commit 9a6d852
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ You can add [optional parameters](https://laravel.com/docs/10.x/socialite#option
]
```

## Stateless Authentication
You can add `stateless` parameters to the provider configuration in the config/services.php config file, for example:

```php
'apple' => [
'client_id' => '...',
'client_secret' => '...',
'stateless'=>true,
]
```

**Note:** you cannot use the `state` parameter, as it is used to determine from which Filament panel the user came from.

## Changelog
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}
}

Route::get("/oauth/callback/{provider}", [SocialiteLoginController::class, 'processCallback'])
Route::match(['get', 'post'], '/oauth/callback/{provider}', [SocialiteLoginController::class, 'processCallback'])
->middleware([
PanelFromUrlQuery::class,
...config('filament-socialite.middleware'),
Expand Down
5 changes: 5 additions & 0 deletions src/FilamentSocialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function getProviderScopes(string $provider): string | array
return $this->getProviderConfig($provider)['scopes'] ?? [];
}

public function getProviderStateless(string $provider): bool
{
return $this->getProviderConfig($provider)['stateless'] ?? false;
}

/**
* @return array<string, mixed>
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Http/Controllers/SocialiteLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public function redirectToProvider(string $provider): RedirectResponse

protected function retrieveOauthUser(string $provider): ?SocialiteUserContract
{
$stateless = $this->socialite->getProviderStateless($provider);
try {
return Socialite::driver($provider)->user();
/** @var \Laravel\Socialite\Two\AbstractProvider $driver */
$driver = Socialite::driver($provider);

return $stateless ? $driver->stateless()->user() : $driver->user();
} catch (InvalidStateException $e) {
Events\InvalidState::dispatch($e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/PanelFromUrlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public static function encrypt(string $panel): string
public static function decrypt(Request $request): string
{
try {
if (! is_string($request->query('state'))) {
if (! is_string($request->get('state'))) {
throw new DecryptException('State is not a string.');
}

return Crypt::decrypt($request->query('state'));
return Crypt::decrypt($request->get('state'));
} catch (DecryptException $e) {
throw InvalidCallbackPayload::make($e);
}
Expand Down

0 comments on commit 9a6d852

Please sign in to comment.