|
1 |
| -# laravel-azure-middleware |
| 1 | +# Laravel Azure Middleware |
| 2 | + |
| 3 | +Provides Azure Authentication Middleware for a Laravel App. |
| 4 | + |
| 5 | +## Normal Installation |
| 6 | + |
| 7 | +1. `composer require rootinc/laravel-azure-middleware` |
| 8 | +2. In our routes folder (most likely `web.php`), add |
| 9 | +```php |
| 10 | +Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure'); |
| 11 | +Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback'); |
| 12 | +``` |
| 13 | + |
| 14 | +3. In our `App\Http\Kernel.php` add `'azure' => \RootInc\LaravelAzureMiddleware\Azure::class,` most likely to the `$routeMiddleware` array. |
| 15 | +4. In our `.env` add `TENANT_ID, CLIENT_ID, CLIENT_SECRET and RESOURCE`. We can get these values/read more here: https://portal.azure.com/ |
| 16 | +5. Add the `azure` middleware to your route groups (or wherever) and enjoy :tada: |
| 17 | +6. If you need custom callbacks, see #Extended Installation. |
| 18 | + |
| 19 | +## Routing |
| 20 | + |
| 21 | +`Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure');` First parameter can be wherever you want to route the azure login. Change as you would like. |
| 22 | + |
| 23 | +`Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback');` First parameter can be whatever you want to route after your callback. Change as you would like. |
| 24 | + |
| 25 | +## Front End |
| 26 | + |
| 27 | +It's best to have an Office 365 button on our login webpage that routes to `/login/azure` (or whatever you renamed it to). This can be as simple as an anchor tag like this `<a href="/login/azure" class="officeButton"></a>` |
| 28 | + |
| 29 | +## Extended Installation |
| 30 | + |
| 31 | +The out-of-the-box implementation let's you login users. However, let's say we would like to store this user into a database. There are two callbacks that are recommended to extend from the Azure class called `success` and `fail`. The following provides information on how to extend the Root Laravel Azure Middleware Library: |
| 32 | + |
| 33 | +1. To get started (assuming we've followed the #Normal Installation directions), create a file called `AppAzure.php` in the `App\Http\Middleware` folder. You can either do this through `artisan` or manually. |
| 34 | +2. Add this as a starting point in this file: |
| 35 | + |
| 36 | +```php |
| 37 | +<?php |
| 38 | + |
| 39 | +namespace App\Http\Middleware; |
| 40 | + |
| 41 | +use RootInc\LaravelAzureMiddleware\Azure as Azure; |
| 42 | + |
| 43 | +use App\User; |
| 44 | + |
| 45 | +class AppAzure extends Azure |
| 46 | +{ |
| 47 | + protected function success($request, $access_token, $refresh_token, $profile) |
| 48 | + { |
| 49 | + $email = strtolower($profile->unique_name); |
| 50 | + |
| 51 | + $user = User::updateOrCreate(['email' => $email], [ |
| 52 | + 'firstName' => $profile->given_name, |
| 53 | + 'lastName' => $profile->family_name |
| 54 | + ]); |
| 55 | + |
| 56 | + $request->session()->put('user_id', $user->id); |
| 57 | + |
| 58 | + return parent::success($request, $access_token, $refresh_token, $profile); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +The above gives us a way to add/update users after a successful handshake. `$profile` contains all sorts of metadata that we use to create or update our user. More information here: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code#jwt-token-claims . The default implementation redirects to `/`, so we call the parent here. Feel free to not extend the default and to redirect elsewhere. |
| 64 | + |
| 65 | +3. Our routes need to be updated to the following: |
| 66 | + |
| 67 | +```php |
| 68 | +Route::get('/login/azure', '\App\Http\Middleware\AppAzure@azure'); |
| 69 | +Route::get('/login/azurecallback', '\App\Http\Middleware\AppAzure@azurecallback'); |
| 70 | +``` |
| 71 | + |
| 72 | +4. Finally, update `Kernel.php`'s `azure` key to be `'azure' => \App\Http\Middleware\AppAzure::class,` |
| 73 | + |
| 74 | +## Contributing |
| 75 | + |
| 76 | +TODO |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +The Laravel Azure Middleware is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). |
0 commit comments