| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Laravel\Sanctum;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Auth\Factory as AuthFactory;
|
|
|
6 |
use Illuminate\Http\Request;
|
|
|
7 |
use Illuminate\Support\Arr;
|
|
|
8 |
use Laravel\Sanctum\Events\TokenAuthenticated;
|
|
|
9 |
|
|
|
10 |
class Guard
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* The authentication factory implementation.
|
|
|
14 |
*
|
|
|
15 |
* @var \Illuminate\Contracts\Auth\Factory
|
|
|
16 |
*/
|
|
|
17 |
protected $auth;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* The number of minutes tokens should be allowed to remain valid.
|
|
|
21 |
*
|
|
|
22 |
* @var int
|
|
|
23 |
*/
|
|
|
24 |
protected $expiration;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* The provider name.
|
|
|
28 |
*
|
|
|
29 |
* @var string
|
|
|
30 |
*/
|
|
|
31 |
protected $provider;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Create a new guard instance.
|
|
|
35 |
*
|
|
|
36 |
* @param \Illuminate\Contracts\Auth\Factory $auth
|
|
|
37 |
* @param int $expiration
|
|
|
38 |
* @param string $provider
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(AuthFactory $auth, $expiration = null, $provider = null)
|
|
|
42 |
{
|
|
|
43 |
$this->auth = $auth;
|
|
|
44 |
$this->expiration = $expiration;
|
|
|
45 |
$this->provider = $provider;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Retrieve the authenticated user for the incoming request.
|
|
|
50 |
*
|
|
|
51 |
* @param \Illuminate\Http\Request $request
|
|
|
52 |
* @return mixed
|
|
|
53 |
*/
|
|
|
54 |
public function __invoke(Request $request)
|
|
|
55 |
{
|
|
|
56 |
foreach (Arr::wrap(config('sanctum.guard', 'web')) as $guard) {
|
|
|
57 |
if ($user = $this->auth->guard($guard)->user()) {
|
|
|
58 |
return $this->supportsTokens($user)
|
|
|
59 |
? $user->withAccessToken(new TransientToken)
|
|
|
60 |
: $user;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if ($token = $this->getTokenFromRequest($request)) {
|
|
|
65 |
$model = Sanctum::$personalAccessTokenModel;
|
|
|
66 |
|
|
|
67 |
$accessToken = $model::findToken($token);
|
|
|
68 |
|
|
|
69 |
if (! $this->isValidAccessToken($accessToken) ||
|
|
|
70 |
! $this->supportsTokens($accessToken->tokenable)) {
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$tokenable = $accessToken->tokenable->withAccessToken(
|
|
|
75 |
$accessToken
|
|
|
76 |
);
|
|
|
77 |
|
|
|
78 |
event(new TokenAuthenticated($accessToken));
|
|
|
79 |
|
|
|
80 |
if (method_exists($accessToken->getConnection(), 'hasModifiedRecords') &&
|
|
|
81 |
method_exists($accessToken->getConnection(), 'setRecordModificationState')) {
|
|
|
82 |
tap($accessToken->getConnection()->hasModifiedRecords(), function ($hasModifiedRecords) use ($accessToken) {
|
|
|
83 |
$accessToken->forceFill(['last_used_at' => now()])->save();
|
|
|
84 |
|
|
|
85 |
$accessToken->getConnection()->setRecordModificationState($hasModifiedRecords);
|
|
|
86 |
});
|
|
|
87 |
} else {
|
|
|
88 |
$accessToken->forceFill(['last_used_at' => now()])->save();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return $tokenable;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Determine if the tokenable model supports API tokens.
|
|
|
97 |
*
|
|
|
98 |
* @param mixed $tokenable
|
|
|
99 |
* @return bool
|
|
|
100 |
*/
|
|
|
101 |
protected function supportsTokens($tokenable = null)
|
|
|
102 |
{
|
|
|
103 |
return $tokenable && in_array(HasApiTokens::class, class_uses_recursive(
|
|
|
104 |
get_class($tokenable)
|
|
|
105 |
));
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Get the token from the request.
|
|
|
110 |
*
|
|
|
111 |
* @param \Illuminate\Http\Request $request
|
|
|
112 |
* @return string|null
|
|
|
113 |
*/
|
|
|
114 |
protected function getTokenFromRequest(Request $request)
|
|
|
115 |
{
|
|
|
116 |
if (is_callable(Sanctum::$accessTokenRetrievalCallback)) {
|
|
|
117 |
return (string) (Sanctum::$accessTokenRetrievalCallback)($request);
|
|
|
118 |
}
|
|
|
119 |
|
| 150 |
lars |
120 |
$token = $request->bearerToken();
|
|
|
121 |
|
|
|
122 |
return $this->isValidBearerToken($token) ? $token : null;
|
| 148 |
lars |
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
| 150 |
lars |
126 |
* Determine if the bearer token is in the correct format.
|
|
|
127 |
*
|
|
|
128 |
* @param string|null $token
|
|
|
129 |
* @return bool
|
|
|
130 |
*/
|
|
|
131 |
protected function isValidBearerToken(string $token = null)
|
|
|
132 |
{
|
|
|
133 |
if (! is_null($token) && str_contains($token, '|')) {
|
|
|
134 |
$model = new Sanctum::$personalAccessTokenModel;
|
|
|
135 |
|
|
|
136 |
if ($model->getKeyType() === 'int') {
|
|
|
137 |
[$id, $token] = explode('|', $token, 2);
|
|
|
138 |
|
|
|
139 |
return ctype_digit($id) && ! empty($token);
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
return ! empty($token);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
| 148 |
lars |
147 |
* Determine if the provided access token is valid.
|
|
|
148 |
*
|
|
|
149 |
* @param mixed $accessToken
|
|
|
150 |
* @return bool
|
|
|
151 |
*/
|
|
|
152 |
protected function isValidAccessToken($accessToken): bool
|
|
|
153 |
{
|
|
|
154 |
if (! $accessToken) {
|
|
|
155 |
return false;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$isValid =
|
|
|
159 |
(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
|
|
|
160 |
&& (! $accessToken->expires_at || ! $accessToken->expires_at->isPast())
|
|
|
161 |
&& $this->hasValidProvider($accessToken->tokenable);
|
|
|
162 |
|
|
|
163 |
if (is_callable(Sanctum::$accessTokenAuthenticationCallback)) {
|
|
|
164 |
$isValid = (bool) (Sanctum::$accessTokenAuthenticationCallback)($accessToken, $isValid);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
return $isValid;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Determine if the tokenable model matches the provider's model type.
|
|
|
172 |
*
|
|
|
173 |
* @param \Illuminate\Database\Eloquent\Model $tokenable
|
|
|
174 |
* @return bool
|
|
|
175 |
*/
|
|
|
176 |
protected function hasValidProvider($tokenable)
|
|
|
177 |
{
|
|
|
178 |
if (is_null($this->provider)) {
|
|
|
179 |
return true;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$model = config("auth.providers.{$this->provider}.model");
|
|
|
183 |
|
|
|
184 |
return $tokenable instanceof $model;
|
|
|
185 |
}
|
|
|
186 |
}
|