|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Oauth\Http\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Illuminate\Http\JsonResponse; |
|
10
|
|
|
use Rinvex\Oauth\Factories\ApiTokenCookieFactory; |
|
11
|
|
|
|
|
12
|
|
|
class CreateFreshApiToken |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The API token cookie factory instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \Rinvex\Oauth\Factories\ApiTokenCookieFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $cookieFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The authentication guard. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $guard; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create a new middleware instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Rinvex\Oauth\Factories\ApiTokenCookieFactory $cookieFactory |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(ApiTokenCookieFactory $cookieFactory) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cookieFactory = $cookieFactory; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Handle an incoming request. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Illuminate\Http\Request $request |
|
44
|
|
|
* @param \Closure $next |
|
45
|
|
|
* @param string|null $guard |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle($request, Closure $next, $guard = null) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->guard = $guard; |
|
52
|
|
|
|
|
53
|
|
|
$response = $next($request); |
|
54
|
|
|
|
|
55
|
|
|
if ($this->shouldReceiveFreshToken($request, $response)) { |
|
56
|
|
|
$response->withCookie($this->cookieFactory->make( |
|
57
|
|
|
$request->user($this->guard)->getAuthIdentifier(), |
|
58
|
|
|
$request->session()->token() |
|
|
|
|
|
|
59
|
|
|
)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $response; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Determine if the given request should receive a fresh token. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Illuminate\Http\Request $request |
|
69
|
|
|
* @param \Illuminate\Http\Response $response |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function shouldReceiveFreshToken($request, $response) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->requestShouldReceiveFreshToken($request) && |
|
76
|
|
|
$this->responseShouldReceiveFreshToken($response); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Determine if the request should receive a fresh token. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Illuminate\Http\Request $request |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function requestShouldReceiveFreshToken($request) |
|
87
|
|
|
{ |
|
88
|
|
|
return $request->isMethod('GET') && $request->user($this->guard); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Determine if the response should receive a fresh token. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Illuminate\Http\Response $response |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function responseShouldReceiveFreshToken($response) |
|
99
|
|
|
{ |
|
100
|
|
|
return ($response instanceof Response || |
|
101
|
|
|
$response instanceof JsonResponse) && |
|
102
|
|
|
! $this->alreadyContainsToken($response); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Determine if the given response already contains an API token. |
|
107
|
|
|
* |
|
108
|
|
|
* This avoids us overwriting a just "refreshed" token. |
|
109
|
|
|
* |
|
110
|
|
|
* @param \Illuminate\Http\Response $response |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function alreadyContainsToken($response) |
|
115
|
|
|
{ |
|
116
|
|
|
foreach ($response->headers->getCookies() as $cookie) { |
|
117
|
|
|
if ($cookie->getName() === config('rinvex.oauth.cookie')) { |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Adding a
@returnannotation 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.