1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tbclla\Revolut\Auth; |
4
|
|
|
|
5
|
|
|
use tbclla\Revolut\Auth\Token; |
6
|
|
|
use tbclla\Revolut\Interfaces\GrantsAccessTokens; |
7
|
|
|
use tbclla\Revolut\Interfaces\PersistableToken; |
8
|
|
|
|
9
|
|
|
class RefreshToken extends Token implements GrantsAccessTokens, PersistableToken |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The type of the token |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const TYPE = 'refresh_token'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The grant type of the token |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const GRANT_TYPE = 'refresh_token'; |
24
|
|
|
|
25
|
|
|
public function getValue() |
26
|
|
|
{ |
27
|
|
|
return $this->value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getType() |
31
|
|
|
{ |
32
|
|
|
return self::TYPE; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getGrantType() |
36
|
|
|
{ |
37
|
|
|
return self::GRANT_TYPE; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function getExpiration() |
41
|
|
|
{ |
42
|
|
|
return config('revolut.expire_api_access', false) |
43
|
|
|
? self::PSD2expiration() |
44
|
|
|
: null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Delete all expired refresh tokens |
49
|
|
|
* |
50
|
|
|
* @return int The number of deleted tokens |
51
|
|
|
*/ |
52
|
|
|
public static function clearExpired() |
53
|
|
|
{ |
54
|
|
|
$latest = self::latest()->select('id')->first(); |
55
|
|
|
|
56
|
|
|
return (int) self::where('id', '!=', $latest->id)->delete(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Calculate the expiration date |
61
|
|
|
* |
62
|
|
|
* An expiration should be set if the account is subject to PSD2 regulation, |
63
|
|
|
* under which access to the API expires after 90 days. |
64
|
|
|
* When access to the API expires, existing access tokens may be revoked. |
65
|
|
|
* The refresh token should be treated as expired premajurely, to prevent it from |
66
|
|
|
* being used to request access tokens which may be revoked before their default |
67
|
|
|
* lifetime has expired. |
68
|
|
|
* |
69
|
|
|
* @see https://developer.revolut.com/docs/business-api/#getting-started-usage-and-limits |
70
|
|
|
* @return \Carbon\Carbon |
71
|
|
|
*/ |
72
|
|
|
private static function PSD2expiration() |
73
|
|
|
{ |
74
|
|
|
return now()->addDays(90)->subMinutes(AccessToken::TTL); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|