Skip to content

Commit c04a51f

Browse files
Merge pull request #16 from JustinByrne/master
Added ability to use Microsoft Graph SDK fixes #11
2 parents 5834808 + 2b184b8 commit c04a51f

File tree

5 files changed

+124
-22
lines changed

5 files changed

+124
-22
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ Provides Azure Authentication Middleware for a Laravel App.
55
## Normal Installation
66

77
1. `composer require rootinc/laravel-azure-middleware`
8-
2. In our routes folder (most likely `web.php`), add
8+
2. run `php artisan vendor:publish` to install config file to `config/azure.php`
9+
3. In our routes folder (most likely `web.php`), add
910
```php
1011
Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure');
1112
Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback');
1213
```
1314

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 `AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_RESOURCE`. We can get these values/read more here: https://portal.azure.com/
16-
5. As of 0.8.0, we added `AZURE_SCOPE`, which are permissions to be used for the request. We can read more about these here: https://docs.microsoft.com/en-us/graph/api/resources/users?view=graph-rest-1.0
17-
6. We also added an optional `AZURE_DOMAIN_HINT` that can be used to help users know which email address they should login with. More info here: https://azure.microsoft.com/en-us/updates/app-service-auth-and-azure-ad-domain-hints/
18-
7. Within our app on https://portal.azure.com/ point `reply url` to the `/login/azurecallback` route with the full url (ex: http://thewebsite.com/login/azurecallback).
19-
8. Add the `azure` middleware to your route groups on any routes that needs protected by auth and enjoy :tada:
20-
9. If you need custom callbacks, see [Extended Installation](#extended-installation).
15+
4. In our `App\Http\Kernel.php` add `'azure' => \RootInc\LaravelAzureMiddleware\Azure::class,` most likely to the `$routeMiddleware` array.
16+
5. In our `.env` add `AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_RESOURCE`. We can get these values/read more here: https://portal.azure.com/
17+
6. As of 0.8.0, we added `AZURE_SCOPE`, which are permissions to be used for the request. We can read more about these here: https://docs.microsoft.com/en-us/graph/api/resources/users?view=graph-rest-1.0
18+
7. We also added an optional `AZURE_DOMAIN_HINT` that can be used to help users know which email address they should login with. More info here: https://azure.microsoft.com/en-us/updates/app-service-auth-and-azure-ad-domain-hints/
19+
8. Within our app on https://portal.azure.com/ point `reply url` to the `/login/azurecallback` route with the full url (ex: http://thewebsite.com/login/azurecallback).
20+
9. Add the `azure` middleware to your route groups on any routes that needs protected by auth and enjoy :tada:
21+
10. If you need custom callbacks, see [Extended Installation](#extended-installation).
2122

2223
__NOTE: ~~You may need to add premissions for (legacy) Azure Active Directory Graph~~ As of 0.8.0, we are using v2 of Azure's login API, which allows us to pass scopes, or permissions we'd like to use.__
2324

@@ -46,6 +47,8 @@ The out-of-the-box implementation let's you login users. However, let's say we
4647
namespace App\Http\Middleware;
4748

4849
use RootInc\LaravelAzureMiddleware\Azure as Azure;
50+
use Microsoft\Graph\Graph;
51+
use Microsoft\Graph\Model;
4952

5053
use Auth;
5154

@@ -55,11 +58,18 @@ class AppAzure extends Azure
5558
{
5659
protected function success($request, $access_token, $refresh_token, $profile)
5760
{
58-
$email = strtolower($profile->unique_name);
61+
$graph = new Graph();
62+
$graph->setAccessToken($access_token);
63+
64+
$graph_user = $graph->createRequest("GET", "/me")
65+
->setReturnType(Model\User::class)
66+
->execute();
67+
68+
$email = strtolower($graph_user->getUserPrincipalName());
5969

6070
$user = User::updateOrCreate(['email' => $email], [
61-
'firstName' => $profile->given_name,
62-
'lastName' => $profile->family_name,
71+
'firstName' => $graph_user->getGivenName(),
72+
'lastName' => $graph_user->getSurname(),
6373
]);
6474

6575
Auth::login($user, true);
@@ -184,7 +194,7 @@ class AppAzure extends Azure
184194
//we could overload this if we wanted too.
185195
public function getAzureUrl()
186196
{
187-
return $this->baseUrl . env('AZURE_TENANT_ID') . $this->route2 . "authorize?response_type=code&client_id=" . env('AZURE_CLIENT_ID') . "&domain_hint=" . urlencode(env('AZURE_DOMAIN_HINT')) . "&scope=" . urldecode(env('AZURE_SCOPE'));
197+
return $this->baseUrl . config('azure.tenant_id') . $this->route2 . "authorize?response_type=code&client_id=" . config('azure.client.id') . "&domain_hint=" . urlencode(config('azure.domain_hint')) . "&scope=" . urldecode(config('azure.scope'));
188198
}
189199

190200
public function azure(Request $request)

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"php": ">=5.6.4",
77
"laravel/framework": ">=5.4.0",
8-
"guzzlehttp/guzzle": "^6.2"
8+
"guzzlehttp/guzzle": "^6.2",
9+
"microsoft/microsoft-graph": "^1.5"
910
},
1011
"license": "MIT",
1112
"authors": [
@@ -18,5 +19,12 @@
1819
"psr-4": {
1920
"RootInc\\LaravelAzureMiddleware\\": "src/"
2021
}
22+
},
23+
"extra": {
24+
"laravel": {
25+
"providers": [
26+
"rootinc\\LaravelAzureMiddleware\\AzureServiceProvider"
27+
]
28+
}
2129
}
2230
}

config/azure.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Tenant ID
7+
|--------------------------------------------------------------------------
8+
|
9+
| This value is equal to the 'Directory (tenant) ID' as found in the Azure
10+
| portal
11+
|
12+
*/
13+
'tenant_id' => env('AZURE_TENANT_ID', ''),
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Client Info
18+
|--------------------------------------------------------------------------
19+
|
20+
| These values are equal to 'Application (client) ID' and the secret you
21+
| made in 'Client secrets' as found in the Azure portal
22+
|
23+
*/
24+
'client' => [
25+
'id' => env('AZURE_CLIENT_ID', ''),
26+
'secret' => env('AZURE_CLIENT_SECRET', ''),
27+
],
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| Resource ID
32+
|--------------------------------------------------------------------------
33+
|
34+
| This value is equal to the 'Object ID' as found in the Azure portal
35+
|
36+
*/
37+
'resource' => env('AZURE_RESOURCE', ''),
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| Domain Hint
42+
|--------------------------------------------------------------------------
43+
|
44+
| This value can be used to help users know which email address they
45+
| should login with.
46+
| https://azure.microsoft.com/en-us/updates/app-service-auth-and-azure-ad-domain-hints/
47+
|
48+
*/
49+
'domain_hint' => env('AZURE_DOMAIN_HINT', ''),
50+
51+
/*
52+
|--------------------------------------------------------------------------
53+
| Permission Scope
54+
|--------------------------------------------------------------------------
55+
|
56+
| This value indicates the permissions granted to the OAUTH session.
57+
| https://docs.microsoft.com/en-us/graph/api/resources/users?view=graph-rest-1.0
58+
|
59+
*/
60+
'scope' => env('AZURE_SCOPE', 'User.Read'),
61+
];

src/Azure.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public function handle($request, Closure $next)
4646
$client = new Client();
4747

4848
try {
49-
$response = $client->request('POST', $this->baseUrl . env('AZURE_TENANT_ID') . $this->route . "token", [
49+
$response = $client->request('POST', $this->baseUrl . config('azure.tenant_id') . $this->route . "token", [
5050
'form_params' => [
5151
'grant_type' => 'refresh_token',
52-
'client_id' => env('AZURE_CLIENT_ID'),
53-
'client_secret' => env('AZURE_CLIENT_SECRET'),
52+
'client_id' => config('azure.client.id'),
53+
'client_secret' => config('azure.client.secret'),
5454
'refresh_token' => $refresh_token,
55-
'resource' => env('AZURE_RESOURCE'),
55+
'resource' => config('azure.resource'),
5656
]
5757
]);
5858

@@ -94,7 +94,7 @@ protected function handleTesting(Request $request, Closure $next)
9494
*/
9595
public function getAzureUrl()
9696
{
97-
return $this->baseUrl . env('AZURE_TENANT_ID') . $this->route2 . "authorize?response_type=code&client_id=" . env('AZURE_CLIENT_ID') . "&domain_hint=" . urlencode(env('AZURE_DOMAIN_HINT')) . "&scope=" . urldecode(env('AZURE_SCOPE'));
97+
return $this->baseUrl . config('azure.tenant_id') . $this->route2 . "authorize?response_type=code&client_id=" . config('azure.client.id') . "&domain_hint=" . urlencode(config('azure.domain_hint')) . "&scope=" . urldecode(config('azure.scope'));
9898
}
9999

100100
/**
@@ -134,13 +134,13 @@ public function azurecallback(Request $request)
134134
$code = $request->input('code');
135135

136136
try {
137-
$response = $client->request('POST', $this->baseUrl . env('AZURE_TENANT_ID') . $this->route . "token", [
137+
$response = $client->request('POST', $this->baseUrl . config('azure.tenant_id') . $this->route . "token", [
138138
'form_params' => [
139139
'grant_type' => 'authorization_code',
140-
'client_id' => env('AZURE_CLIENT_ID'),
141-
'client_secret' => env('AZURE_CLIENT_SECRET'),
140+
'client_id' => config('azure.client.id'),
141+
'client_secret' => config('azure.client.secret'),
142142
'code' => $code,
143-
'resource' => env('AZURE_RESOURCE'),
143+
'resource' => config('azure.resource'),
144144
]
145145
]);
146146

src/AzureServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace rootinc\LaravelAzureMiddleware;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class AzureServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
if ($this->app->runningInConsole()) {
17+
18+
$this->publishes([
19+
__DIR__.'/../config/azure.php' => config_path('azure.php'),
20+
], 'azure-config');
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)