1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Metaclassing\EnterpriseAuth\Controller; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use Laravel\Socialite\Facades\Socialite; |
7
|
|
|
|
8
|
|
|
class ApiAuthController extends AuthController |
9
|
|
|
{ |
10
|
|
|
// Helper to find a token wherever it is hidden and attempt to auth it |
11
|
|
|
public function extractOauthAccessTokenFromRequest(\Illuminate\Http\Request $request) |
12
|
|
|
{ |
13
|
|
|
$oauthAccessToken = ''; |
14
|
|
|
|
15
|
|
|
// IF we get an explicit TOKEN=abc123 in the $request |
16
|
|
|
if ($request->query('token')) { |
17
|
|
|
$oauthAccessToken = $request->query('token'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// IF posted as access_token=abc123 in the $request |
21
|
|
|
if ($request->input('access_token')) { |
22
|
|
|
$oauthAccessToken = $request->input('access_token'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// IF the request has an Authorization: Bearer abc123 header |
26
|
|
|
$header = $request->headers->get('authorization'); |
27
|
|
|
$regex = '/bearer\s+(\S+)/i'; |
28
|
|
|
if ($header && preg_match($regex, $header, $matches)) { |
29
|
|
|
$oauthAccessToken = $matches[1]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $oauthAccessToken; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Route to dump out the authenticated API user |
36
|
|
|
public function getAuthorizedUserInfo(\Illuminate\Http\Request $request) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$user = auth()->user(); |
39
|
|
|
|
40
|
|
|
return response()->json($user); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Route to dump out the authenticated users groups/roles |
44
|
|
|
public function getAuthorizedUserRoles(\Illuminate\Http\Request $request) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$user = auth()->user(); |
47
|
|
|
$roles = $user->roles()->get(); |
48
|
|
|
|
49
|
|
|
return response()->json($roles); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Route to dump out the authenticated users group/roles abilities/permissions |
53
|
|
|
public function getAuthorizedUserRolesAbilities(\Illuminate\Http\Request $request) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$user = auth()->user(); |
56
|
|
|
$roles = $user->roles()->get()->all(); |
57
|
|
|
foreach ($roles as $key => $role) { |
58
|
|
|
$role->permissions = $role->abilities()->get()->all(); |
59
|
|
|
if (! count($role->permissions)) { |
60
|
|
|
unset($roles[$key]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$roles = array_values($roles); |
64
|
|
|
|
65
|
|
|
return response()->json($roles); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.