1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kobas\APIClient\Auth; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ConnectException; |
6
|
|
|
use Kobas\APIClient\Exception\AuthenticationException; |
7
|
|
|
use Kobas\APIClient\Exception\CurlException; |
8
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
9
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
10
|
|
|
use Kobas\OAuth2\Client\Provider\Kobas as OAuthProvider; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class OAuthSigner |
14
|
|
|
* |
15
|
|
|
* @package Kobas\APIClient\Auth |
16
|
|
|
*/ |
17
|
|
|
class Provider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var AccessToken[] |
21
|
|
|
*/ |
22
|
|
|
protected static $tokens; |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $companyId; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $clientId; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $clientSecret; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $scopes; |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $url; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* OAuthSigner constructor. |
46
|
|
|
* |
47
|
|
|
* @param int $companyId |
48
|
|
|
* @param string $clientId |
49
|
|
|
* @param string $clientSecret |
50
|
|
|
* @param string $scopes |
51
|
|
|
* @param string $url |
52
|
|
|
*/ |
53
|
15 |
|
public function __construct($companyId, $clientId, $clientSecret, $scopes, $url = 'https://oauth.kobas.co.uk') |
54
|
|
|
{ |
55
|
15 |
|
$this->companyId = (int)$companyId; |
56
|
15 |
|
$this->clientId = (string)$clientId; |
57
|
15 |
|
$this->clientSecret = (string)$clientSecret; |
58
|
15 |
|
$this->scopes = (string)$scopes; |
59
|
15 |
|
$this->url = (string)$url; |
60
|
15 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public function getUrl() |
66
|
|
|
{ |
67
|
3 |
|
return $this->url; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $url |
72
|
|
|
*/ |
73
|
3 |
|
public function setUrl($url) |
74
|
|
|
{ |
75
|
3 |
|
$this->url = $url; |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
3 |
|
public function getCompanyId() |
82
|
|
|
{ |
83
|
3 |
|
return $this->companyId; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
* @throws AuthenticationException |
89
|
|
|
* @throws CurlException |
90
|
|
|
*/ |
91
|
3 |
|
public function getAccessToken() |
92
|
|
|
{ |
93
|
3 |
|
$provider = $this->getProvider(); |
94
|
|
|
|
95
|
3 |
|
if (!is_array(self::$tokens) || |
|
|
|
|
96
|
|
|
!array_key_exists($provider->companyId, self::$tokens) || |
97
|
|
|
!self::$tokens[$provider->companyId] instanceof AccessToken || |
98
|
1 |
|
self::$tokens[$provider->companyId]->hasExpired() |
99
|
2 |
|
) { |
100
|
|
|
try { |
101
|
3 |
|
self::$tokens[$provider->companyId] = $provider->getAccessToken( |
102
|
3 |
|
'client_credentials', |
103
|
3 |
|
['scope' => $this->scopes] |
104
|
2 |
|
); |
105
|
3 |
|
} catch (IdentityProviderException $e) { |
106
|
3 |
|
throw new AuthenticationException($e->getMessage(), $e->getCode()); |
107
|
|
|
} catch (ConnectException $e) { |
108
|
|
|
throw new CurlException($e->getMessage(), $e->getCode()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return self::$tokens[$provider->companyId]->getToken(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return OAuthProvider |
117
|
|
|
*/ |
118
|
3 |
|
public function getProvider() |
119
|
2 |
|
{ |
120
|
3 |
|
$provider = new OAuthProvider([ |
121
|
3 |
|
'clientId' => $this->clientId, |
122
|
3 |
|
'clientSecret' => $this->clientSecret, |
123
|
3 |
|
'companyId' => $this->companyId, |
124
|
3 |
|
'url' => $this->url |
125
|
2 |
|
]); |
126
|
3 |
|
return $provider; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|