Completed
Push — master ( 1f575e...6f6498 )
by Jan Philip
12:45 queued 11:26
created

DooProvider::getConfigurableOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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