DirectoryTree / Bartender

An opinionated way to authenticate users using Laravel Socialite.
MIT License
254 stars 9 forks source link

Question: Multi-Tenant Microsoft Azure Configurations #6

Closed bubba-h57 closed 7 months ago

bubba-h57 commented 7 months ago

With the Microsoft-Azure provider, to have multiple/different Active directories on Azure (i.e. multiple tenants), the same driver can be used but with a different config. See Advanced Usage.

Does Bartender support this type of configuration? If so, how?

stevebauman commented 7 months ago

Hi @bubba-h57, for this you'll have to implement your own UserProviderHandler or extend the built in one:

// app/Socialite/AzureProviderHandler.php

namespace App\Socialite;

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use SocialiteProviders\Manager\Config;
use Laravel\Socialite\Contracts\Provider;
use DirectoryTree\Bartender\ProviderHandler;

class AzureProviderHandler implements ProviderHandler
{
    public function redirect(Provider $provider, string $driver): RedirectResponse
    {
        $provider->setConfig($this->getConfig());

        return $provider->redirect();
    }

    public function callback(Provider $provider, string $driver): RedirectResponse
    {
        $user = $provider->setConfig($this->getConfig())->user();

        // Authenticate the user your own way...

        return redirect()->route('dashboard');
    }

    protected function getConfig(): Config
    {
        return new Config(...);
    }
}

Then, serve your custom handler for the azure driver:

// app/Providers/AuthServiceProvider.php

namespace App\Providers;

use App\Socialite\AzureProviderHandler;
use DirectoryTree\Bartender\Facades\Bartender;

class AuthServiceProvider extends ServiceProvider
{
    // ...

    public function boot(): void
    {
        // ...

        Bartender::serve('azure', AzureProviderHandler::class);
    }
}

Let me know if you have any further questions.