Skip to content

Commit

Permalink
add readme and license file. renamed isallowedcallback
Browse files Browse the repository at this point in the history
  • Loading branch information
josh48202 committed Apr 5, 2024
1 parent 8e5a205 commit 90184cc
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 30 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) chrisreedio <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Filament Connectify - Social Login through Laravel Socialite

### Add OAuth2 Login support to Filament v3 through Laravel Socialite

This package extends [Laravel Socialite](https://laravel.com/docs/master/socialite). Socialite currently supports
authentication via Facebook, Twitter, LinkedIn, Google, GitHub, GitLab, and Bitbucket out of the box.

Refer to the [Socialite documentation](https://laravel.com/docs/master/socialite) for more information on how to
configure your application to use these providers.

Many other providers are available via the [Socialite Providers](https://socialiteproviders.com/) website. Refer to the
documentation for each provider for information on how to configure your application to use them.

---

## Installation

Install package via composer:
```bash
composer require wjbecker/filament-connectify
```

Publish & migrate migration files
```bash
php artisan vendor:publish --tag="filament-connectify-migrations
php artisan migrate
```
To use provider icons you can add [Blade Font Awesome](https://github.com/owenvoke/blade-fontawesome) brand icons
```bash
composer require owenvoke/blade-fontawesome
```
---
## Provider Configuration
Refer to the [Socialite documentation](https://laravel.com/docs/master/socialite) for more information.
---
## Panel Configuration
Include this plugin in your panel configuration:
```php
use Wjbecker\FilamentConnectify\FilamentConnectifyPlugin;
return $panel
// ...
->plugins([
// ... Other Plugins
FilamentConnectifyPlugin::make()
// (required) add providers
->providers([
'azure' => [
'label' => 'Continue with Microsoft',
'icon' => 'fab-microsoft', // requires additional package
]
])
// (optional) restrict login callback
->isAllowedCallback(function (\SocialiteProviders\Manager\OAuth2\User $socialiteUser) {
$decodedToken = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $socialiteUser->token)[1]))));
return $decodedToken->tid === {{azure_tenant_id}};
})
// (optional) change the user model class
->userModel(\App\Models\User::class)
// (optional) change redirect url callback
->redirectUrlCallback(function ($provider) {
return 'https://'.tenant('id').'.foo.test'.route(FilamentConnectifyPlugin::get()->getCallbackRoute(), $provider, false);
})
])
```
---
### Sample Provider Configuration - Azure Active Directory
To start, You would refer to the documentation for
the [Azure Socialite Provider](https://socialiteproviders.com/Microsoft-Azure/).
Normally, you would follow the providers documentation on the aforementioned link but to demonstrate, I'll include the steps here.
Per their documentation, you would install the community Azure provider via
```bash
composer require socialiteproviders/microsoft-azure
```
Then you would configure your `config/services.php` file to include the Azure provider's credentials:
```php
'azure' => [
'client_id' => env('AZURE_CLIENT_ID'),
'client_secret' => env('AZURE_CLIENT_SECRET'),
'redirect' => env('AZURE_REDIRECT_URI'),
'tenant' => env('AZURE_TENANT_ID'),
'proxy' => env('PROXY') // optionally
],
```
In addition, you need to add this provider's event listener to your `app/Providers/EventServiceProvider.php` file:
```php
protected $listen = [
// ... other listeners
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\Azure\AzureExtendSocialite::class.'@handle',
],
];
```
Finally, don't forget to add the needed environment variables to your `.env` file:
```dotenv
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_REDIRECT_URI=
AZURE_TENANT_ID=
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
33 changes: 7 additions & 26 deletions src/FilamentConnectifyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ class FilamentConnectifyPlugin implements Plugin

protected string $redirectRoute;

protected array $tenantAllowList = [];

protected string $loginRoute;

protected string $userModel = User::class;

protected ?Closure $redirectUrlCallback;

protected ?Closure $tenantAllowedCallback;
protected ?Closure $isAllowedCallback;

private string $callbackRoute;


public static function make(): static
{
return app(static::class);
Expand Down Expand Up @@ -103,24 +102,13 @@ public function getRedirectUrlCallback(): Closure
};
}

public function getTenantAllowedCallback(): Closure
public function getIsAllowedCallback(): Closure
{
return $this->tenantAllowedCallback ?? function ($socialiteUser) {
$tenants = $this->getTenantAllowList();

if (empty($tenants)) return true;

$decodedToken = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $socialiteUser->token)[1]))));

return in_array($decodedToken->tid, $tenants);
return $this->isAllowedCallback ?? function ($socialiteUser) {
return true;
};
}

public function getTenantAllowList(): array
{
return $this->tenantAllowList;
}

public function getUserModel(): string
{
return $this->userModel;
Expand All @@ -133,16 +121,9 @@ public function redirectUrlCallback(Closure $callback = null): static
return $this;
}

public function tenantAllowedCallback(Closure $callback = null): static
{
$this->tenantAllowedCallback = $callback;

return $this;
}

public function tenants(array $tenants): static
public function isAllowedCallback(Closure $callback = null): static
{
$this->tenantAllowList = $tenants;
$this->isAllowedCallback = $callback;

return $this;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Controllers/FilamentConnectifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wjbecker\FilamentConnectify\Http\Controllers;

use Filament\Facades\Filament;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
Expand Down Expand Up @@ -31,7 +30,7 @@ public function callback(string $provider)
/** @var User $socialite */
$socialite = Socialite::driver($provider)->stateless()->user();

if (!$this->isTenantAllowed($socialite)) {
if (!$this->isAllowed($socialite)) {
session()->flash('filament.connectify.login.error', 'Tenant is not allowed.');
return redirect()->route(FilamentConnectifyPlugin::get()->getLoginRoute());
}
Expand Down Expand Up @@ -65,9 +64,9 @@ public function callback(string $provider)
return redirect()->route(FilamentConnectifyPlugin::get()->getLoginRoute());
}

private function isTenantAllowed($socialiteUser)
private function isAllowed($socialiteUser)
{
return app()->call(FilamentConnectifyPlugin::get()->getTenantAllowedCallback(), ['socialiteUser' => $socialiteUser]);
return app()->call(FilamentConnectifyPlugin::get()->getIsAllowedCallback(), ['socialiteUser' => $socialiteUser]);
}

private function redirectUrl($provider)
Expand Down

0 comments on commit 90184cc

Please sign in to comment.