1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CommerceGuys\Guzzle\Oauth2\GrantType; |
4
|
|
|
|
5
|
|
|
use CommerceGuys\Guzzle\Oauth2\AccessToken; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
abstract class GrantTypeBase implements GrantTypeInterface |
11
|
|
|
{ |
12
|
|
|
const CONFIG_TOKEN_URL = 'token_url'; |
13
|
|
|
const CONFIG_CLIENT_ID = 'client_id'; |
14
|
|
|
const CONFIG_CLIENT_SECRET = 'client_secret'; |
15
|
|
|
const CONFIG_AUTH_LOCATION = 'auth_location'; |
16
|
|
|
|
17
|
|
|
const GRANT_TYPE = 'grant_type'; |
18
|
|
|
|
19
|
|
|
const MISSING_ARGUMENT = 'The config is missing the following key: "%s"'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ClientInterface The token endpoint client |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Configuration settings |
28
|
|
|
*/ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $grantType = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClientInterface $client |
38
|
|
|
* @param array $config |
39
|
|
|
*/ |
40
|
18 |
|
public function __construct(ClientInterface $client, array $config = []) |
41
|
|
|
{ |
42
|
18 |
|
$this->client = $client; |
43
|
18 |
|
$this->config = array_merge($this->getDefaults(), $config); |
44
|
|
|
|
45
|
18 |
|
foreach ($this->getRequired() as $key => $requiredAttribute) { |
46
|
18 |
|
if (!isset($this->config[$key]) || empty($this->config[$key])) { |
47
|
7 |
|
throw new InvalidArgumentException(sprintf(self::MISSING_ARGUMENT, $key)); |
48
|
|
|
} |
49
|
15 |
|
} |
50
|
11 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get default configuration items. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
18 |
|
protected function getDefaults() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
18 |
|
'scope' => '', |
61
|
18 |
|
self::CONFIG_TOKEN_URL => '/oauth2/token', |
62
|
18 |
|
self::CONFIG_AUTH_LOCATION => 'headers', |
63
|
18 |
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get required configuration items. |
68
|
|
|
* |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
18 |
|
protected function getRequired() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
18 |
|
self::CONFIG_CLIENT_ID => '', |
75
|
18 |
|
self::CONFIG_CLIENT_SECRET => '', |
76
|
18 |
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get additional options, if any. |
81
|
|
|
* |
82
|
|
|
* @return array|null |
83
|
|
|
*/ |
84
|
5 |
|
protected function getAdditionalOptions() |
85
|
|
|
{ |
86
|
5 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getConfig() |
93
|
|
|
{ |
94
|
|
|
return $this->config; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
* |
100
|
|
|
* @return mixed|null |
101
|
|
|
*/ |
102
|
7 |
|
public function getConfigByName($name) |
103
|
|
|
{ |
104
|
7 |
|
if (array_key_exists($name, $this->config)) { |
105
|
7 |
|
return $this->config[$name]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
6 |
|
public function getToken() |
115
|
|
|
{ |
116
|
6 |
|
$body = $this->config; |
117
|
6 |
|
$body[self::GRANT_TYPE] = $this->grantType; |
118
|
6 |
|
unset($body[self::CONFIG_TOKEN_URL], $body[self::CONFIG_AUTH_LOCATION]); |
119
|
|
|
|
120
|
6 |
|
$requestOptions = []; |
121
|
|
|
|
122
|
6 |
|
if ($this->config[self::CONFIG_AUTH_LOCATION] !== RequestOptions::BODY) { |
123
|
6 |
|
$requestOptions[RequestOptions::AUTH] = [$this->config[self::CONFIG_CLIENT_ID], $this->config[self::CONFIG_CLIENT_SECRET]]; |
124
|
6 |
|
unset($body[self::CONFIG_CLIENT_ID], $body[self::CONFIG_CLIENT_SECRET]); |
125
|
6 |
|
} |
126
|
|
|
|
127
|
6 |
|
$requestOptions[RequestOptions::FORM_PARAMS] = $body; |
128
|
|
|
|
129
|
6 |
|
if ($additionalOptions = $this->getAdditionalOptions()) { |
130
|
1 |
|
$requestOptions = array_merge_recursive($requestOptions, $additionalOptions); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
6 |
|
$response = $this->client->post($this->config[self::CONFIG_TOKEN_URL], $requestOptions); |
134
|
6 |
|
$data = json_decode($response->getBody()->__toString(), true); |
135
|
|
|
|
136
|
6 |
|
return new AccessToken($data['access_token'], $data['token_type'], $data); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|