-
Notifications
You must be signed in to change notification settings - Fork 113
/
Token.php
84 lines (68 loc) · 1.85 KB
/
Token.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace App\Jwt;
use App\Contracts\JwtTokenInterface;
use Illuminate\Support\Collection;
class Token implements JwtTokenInterface
{
/** User-supplied signature */
private ?string $signature;
/** @var Collection<string, mixed> */
private Collection $header;
/** @var Collection<string, mixed> */
private Collection $payload;
/**
* Token constructor.
*
* @param array<string, mixed>|null $headers
* @param array<string, mixed>|null $claims
* @param string|null $signature User-supplied signature
*/
public function __construct(
array $headers = null,
array $claims = null,
string $signature = null)
{
$this->header = collect(
array_merge($this->getDefaultHeaders(), $headers ?? [])
);
$this->payload = collect($claims);
$this->signature = $signature;
}
public function getDefaultHeaders(): array
{
$headers = config('jwt.headers');
return is_array($headers) ? $headers : [];
}
public function headers(): Collection
{
return clone $this->header;
}
public function claims(): Collection
{
return clone $this->payload;
}
public function getUserSignature(): ?string
{
return $this->signature;
}
public function putToPayload(string $key, mixed $value): void
{
$this->payload->put($key, $value);
}
public function putToHeader(string $key, mixed $value): void
{
$this->header->put($key, $value);
}
public function setUserSignature(string $signature): void
{
$this->signature = $signature;
}
public function getSubject(): mixed
{
return $this->payload->get('sub');
}
public function getExpiration(): int
{
return (int) $this->payload->get('exp');
}
}