Laravel socialite : configure microsoft Azure
Tags: laravel, azure, php, oauth
Published: 2022-07-05 16:07:05

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](https://laravel.com/docs/9.x/socialite).  

## Package installation and configuration :

### 1. Installation :

In first execute this command to install the provider:

```bash
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 :

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

### 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',`.

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

### 4. Add configuration to config/services.php

```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) :

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

To logout of you app and Azure :

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

In [laravel documentation](https://laravel.com/docs/9.x/socialite), 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 :

```php
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

1. On the [portal azure](https://portal.azure.com/#home) click on “App registrations”


![App registration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o6qmggh8b9dpcvtyeh8.png)

2. Then click  on “New registration”


![New registration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67uibl5n3o010znflt7w.png)

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

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

6. Click on Register

### Configuration of the app in Laravel :

In your .env file add :

```php
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** : 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jti2dbrcm3rc1jqapcz.png)

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