|
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 $token; |
|
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
|
|
|
public function __construct(int $companyId, string $clientId, string $clientSecret, string $scopes) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->companyId = $companyId; |
|
54
|
|
|
$this->clientId = $clientId; |
|
55
|
|
|
$this->clientSecret = $clientSecret; |
|
56
|
|
|
$this->scopes = $scopes; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $url |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setUrl($url) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->url = $url; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getUrl(): string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->url; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCompanyId(): int |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->companyId; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws AuthenticationException |
|
86
|
|
|
* @throws CurlException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAccessToken() |
|
89
|
|
|
{ |
|
90
|
|
|
$provider = $this->getProvider(); |
|
91
|
|
|
|
|
92
|
|
|
if (!self::$token instanceof AccessToken || self::$token->hasExpired()) { |
|
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
self::$token = $provider->getAccessToken('client_credentials', ['scope' => $this->scopes]); |
|
95
|
|
|
} catch(IdentityProviderException $e) { |
|
96
|
|
|
throw new AuthenticationException($e->getMessage(), $e->getCode()); |
|
97
|
|
|
} catch(ConnectException $e) { |
|
98
|
|
|
throw new CurlException($e->getMessage(), $e->getCode()); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return self::$token->getToken(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return \Kobas\OAuth2\Client\Provider\Kobas |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getProvider() |
|
109
|
|
|
{ |
|
110
|
|
|
$provider = new \Kobas\OAuth2\Client\Provider\Kobas([ |
|
111
|
|
|
'clientId' => $this->clientId, |
|
112
|
|
|
'clientSecret' => $this->clientSecret, |
|
113
|
|
|
'companyId' => $this->companyId, |
|
114
|
|
|
'url' => $this->url |
|
115
|
|
|
]); |
|
116
|
|
|
return $provider; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|