Completed
Push — master ( 2693a9...e51dba )
by Jan Philip
03:07
created

DooProvider::checkResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 6
cp 0.5
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 4.125
1
<?php
2
/**
3
 * This file is based on the Generic Provider from the league/oauth2-client library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that is distributed with the league/oauth2-client library.
7
 */
8
9
namespace JPBernius\OAuth2\Client\Provider;
10
11
use GuzzleHttp\Client;
12
use League\OAuth2\Client\Grant\AbstractGrant;
13
use League\OAuth2\Client\Provider\AbstractProvider;
14
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
15
use League\OAuth2\Client\Provider\GenericResourceOwner;
16
use League\OAuth2\Client\Token\AccessToken;
17
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
 * OAuth 2.0 service provider for interaction with doo.net API using Bearer token authentication.
22
 */
23
class DooProvider extends AbstractProvider
24
{
25
    use BearerAuthorizationTrait;
26
27
    /**
28
     * @var string
29
     */
30
    private $apiUrl = 'https://api.doo.net/v1/';
31
32
    /**
33
     * @var string
34
     */
35
    private $uriAuthorize = 'oauth';
36
37
    /**
38
     * @var string
39
     */
40
    private $uriAccessToken = 'oauth';
41
42
    /**
43
     * @var string
44
     */
45
    private $uriResourceOwnerDetails = 'organizers/current';
46
47
    /**
48
     * @var array|null
49
     */
50
    private $scopes = null;
51
52
    /**
53
     * @var string
54
     */
55
    private $scopeSeparator;
56
57
    /**
58
     * @var string
59
     */
60
    private $responseError = 'error';
61
62
    /**
63
     * @var string
64
     */
65
    private $responseCode;
66
67
    /**
68
     * @var string
69
     */
70
    private $responseResourceOwnerId = 'id';
71
72
    /**
73
     * @param array $options
74
     * @param array $collaborators
75
     */
76 18
    public function __construct(array $options = [], array $collaborators = [])
77
    {
78 18
        $possible   = $this->getConfigurableOptions();
79 18
        $configured = array_intersect_key($options, array_flip($possible));
80
81 18
        foreach ($configured as $key => $value) {
82
            $this->$key = $value;
83 12
        }
84
85
        // Remove all options that are only used locally
86 18
        $options = array_diff_key($options, $configured);
87
88 18
        parent::__construct($options, $collaborators);
89
90 18
        $this->setHttpClient(new Client(array_intersect_key(
91 12
            $options,
92 18
            array_flip(['verify', 'timeout'])
93 12
        )));
94 18
    }
95
96
    /**
97
     * Returns all options that can be configured.
98
     *
99
     * @return array
100
     */
101 18
    protected function getConfigurableOptions()
102
    {
103
        return [
104 18
            'urlAuthorize',
105 12
            'urlAccessToken',
106 12
            'uriResourceOwnerDetails',
107 12
            'scopeSeparator',
108 12
            'responseError',
109 12
            'responseCode',
110 12
            'scopes',
111
            'apiUrl'
112 12
        ];
113
    }
114
115 18
    public function getBaseUri()
116
    {
117 18
        return $this->apiUrl;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 6
    public function getBaseAuthorizationUrl()
124
    {
125 6
        return $this->getBaseUri() . $this->uriAuthorize;
126 2
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 14
    public function getBaseAccessTokenUrl(array $params)
132
    {
133 14
        return $this->getBaseUri() . $this->uriAccessToken;
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function getResourceOwnerDetailsUrl(AccessToken $token)
140
    {
141
        return $this->getBaseUri() . $this->uriResourceOwnerDetails;
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147 3
    public function getDefaultScopes()
148
    {
149 3
        return $this->scopes;
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155 3
    protected function getScopeSeparator()
156
    {
157 3
        return $this->scopeSeparator ?: parent::getScopeSeparator();
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163 9
    protected function checkResponse(ResponseInterface $response, $data)
164
    {
165 9
        if (!empty($data[$this->responseError])) {
166
            $error = $data[$this->responseError];
167
            $code  = $this->responseCode ? $data[$this->responseCode] : 0;
168
            throw new IdentityProviderException($error, $code, $data);
169
        }
170 9
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    protected function createResourceOwner(array $response, AccessToken $token)
176
    {
177
        return new GenericResourceOwner($response, $this->responseResourceOwnerId);
178
    }
179
180
    /**
181
     * @param array $response
182
     * @param AbstractGrant $grant
183
     * @return AccessToken
184
     */
185 15
    protected function createAccessToken(array $response, AbstractGrant $grant)
186
    {
187 9
        if (isset($response['data'])) {
188 6
            $data = $response['data'];
189 6
            return new AccessToken($data);
190
        }
191
192 13
        throw new IdentityProviderException(
193 3
            'Bad Request',
194 3
            400,
195 1
            'Data provided for issuing access_token is not correct'
196 2
        );
197
        
198
    }
199
200
    /**
201
     * Returns the default headers used by this provider.
202
     *
203
     * Typically this is used to set 'Accept' or 'Content-Type' headers.
204
     *
205
     * @return array
206
     */
207 9
    protected function getDefaultHeaders()
208
    {
209
        return [
210
            'Accept' => 'application/hal+json'
211 9
        ];
212
    }
213
214
    /**
215
     * Builds request options used for requesting an access token.
216
     *
217
     * @param  array $params
218
     * @return array
219
     */
220 9
    protected function getAccessTokenOptions(array $params)
221
    {
222 9
        $options = ['headers' => ['content-type' => 'application/json']];
223
224 9
        if ($this->getAccessTokenMethod() === self::METHOD_POST) {
225 9
            $options['body'] = $this->getAccessTokenBody($params);
226 6
        }
227
228 9
        return $options;
229
    }
230
231
    /**
232
     * Returns the request body for requesting an access token.
233
     *
234
     * @param  array $params
235
     * @return string
236
     */
237 9
    protected function getAccessTokenBody(array $params)
238
    {
239 9
        return json_encode($params);
240
    }
241
242 3
    public function getBearerAuthorizationHeader()
243
    {
244 3
        $token = $this->getAccessToken('client_credentials')->getToken();
245 3
        return $this->getAuthorizationHeaders($token);
246
    }
247
}
248