Completed
Push — master ( b42a23...f9e78a )
by Abdelrahman
07:26
created

TokenGuard::user()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Guards;
17
18
use Illuminate\Http\Request;
19
use Illuminate\Auth\GuardHelpers;
20
use Illuminate\Contracts\Auth\Guard;
21
use Rinvex\Fort\Contracts\UserRepositoryContract;
22
23
class TokenGuard implements Guard
24
{
25
    use GuardHelpers;
26
27
    /**
28
     * The request instance.
29
     *
30
     * @var \Illuminate\Http\Request
31
     */
32
    protected $request;
33
34
    /**
35
     * The name of the query string item from the request containing the API token.
36
     *
37
     * @var string
38
     */
39
    protected $inputKey;
40
41
    /**
42
     * The name of the token "column" in persistent storage.
43
     *
44
     * @var string
45
     */
46
    protected $storageKey;
47
48
    /**
49
     * Create a new authentication guard.
50
     *
51
     * @param  \Rinvex\Fort\Contracts\UserRepositoryContract  $provider
52
     * @param  \Illuminate\Http\Request  $request
53
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
54
     */
55
    public function __construct(UserRepositoryContract $provider, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
56
    {
57
        $this->request = $request;
58
        $this->provider = $provider;
0 ignored issues
show
Documentation Bug introduced by
It seems like $provider of type object<Rinvex\Fort\Contr...UserRepositoryContract> is incompatible with the declared type object<Illuminate\Contracts\Auth\UserProvider> of property $provider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->inputKey = 'api_token';
60
        $this->storageKey = 'api_token';
61
    }
62
63
    /**
64
     * Get the currently authenticated user.
65
     *
66
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
67
     */
68
    public function user()
69
    {
70
        // If we've already retrieved the user for the current request we can just
71
        // return it back immediately. We do not want to fetch the user data on
72
        // every call to this method because that would be tremendously slow.
73
        if (! is_null($this->user)) {
74
            return $this->user;
75
        }
76
77
        $user = null;
78
79
        $token = $this->getTokenForRequest();
80
81
        if (! empty($token)) {
82
            $user = $this->provider->findByCredentials(
0 ignored issues
show
Bug introduced by
The method findByCredentials() does not seem to exist on object<Illuminate\Contracts\Auth\UserProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                [$this->storageKey => $token]
84
            );
85
        }
86
87
        return $this->user = $user;
88
    }
89
90
    /**
91
     * Get the token for the current request.
92
     *
93
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
94
     */
95
    public function getTokenForRequest()
96
    {
97
        $token = $this->request->query($this->inputKey);
98
99
        if (empty($token)) {
100
            $token = $this->request->input($this->inputKey);
101
        }
102
103
        if (empty($token)) {
104
            $token = $this->request->bearerToken();
105
        }
106
107
        if (empty($token)) {
108
            $token = $this->request->getPassword();
109
        }
110
111
        return $token;
112
    }
113
114
    /**
115
     * Validate a user's credentials.
116
     *
117
     * @param  array  $credentials
118
     * @return bool
119
     */
120
    public function validate(array $credentials = [])
121
    {
122
        if (empty($credentials[$this->inputKey])) {
123
            return false;
124
        }
125
126
        $credentials = [$this->storageKey => $credentials[$this->inputKey]];
127
128
        if ($this->provider->findByCredentials($credentials)) {
0 ignored issues
show
Bug introduced by
The method findByCredentials() does not seem to exist on object<Illuminate\Contracts\Auth\UserProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $this->pro...dentials($credentials);.
Loading history...
129
            return true;
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * Set the current request instance.
137
     *
138
     * @param  \Illuminate\Http\Request  $request
139
     * @return $this
140
     */
141
    public function setRequest(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
142
    {
143
        $this->request = $request;
144
145
        return $this;
146
    }
147
}
148