|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Oauth\Factories; |
|
6
|
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Firebase\JWT\JWT; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
10
|
|
|
use Illuminate\Contracts\Encryption\Encrypter; |
|
11
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
|
12
|
|
|
|
|
13
|
|
|
class ApiTokenCookieFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The configuration repository implementation. |
|
17
|
|
|
* |
|
18
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The encrypter implementation. |
|
24
|
|
|
* |
|
25
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $encrypter; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create an API token cookie factory instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
|
33
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Config $config, Encrypter $encrypter) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->config = $config; |
|
40
|
|
|
$this->encrypter = $encrypter; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new API token cookie. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $userId |
|
47
|
|
|
* @param string $csrfToken |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Symfony\Component\HttpFoundation\Cookie |
|
50
|
|
|
*/ |
|
51
|
|
|
public function make($userId, $csrfToken) |
|
52
|
|
|
{ |
|
53
|
|
|
$config = $this->config->get('session'); |
|
54
|
|
|
|
|
55
|
|
|
$expiration = Carbon::now()->addMinutes($config['lifetime']); |
|
56
|
|
|
|
|
57
|
|
|
return new Cookie( |
|
58
|
|
|
config('rinvex.oauth.cookie'), |
|
59
|
|
|
$this->createToken($userId, $csrfToken, $expiration), |
|
60
|
|
|
$expiration, |
|
|
|
|
|
|
61
|
|
|
$config['path'], |
|
62
|
|
|
$config['domain'], |
|
63
|
|
|
$config['secure'], |
|
64
|
|
|
true, |
|
65
|
|
|
false, |
|
66
|
|
|
$config['same_site'] ?? null |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Create a new JWT token for the given user ID and CSRF token. |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $userId |
|
74
|
|
|
* @param string $csrfToken |
|
75
|
|
|
* @param \Carbon\Carbon $expiration |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function createToken($userId, $csrfToken, Carbon $expiration) |
|
80
|
|
|
{ |
|
81
|
|
|
return JWT::encode([ |
|
82
|
|
|
'sub' => $userId, |
|
83
|
|
|
'csrf' => $csrfToken, |
|
84
|
|
|
'expiry' => $expiration->getTimestamp(), |
|
85
|
|
|
], $this->encrypter->getKey()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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.