1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Http; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Auth |
9
|
|
|
* |
10
|
|
|
* @author Rafael Teixeira <[email protected]> |
11
|
|
|
* @package WeDevBr\Bankly |
12
|
|
|
*/ |
13
|
|
|
class Auth |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** @var self */ |
16
|
|
|
private static $login; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $loginUrl; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $clientId; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $clientSecret; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $grantType = 'client_credentials'; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $token; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $tokenExpiry; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return void |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
private function __construct() |
40
|
|
|
{ |
41
|
|
|
// |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the instance of this class |
46
|
|
|
* |
47
|
|
|
* @param string|null $loginUrl |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
|
|
public static function login($loginUrl = null) |
51
|
|
|
{ |
52
|
|
|
if (is_null(self::$login)) { |
53
|
|
|
self::$login = new Auth(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (is_null($loginUrl)) { |
57
|
|
|
self::$login->loginUrl = config('bankly')['login_url']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return self::$login; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public function setClientCredentials() |
67
|
|
|
{ |
68
|
|
|
$this->clientId = $this->clientId ?? config('bankly')['client_id']; |
69
|
|
|
$this->clientSecret = $this->clientSecret ?? config('bankly')['client_secret']; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param null|string $clientId |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
|
|
public function setClientId($clientId) |
78
|
|
|
{ |
79
|
|
|
$this->clientId = $clientId; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param null|string $clientSecret |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
public function setClientSecret($clientSecret) |
88
|
|
|
{ |
89
|
|
|
$this->clientSecret = $clientSecret; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $grantType |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function setGrantType(string $grantType) |
98
|
|
|
{ |
99
|
|
|
$this->grantType = $grantType; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getToken() |
107
|
|
|
{ |
108
|
|
|
if (now()->unix() > $this->tokenExpiry || !$this->token) { |
109
|
|
|
$this->auth(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->token; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
private function auth(): void |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->setClientCredentials(); |
121
|
|
|
|
122
|
|
|
//TODO: Add auth for username and password |
123
|
|
|
$body = [ |
124
|
|
|
'grant_type' => $this->grantType, |
125
|
|
|
'client_secret' => $this->clientSecret, |
126
|
|
|
'client_id' => $this->clientId |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
$response = Http::asForm()->post($this->loginUrl, $body)->throw()->json(); |
130
|
|
|
$this->token = $response['access_token']; |
131
|
|
|
$this->tokenExpiry = now()->addSeconds($response['expires_in'])->unix(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|