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 |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct(UserRepositoryContract $provider, Request $request) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->request = $request; |
58
|
|
|
$this->provider = $provider; |
|
|
|
|
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( |
|
|
|
|
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 |
|
|
|
|
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)) { |
|
|
|
|
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) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$this->request = $request; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.