|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metaclassing\EnterpriseAuth\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
|
6
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
7
|
|
|
|
|
8
|
|
|
class WebAuthController extends AuthController |
|
9
|
|
|
{ |
|
10
|
|
|
// Route to save unauthenticated users original page request and redirect to oauth provider redirect |
|
11
|
|
|
public function loginOrRegister(\Illuminate\Http\Request $request) |
|
12
|
|
|
{ |
|
13
|
|
|
// This detects if we should hit the API auth handler or WEB auth handler |
|
14
|
|
|
if ($request->expectsJson()) { |
|
15
|
|
|
$response = response()->json(['message' => $exception->getMessage()], 401); |
|
|
|
|
|
|
16
|
|
|
} else { |
|
17
|
|
|
// This is what gets called after a user is redirected to /login by the framework |
|
18
|
|
|
$lastPage = $request->session()->get('url.intended'); |
|
19
|
|
|
\Illuminate\Support\Facades\Log::info('AUTH loginOrRegister with request url '.$lastPage); |
|
20
|
|
|
// Make sure they are not going to end up in a redirect loop with the login route |
|
21
|
|
|
if ($lastPage && $lastPage != route('login')) { |
|
22
|
|
|
$request->session()->put('oauthIntendedUrl', $lastPage); |
|
23
|
|
|
} |
|
24
|
|
|
$response = redirect()->guest(config('enterpriseauth.routes.login')); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return $response; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Route called to redirect unauthenticated users to oauth identity provider |
|
31
|
|
|
public function redirectToOauthProvider(\Illuminate\Http\Request $request) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$url = $this->buildAuthUrl(); |
|
34
|
|
|
//return new \Illuminate\Http\RedirectResponse($url); |
|
35
|
|
|
return redirect($url); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Helper to build redirect url from azure AD tenant |
|
39
|
|
|
public function buildAuthUrl() |
|
40
|
|
|
{ |
|
41
|
|
|
$url = $this->azureActiveDirectory->authorizationEndpoint |
|
42
|
|
|
. '?' |
|
43
|
|
|
. $this->buildAuthUrlQueryString(); |
|
44
|
|
|
|
|
45
|
|
|
return $url; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// helper to build query string for oauth provider |
|
49
|
|
|
public function buildAuthUrlQueryString() |
|
50
|
|
|
{ |
|
51
|
|
|
$fields = [ |
|
52
|
|
|
'client_id' => ENV('AZURE_AD_CLIENT_ID'), |
|
53
|
|
|
'redirect_uri' => ENV('AZURE_AD_CALLBACK_URL'), |
|
54
|
|
|
'scope' => 'https://graph.microsoft.com/.default', |
|
55
|
|
|
'response_type' => 'code', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
return http_build_query($fields); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Route to handle response back from our oauth provider |
|
62
|
|
|
public function handleOauthResponse(\Illuminate\Http\Request $request) |
|
63
|
|
|
{ |
|
64
|
|
|
// Turn coke into pepsi |
|
65
|
|
|
$accessToken = $this->getAccessTokenFromCode($request->input('code')); |
|
66
|
|
|
// Get the associated laravel \App\User object |
|
67
|
|
|
$user = $this->validateOauthCreateOrUpdateUserAndGroups($accessToken); |
|
68
|
|
|
// Authenticate the users session |
|
69
|
|
|
auth()->login($user, true); |
|
70
|
|
|
|
|
71
|
|
|
// Check to see if there is an intended destination url saved |
|
72
|
|
|
$destination = $request->session() |
|
73
|
|
|
->get('oauthIntendedUrl'); |
|
74
|
|
|
// If there is no intended destination url, use the default |
|
75
|
|
|
if (! $destination) { |
|
76
|
|
|
$destination = config('enterpriseauth.redirect_on_login'); |
|
77
|
|
|
} |
|
78
|
|
|
\Illuminate\Support\Facades\Log::info('AUTH success USER ID '.$user->id.' with redirect url '.$destination); |
|
79
|
|
|
|
|
80
|
|
|
return redirect($destination); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Turn coke into pepsi: Take the authorization code and turn it into an access token for graph api |
|
84
|
|
|
public function getAccessTokenFromCode($code) |
|
85
|
|
|
{ |
|
86
|
|
|
$guzzle = new \GuzzleHttp\Client(); |
|
87
|
|
|
$url = $this->azureActiveDirectory->tokenEndpoint; |
|
88
|
|
|
$parameters = [ |
|
89
|
|
|
'headers' => [ |
|
90
|
|
|
'Accept' => 'application/json' |
|
91
|
|
|
], |
|
92
|
|
|
'form_params' => [ |
|
93
|
|
|
'code' => $code, |
|
94
|
|
|
'scope' => 'https://graph.microsoft.com/.default', |
|
95
|
|
|
'client_id' => env('AZURE_AD_CLIENT_ID'), |
|
96
|
|
|
'client_secret' => env('AZURE_AD_CLIENT_SECRET'), |
|
97
|
|
|
'redirect_uri' => ENV('AZURE_AD_CALLBACK_URL'), |
|
98
|
|
|
'grant_type' => 'authorization_code', |
|
99
|
|
|
] |
|
100
|
|
|
]; |
|
101
|
|
|
$response = $guzzle->post($url, $parameters); |
|
102
|
|
|
$responseObject = json_decode($response->getBody()); |
|
103
|
|
|
$accessToken = $responseObject->access_token; |
|
104
|
|
|
return $accessToken; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|