1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Gertoska\OAuth2Request\Credential; |
6
|
|
|
|
7
|
|
|
final class Credential |
8
|
|
|
{ |
9
|
|
|
private $uri; |
10
|
|
|
private $authorization; |
11
|
|
|
private $grantType; |
12
|
|
|
private $username; |
13
|
|
|
private $password; |
14
|
|
|
private $accessToken; |
15
|
|
|
private $tokenType; |
16
|
|
|
private $refreshToken; |
17
|
|
|
private $expiresIn; |
18
|
|
|
private $scope; |
19
|
|
|
private $obtainedIn; |
20
|
|
|
|
21
|
16 |
|
public function __construct( |
22
|
|
|
string $uri, |
23
|
|
|
string $authorization, |
24
|
|
|
string $grantType, |
25
|
|
|
string $username, |
26
|
|
|
string $password, |
27
|
|
|
string $accessToken = null, |
28
|
|
|
string $tokenType = null, |
29
|
|
|
string $refreshToken = null, |
30
|
|
|
int $expiresIn = null, |
31
|
|
|
string $scope = null, |
32
|
|
|
int $obtainedIn = null |
33
|
|
|
) { |
34
|
16 |
|
$this->uri = $uri; |
35
|
16 |
|
$this->authorization = $authorization; |
36
|
16 |
|
$this->grantType = $grantType; |
37
|
16 |
|
$this->username = $username; |
38
|
16 |
|
$this->password = $password; |
39
|
16 |
|
$this->accessToken = $accessToken; |
40
|
16 |
|
$this->tokenType = $tokenType; |
41
|
16 |
|
$this->refreshToken = $refreshToken; |
42
|
16 |
|
$this->expiresIn = $expiresIn; |
43
|
16 |
|
$this->scope = $scope; |
44
|
16 |
|
$this->obtainedIn = $obtainedIn; |
45
|
16 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function uri(): string |
48
|
|
|
{ |
49
|
1 |
|
return $this->uri; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function authorization(): string |
53
|
|
|
{ |
54
|
1 |
|
$auth = $this->authorization; |
55
|
|
|
|
56
|
1 |
|
return (base64_encode(base64_decode($auth)) !== $auth) ? base64_encode($auth) : $auth; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function grantType(): string |
60
|
|
|
{ |
61
|
1 |
|
return $this->grantType; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function username(): string |
65
|
|
|
{ |
66
|
1 |
|
return $this->username; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function password(): string |
70
|
|
|
{ |
71
|
1 |
|
return $this->password; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function accessToken(): ?string |
75
|
|
|
{ |
76
|
3 |
|
return $this->accessToken; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function tokenType(): ?string |
80
|
|
|
{ |
81
|
1 |
|
return $this->tokenType; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function refreshToken(): ?string |
85
|
|
|
{ |
86
|
1 |
|
return $this->refreshToken; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function expiresIn(): ?int |
90
|
|
|
{ |
91
|
1 |
|
return $this->expiresIn; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function scope(): ?string |
95
|
|
|
{ |
96
|
1 |
|
return $this->scope; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function obtainedIn(): ?int |
100
|
|
|
{ |
101
|
1 |
|
return $this->obtainedIn; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function setOAuth( |
105
|
|
|
string $accessToken, |
106
|
|
|
string $tokenType, |
107
|
|
|
string $refreshToken, |
108
|
|
|
int $expiresIn, |
109
|
|
|
string $scope, |
110
|
|
|
int $obtainedIn |
111
|
|
|
): Credential { |
112
|
1 |
|
$this->accessToken = $accessToken; |
113
|
1 |
|
$this->tokenType = $tokenType; |
114
|
1 |
|
$this->refreshToken = $refreshToken; |
115
|
1 |
|
$this->expiresIn = $expiresIn; |
116
|
1 |
|
$this->scope = $scope; |
117
|
1 |
|
$this->obtainedIn = $obtainedIn; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
public function checkIfIsNotNecessaryToRefreshToken(): bool |
123
|
|
|
{ |
124
|
2 |
|
return null !== $this->accessToken() && !$this->hasExpired(); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
public function hasExpired(): bool |
128
|
|
|
{ |
129
|
1 |
|
return null === $this->expiresIn || microtime(true) - $this->obtainedIn >= $this->expiresIn; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|