|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CommerceGuys\Guzzle\Oauth2\GrantType; |
|
4
|
|
|
|
|
5
|
|
|
use CommerceGuys\Guzzle\Oauth2\AccessToken; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use GuzzleHttp\Collection; |
|
8
|
|
|
|
|
9
|
|
|
abstract class GrantTypeBase implements GrantTypeInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ClientInterface The token endpoint client */ |
|
12
|
|
|
protected $client; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Collection Configuration settings */ |
|
15
|
|
|
protected $config; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $grantType = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ClientInterface $client |
|
22
|
|
|
* @param array $config |
|
23
|
|
|
*/ |
|
24
|
10 |
|
public function __construct(ClientInterface $client, array $config = []) |
|
25
|
|
|
{ |
|
26
|
10 |
|
$this->client = $client; |
|
27
|
10 |
|
$this->config = Collection::fromConfig($config, $this->getDefaults(), $this->getRequired()); |
|
28
|
7 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get default configuration items. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
10 |
|
protected function getDefaults() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
10 |
|
'client_secret' => '', |
|
39
|
10 |
|
'scope' => '', |
|
40
|
10 |
|
'token_url' => 'oauth2/token', |
|
41
|
10 |
|
'auth_location' => 'headers', |
|
42
|
10 |
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get required configuration items. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string[] |
|
49
|
|
|
*/ |
|
50
|
10 |
|
protected function getRequired() |
|
51
|
|
|
{ |
|
52
|
10 |
|
return ['client_id']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get additional options, if any. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array|null |
|
59
|
|
|
*/ |
|
60
|
4 |
|
protected function getAdditionalOptions() |
|
61
|
|
|
{ |
|
62
|
4 |
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
5 |
|
public function getToken() |
|
69
|
|
|
{ |
|
70
|
5 |
|
$config = $this->config->toArray(); |
|
71
|
|
|
|
|
72
|
5 |
|
$body = $config; |
|
73
|
5 |
|
$body['grant_type'] = $this->grantType; |
|
74
|
5 |
|
unset($body['token_url'], $body['auth_location']); |
|
75
|
|
|
|
|
76
|
5 |
|
$requestOptions = []; |
|
77
|
|
|
|
|
78
|
5 |
|
if ($config['auth_location'] !== 'body') { |
|
79
|
5 |
|
$requestOptions['auth'] = [$config['client_id'], $config['client_secret']]; |
|
80
|
5 |
|
unset($body['client_id'], $body['client_secret']); |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
$requestOptions['body'] = $body; |
|
84
|
|
|
|
|
85
|
5 |
|
if ($additionalOptions = $this->getAdditionalOptions()) { |
|
86
|
1 |
|
$requestOptions = array_merge_recursive($requestOptions, $additionalOptions); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
$response = $this->client->post($config['token_url'], $requestOptions); |
|
90
|
5 |
|
$data = $response->json(); |
|
91
|
|
|
|
|
92
|
5 |
|
return new AccessToken($data['access_token'], $data['token_type'], $data); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|