DEV Community

Judicaël
Judicaël

Posted on • Edited on

Laravel socialite : configure microsoft Azure

You have a Laravel application and you want to connect your users with azure. In this article I will explain to you how to do it. For this tutorial I use Laravel Socialite package.

Package installation and configuration :

1. Installation :

In first execute this command to install the provider:



composer require socialiteproviders/microsoft-azure


Enter fullscreen mode Exit fullscreen mode

2. Service provider :

In config/app.php add \SocialiteProviders\Manager\ServiceProvider::class to your providers[] array, and remove Laravel\Socialite\SocialiteServiceProvider from your providers[] if you have added it already.

For example :



'providers' => [
    // a whole bunch of providers
    // remove 'Laravel\Socialite\SocialiteServiceProvider',
    \SocialiteProviders\Manager\ServiceProvider::class, // add
];


Enter fullscreen mode Exit fullscreen mode

3. Event listener

  • Add SocialiteProviders\Manager\SocialiteWasCalled event to your listen[] array in app/Providers/EventServiceProvider.
  • Add your listeners (i.e. the ones from the providers) to the SocialiteProviders\Manager\SocialiteWasCalled[] that you just created.
  • Add the listener for your provider to the array. In our example, this is \SocialiteProviders\Azure\AzureExtendSocialite::class.'@handle',.


protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        \SocialiteProviders\Azure\AzureExtendSocialite::class.'@handle',
    ],
];


Enter fullscreen mode Exit fullscreen mode

4. Add configuration to config/services.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
],


Enter fullscreen mode Exit fullscreen mode

5. Usage on your application

You should now be able to use it (assuming you have the facade installer) :



return Socialite::driver('azure')->redirect();


Enter fullscreen mode Exit fullscreen mode

To logout of you app and Azure :



public function logout(Request $request) 
{
     Auth::guard()->logout();
     $request->session()->flush();
     $azureLogoutUrl = Socialite::driver('azure')->getLogoutUrl(route('login'));
     return redirect($azureLogoutUrl);
}


Enter fullscreen mode Exit fullscreen mode

In laravel documentation, to authenticate users with an OAuth provider, you will need two routes : one for redirecting the user to the OAuth provider, and another for receving the callback from the provider after authentication :



use Laravel\Socialite\Facades\Socialite;

Route::get('/auth/redirect', function () {
    return Socialite::driver('azure')->redirect();
});

Route::get('/auth/callback', function () {
    $user = Socialite::driver('azure')->user();

    // $user->token
});


Enter fullscreen mode Exit fullscreen mode

Azure configuration

1. Create the app

  1. On the portal azure click on “App registrations”

App registration

  1. Then click on “New registration”

New registration

  1. Choose a name for your application
  2. Choose the supported account types
  3. On Redirect URI choose “web” and fill the uri with the callback route of your laravel application :


//for example
http://localhost:8000/auth/callback


Enter fullscreen mode Exit fullscreen mode
  1. Click on Register

Configuration of the app in Laravel :

In your .env file add :



AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_REDIRECT_URI=
AZURE_TENANT_ID=
PROXY=


Enter fullscreen mode Exit fullscreen mode

AZURE_CLIENT_ID is “Application (client) id” on your azure application

AZURE_REDIRECT_URI is the callback route URI

AZURE_TENANT_ID is the “Directorty (tenant) ID on your azure application

For The AZURE_CLIENT_SECRET :

  1. click on Add a certificate or secret
  2. click on New client secret
  3. choose a description
  4. click on adding button
  5. copie the Value key

Value key

And now your users can connect with their Microsoft account to your application !

Top comments (4)

Collapse
 
beres88 profile image
Arpad Beres

Hi Judicaël,

Could you show me an example code for the login? :)
The login controller would be most interested in how the login/logout is implemented?

But if there is a minimalist sample project for this, it would be a great help to me.

This Azure AD authentication is very new to me and I think I am lost a litle bit.
Thank you very much in advance.

Collapse
 
judicaelg profile image
Judicaël

Hi Arpad,

In my login view, I've created a button that redirects the user to the "/auth/redirect" route, and I've done the same for the logout button.

Collapse
 
usmraf786 profile image
usmraf786

I follow this example, but its not working. When I click login, It did not ask Microsoft email and password to login.

Collapse
 
judicaelg profile image
Judicaël

do you have any error feedback?