1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erdemkeren\TemporaryAccess; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use LogicException; |
7
|
|
|
use Erdemkeren\TemporaryAccess\Token\TokenInterface; |
8
|
|
|
use Erdemkeren\TemporaryAccess\Contracts\AccessTokenInterface; |
9
|
|
|
|
10
|
|
|
final class GenericAccessToken implements AccessTokenInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The token. |
14
|
|
|
* |
15
|
|
|
* @var TokenInterface |
16
|
|
|
*/ |
17
|
|
|
private $token; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The attributes of the access token. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $attributes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* GenericAccessToken constructor. |
28
|
|
|
* |
29
|
|
|
* @param TokenInterface $token The access token. |
30
|
|
|
* @param array $attributes The attributes of the access token. |
31
|
|
|
*/ |
32
|
|
|
public function __construct(TokenInterface $token, array $attributes) |
33
|
|
|
{ |
34
|
|
|
$this->token = $token; |
35
|
|
|
$this->attributes = $attributes; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the unique identifier of the authenticatable who owns the access token. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function authenticatableId() |
44
|
|
|
{ |
45
|
|
|
return $this->getAttributeValue('authenticatable_id'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the token. |
50
|
|
|
* |
51
|
|
|
* @return TokenInterface |
52
|
|
|
*/ |
53
|
|
|
public function token() |
54
|
|
|
{ |
55
|
|
|
return $this->token; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the access token as plain text. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* @throws LogicException |
63
|
|
|
*/ |
64
|
|
|
public function plain() |
65
|
|
|
{ |
66
|
|
|
if (! $plainText = $this->token->plain()) { |
67
|
|
|
$message = 'The plain text is not available at this state.'; |
68
|
|
|
throw new LogicException($message); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $plainText; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the access token encrypted. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function encrypted() |
80
|
|
|
{ |
81
|
|
|
return (string) $this->token(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the created at timestamp of the access token. |
86
|
|
|
* |
87
|
|
|
* @return \Carbon\Carbon |
88
|
|
|
*/ |
89
|
|
|
public function createdAt() |
90
|
|
|
{ |
91
|
|
|
return new Carbon($this->getAttributeValue('created_at')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the expires at timestamp of the access token. |
96
|
|
|
* |
97
|
|
|
* @return \Carbon\Carbon |
98
|
|
|
*/ |
99
|
|
|
public function expiresAt() |
100
|
|
|
{ |
101
|
|
|
return new Carbon($this->getAttributeValue('expires_at')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get a new instance of the access token with a longer expire date. |
106
|
|
|
* |
107
|
|
|
* @param int $prolong The prolong time in seconds. |
108
|
|
|
* |
109
|
|
|
* @return GenericAccessToken |
110
|
|
|
*/ |
111
|
|
|
public function prolong($prolong) |
112
|
|
|
{ |
113
|
|
|
return new static($this->token(), [ |
114
|
|
|
'authenticatable_id' => $this->authenticatableId(), |
115
|
|
|
'created_at' => $this->createdAt(), |
116
|
|
|
'expires_at' => (string) $this->expiresAt()->addSeconds($prolong), |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the value of the given key. |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
private function getAttributeValue($key) |
128
|
|
|
{ |
129
|
|
|
return $this->attributes[$key]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Convert the access token to string. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function __toString() |
138
|
|
|
{ |
139
|
|
|
return $this->encrypted(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|