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