Passed
Push — master ( 1164b2...5d6ac0 )
by meta
03:55
created

ApiAuthController::authenticateRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use Laravel\Socialite\Facades\Socialite;
7
8
class ApiAuthController extends AuthController
9
{
10
    public function authenticateRequest(\Illuminate\Http\Request $request)
11
    {
12
        $accessToken = $this->extractOauthAccessTokenFromRequest($request);
13
14
        // IF we got a token, prefer using that over cert auth
15
        if ($accessToken) {
16
            return $this->attemptTokenAuth($accessToken);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->attemptTokenAuth($accessToken) targeting Metaclassing\EnterpriseA...ler::attemptTokenAuth() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
17
        } else {
18
            return $this->attemptCertAuth();
19
        }
20
    }
21
22
    public function attemptTokenAuth($accessToken)
23
    {
24
        // Check the cache to see if this is a previously authenticated oauth access token
25
        $key = '/oauth/tokens/'.$accessToken;
26
        if ($accessToken && \Cache::has($key)) {
27
            $user = \Cache::get($key);
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
28
        // Check to see if they have newly authenticated with an oauth access token
29
        } else {
30
            try {
31
                $this->user = $this->validateOauthCreateOrUpdateUserAndGroups($accessToken);
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
            } catch (\Exception $e) {
33
                //echo 'token auth error: '.$e->getMessage();
34
            }
35
        }
36
    }
37
38
    public function attemptCertAuth()
39
    {
40
        try {
41
            return $apiAuthController->certAuth();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiAuthController seems to be never defined.
Loading history...
42
        } catch (\Exception $e) {
43
            //echo 'cert auth error: '.$e->getMessage();
44
        }
45
    }
46
47
    // Helper to find a token wherever it is hidden and attempt to auth it
48
    public function extractOauthAccessTokenFromRequest(\Illuminate\Http\Request $request)
49
    {
50
        $oauthAccessToken = '';
51
52
        // IF we get an explicit TOKEN=abc123 in the $request
53
        if ($request->query('token')) {
54
            $oauthAccessToken = $request->query('token');
55
        }
56
57
        // IF posted as access_token=abc123 in the $request
58
        if ($request->input('access_token')) {
59
            $oauthAccessToken = $request->input('access_token');
60
        }
61
62
        // IF the request has an Authorization: Bearer abc123 header
63
        $header = $request->headers->get('authorization');
64
        $regex = '/bearer\s+(\S+)/i';
65
        if ($header && preg_match($regex, $header, $matches)) {
66
            $oauthAccessToken = $matches[1];
67
        }
68
69
        return $oauthAccessToken;
70
    }
71
72
    // Route to dump out the authenticated API user
73
    public function getAuthorizedUserInfo(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    public function getAuthorizedUserInfo(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        $user = auth()->user();
76
77
        return response()->json($user);
78
    }
79
80
    // Route to dump out the authenticated users groups/roles
81
    public function getAuthorizedUserRoles(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
    public function getAuthorizedUserRoles(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $user = auth()->user();
84
        $roles = $user->roles()->get();
85
86
        return response()->json($roles);
87
    }
88
89
    // Route to dump out the authenticated users group/roles abilities/permissions
90
    public function getAuthorizedUserRolesAbilities(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

90
    public function getAuthorizedUserRolesAbilities(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        $user = auth()->user();
93
        $roles = $user->roles()->get()->all();
94
        foreach ($roles as $key => $role) {
95
            $role->permissions = $role->abilities()->get()->all();
96
            if (! count($role->permissions)) {
97
                unset($roles[$key]);
98
            }
99
        }
100
        $roles = array_values($roles);
101
102
        return response()->json($roles);
103
    }
104
}
105