1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kobas\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class Kobas extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
use BearerAuthorizationTrait; |
14
|
|
|
/** |
15
|
|
|
* Default scopes |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $defaultScopes = ['integration']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Kobas OAuth URL |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $url = 'https://oauth.kobas.co.uk'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Kobas Company ID |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public $companyId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get authorization url to begin OAuth flow |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
9 |
|
public function getBaseAuthorizationUrl() |
41
|
|
|
{ |
42
|
9 |
|
return $this->url . '/authorize'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get access token url to retrieve token |
48
|
|
|
* |
49
|
|
|
* @array $params |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
15 |
|
public function getBaseAccessTokenUrl(array $params) |
53
|
|
|
{ |
54
|
15 |
|
return $this->url . '/access_token?x-kobas-company-id=' . $this->companyId; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get provider url to fetch user details |
59
|
|
|
* |
60
|
|
|
* @param AccessToken $token |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
65
|
|
|
{ |
66
|
6 |
|
return $this->url . '/me'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the default scopes used by this provider. |
71
|
|
|
* |
72
|
|
|
* This should not be a complete list of all scopes, but the minimum |
73
|
|
|
* required for the provider user interface! |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
6 |
|
protected function getDefaultScopes() |
78
|
|
|
{ |
79
|
6 |
|
return $this->defaultScopes; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the string that should be used to separate scopes when building |
84
|
|
|
* the URL for requesting an access token. |
85
|
|
|
* |
86
|
|
|
* @return string Scope separator, defaults to ' ' |
87
|
|
|
*/ |
88
|
9 |
|
protected function getScopeSeparator() |
89
|
|
|
{ |
90
|
9 |
|
return ' '; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check a provider response for errors. |
95
|
|
|
* |
96
|
|
|
* @throws IdentityProviderException |
97
|
|
|
* @param ResponseInterface $response |
98
|
|
|
* @param string $data Parsed response data |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
12 |
|
protected function checkResponse(ResponseInterface $response, $data) |
102
|
|
|
{ |
103
|
12 |
|
$acceptableStatuses = [200, 201]; |
104
|
12 |
|
if (!in_array($response->getStatusCode(), $acceptableStatuses)) { |
105
|
3 |
|
throw new IdentityProviderException( |
106
|
3 |
|
$data['message'] ?: $response->getReasonPhrase(), |
107
|
3 |
|
$response->getStatusCode(), |
108
|
2 |
|
$response |
|
|
|
|
109
|
1 |
|
); |
110
|
|
|
} |
111
|
9 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generate a user object from a successful user details request. |
115
|
|
|
* |
116
|
|
|
* @param object $response |
117
|
|
|
* @param AccessToken $token |
118
|
|
|
* @return KobasResourceOwner |
119
|
|
|
*/ |
120
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
121
|
|
|
{ |
122
|
3 |
|
return new KobasResourceOwner($response); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|