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
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
];
3. Event listener
- Add
SocialiteProviders\Manager\SocialiteWasCalled
event to yourlisten[]
array inapp/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',
],
];
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
],
5. Usage on your application
You should now be able to use it (assuming you have the facade installer) :
return Socialite::driver('azure')->redirect();
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);
}
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
});
Azure configuration
1. Create the app
- On the portal azure click on “App registrations”
- Then click on “New registration”
- Choose a name for your application
- Choose the supported account types
- On Redirect URI choose “web” and fill the uri with the callback route of your laravel application :
//for example
http://localhost:8000/auth/callback
- 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=
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 :
- click on Add a certificate or secret
- click on New client secret
- choose a description
- click on adding button
- copie the Value key
And now your users can connect with their Microsoft account to your application !
Top comments (4)
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.
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.
I follow this example, but its not working. When I click login, It did not ask Microsoft email and password to login.
do you have any error feedback?