1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Auth;
|
4
|
|
5
|
use Illuminate\Http\Request;
|
6
|
use Illuminate\Contracts\Auth\Guard;
|
7
|
use Illuminate\Contracts\Auth\UserProvider;
|
8
|
|
9
|
class TokenGuard implements Guard
|
10
|
{
|
11
|
use GuardHelpers;
|
12
|
|
13
|
/**
|
14
|
* The request instance.
|
15
|
*
|
16
|
* @var \Illuminate\Http\Request
|
17
|
*/
|
18
|
protected $request;
|
19
|
|
20
|
/**
|
21
|
* The name of the field on the request containing the API token.
|
22
|
*
|
23
|
* @var string
|
24
|
*/
|
25
|
protected $inputKey;
|
26
|
|
27
|
/**
|
28
|
* The name of the token "column" in persistent storage.
|
29
|
*
|
30
|
* @var string
|
31
|
*/
|
32
|
protected $storageKey;
|
33
|
|
34
|
/**
|
35
|
* Create a new authentication guard.
|
36
|
*
|
37
|
* @param \Illuminate\Contracts\Auth\UserProvider $provider
|
38
|
* @param \Illuminate\Http\Request $request
|
39
|
* @return void
|
40
|
*/
|
41
|
public function __construct(UserProvider $provider, Request $request)
|
42
|
{
|
43
|
$this->request = $request;
|
44
|
$this->provider = $provider;
|
45
|
$this->inputKey = 'api_token';
|
46
|
$this->storageKey = 'api_token';
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Get the currently authenticated user.
|
51
|
*
|
52
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
53
|
*/
|
54
|
public function user()
|
55
|
{
|
56
|
// If we've already retrieved the user for the current request we can just
|
57
|
// return it back immediately. We do not want to fetch the user data on
|
58
|
// every call to this method because that would be tremendously slow.
|
59
|
if (! is_null($this->user)) {
|
60
|
return $this->user;
|
61
|
}
|
62
|
|
63
|
$user = null;
|
64
|
|
65
|
$token = $this->getTokenForRequest();
|
66
|
|
67
|
if (! empty($token)) {
|
68
|
$user = $this->provider->retrieveByCredentials(
|
69
|
[$this->storageKey => $token]
|
70
|
);
|
71
|
}
|
72
|
|
73
|
return $this->user = $user;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Get the token for the current request.
|
78
|
*
|
79
|
* @return string
|
80
|
*/
|
81
|
protected function getTokenForRequest()
|
82
|
{
|
83
|
$token = $this->request->input($this->inputKey);
|
84
|
|
85
|
if (empty($token)) {
|
86
|
$token = $this->request->bearerToken();
|
87
|
}
|
88
|
|
89
|
if (empty($token)) {
|
90
|
$token = $this->request->getPassword();
|
91
|
}
|
92
|
|
93
|
return $token;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Validate a user's credentials.
|
98
|
*
|
99
|
* @param array $credentials
|
100
|
* @return bool
|
101
|
*/
|
102
|
public function validate(array $credentials = [])
|
103
|
{
|
104
|
if (empty($credentials[$this->inputKey])) {
|
105
|
return false;
|
106
|
}
|
107
|
|
108
|
$credentials = [$this->storageKey => $credentials[$this->inputKey]];
|
109
|
|
110
|
if ($this->provider->retrieveByCredentials($credentials)) {
|
111
|
return true;
|
112
|
}
|
113
|
|
114
|
return false;
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Set the current request instance.
|
119
|
*
|
120
|
* @param \Illuminate\Http\Request $request
|
121
|
* @return $this
|
122
|
*/
|
123
|
public function setRequest(Request $request)
|
124
|
{
|
125
|
$this->request = $request;
|
126
|
|
127
|
return $this;
|
128
|
}
|
129
|
}
|