1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Metaclassing\EnterpriseAuth\Controllers; |
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 is what gets called after a user is redirected to /login by the framework |
14
|
|
|
$lastPage = $request->session()->get('url.intended'); |
15
|
|
|
\Illuminate\Support\Facades\Log::info('AUTH loginOrRegister with request url '.$lastPage); |
16
|
|
|
|
17
|
|
|
// Make sure they are not going to end up in a redirect loop with the login route |
18
|
|
|
if ($lastPage && $lastPage != route('login')) { |
19
|
|
|
$request->session()->put('oauthIntendedUrl', $lastPage); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return redirect()->guest(config('enterpriseauth.routes.login')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Route to clear the session and redirect to oauth signout handler |
26
|
|
|
public function logout(\Illuminate\Http\Request $request) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
auth()->logout(); |
29
|
|
|
return redirect(config('enterpriseauth.routes.logout')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Route to redirect to oauth idp end-session endpoint |
33
|
|
|
public function logoutFromOauthProvider(\Illuminate\Http\Request $request) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$endSessionEndpoint = $this->azureActiveDirectory->endSessionEndpoint; |
36
|
|
|
return redirect($endSessionEndpoint); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Route called to redirect administrative users to provide consent to access aad |
40
|
|
|
public function redirectToOauthAdminConsent(\Illuminate\Http\Request $request) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$url = $this->azureActiveDirectory->buildAdminConsentUrl(config('enterpriseauth.credentials.client_id'), |
43
|
|
|
config('enterpriseauth.credentials.callback_url')); |
44
|
|
|
//return new \Illuminate\Http\RedirectResponse($url); |
45
|
|
|
return redirect($url); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Route called to redirect unauthenticated users to oauth identity provider |
49
|
|
|
public function redirectToOauthProvider(\Illuminate\Http\Request $request) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$url = $this->buildAuthUrl(); |
52
|
|
|
//return new \Illuminate\Http\RedirectResponse($url); |
53
|
|
|
return redirect($url); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Helper to build redirect url from azure AD tenant |
57
|
|
|
public function buildAuthUrl() |
58
|
|
|
{ |
59
|
|
|
$url = $this->azureActiveDirectory->authorizationEndpoint |
60
|
|
|
.'?' |
61
|
|
|
.$this->buildAuthUrlQueryString(); |
62
|
|
|
|
63
|
|
|
return $url; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// helper to build query string for oauth provider |
67
|
|
|
public function buildAuthUrlQueryString() |
68
|
|
|
{ |
69
|
|
|
$fields = [ |
70
|
|
|
'client_id' => config('enterpriseauth.credentials.client_id'), |
71
|
|
|
'redirect_uri' => config('enterpriseauth.credentials.callback_url'), |
72
|
|
|
'scope' => 'https://graph.microsoft.com/.default', |
73
|
|
|
'response_type' => 'code', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
return http_build_query($fields); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Route to handle response back from our oauth provider |
80
|
|
|
public function handleOauthResponse(\Illuminate\Http\Request $request) |
81
|
|
|
{ |
82
|
|
|
// Handle user authentication responses |
83
|
|
|
if ($request->input('code')) { |
84
|
|
|
return $this->handleOauthLoginResponse($request); |
85
|
|
|
} |
86
|
|
|
if ($request->input('admin_consent')) { |
87
|
|
|
return 'Thank you'; |
88
|
|
|
} |
89
|
|
|
throw new \Exception('Unhandled oauth response'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function handleOauthLoginResponse(\Illuminate\Http\Request $request) |
93
|
|
|
{ |
94
|
|
|
// Turn coke into pepsi |
95
|
|
|
$accessToken = $this->getAccessTokenFromCode($request->input('code')); |
96
|
|
|
// Get the associated laravel \App\User object |
97
|
|
|
$user = $this->validateOauthCreateOrUpdateUserAndGroups($accessToken); |
98
|
|
|
// Authenticate the users session |
99
|
|
|
auth()->login($user, true); |
100
|
|
|
|
101
|
|
|
// Check to see if there is an intended destination url saved |
102
|
|
|
$destination = $request->session() |
103
|
|
|
->get('oauthIntendedUrl'); |
104
|
|
|
// If there is no intended destination url, use the default |
105
|
|
|
if (! $destination) { |
106
|
|
|
$destination = config('enterpriseauth.redirect_on_login'); |
107
|
|
|
} |
108
|
|
|
\Illuminate\Support\Facades\Log::info('AUTH success USER ID '.$user->id.' with redirect url '.$destination); |
109
|
|
|
|
110
|
|
|
return redirect($destination); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Turn coke into pepsi: Take the authorization code and turn it into an access token for graph api |
114
|
|
|
public function getAccessTokenFromCode($code) |
115
|
|
|
{ |
116
|
|
|
$guzzle = new \GuzzleHttp\Client(); |
117
|
|
|
$url = $this->azureActiveDirectory->tokenEndpoint; |
118
|
|
|
$parameters = [ |
119
|
|
|
'headers' => [ |
120
|
|
|
'Accept' => 'application/json', |
121
|
|
|
], |
122
|
|
|
'form_params' => [ |
123
|
|
|
'code' => $code, |
124
|
|
|
'scope' => 'https://graph.microsoft.com/.default', |
125
|
|
|
'client_id' => config('enterpriseauth.credentials.client_id'), |
126
|
|
|
'client_secret' => config('enterpriseauth.credentials.client_secret'), |
127
|
|
|
'redirect_uri' => config('enterpriseauth.credentials.callback_url'), |
128
|
|
|
'grant_type' => 'authorization_code', |
129
|
|
|
], |
130
|
|
|
]; |
131
|
|
|
$response = $guzzle->post($url, $parameters); |
132
|
|
|
$responseObject = json_decode($response->getBody()); |
133
|
|
|
$accessToken = $responseObject->access_token; |
134
|
|
|
|
135
|
|
|
return $accessToken; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.