Completed
Pull Request — master (#1)
by
unknown
12:16
created

ORCID::getScopeSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the cilogon/oauth2-orcid library.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author    Terry Fleury <[email protected]>
10
 * @copyright 2016 University of Illinois
11
 * @license   https://opensource.org/licenses/NCSA NCSA
12
 * @link      https://github.com/cilogon/oauth2-orcid GitHub
13
 */
14
15
namespace CILogon\OAuth2\Client\Provider;
16
17
use League\OAuth2\Client\Provider\AbstractProvider;
18
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
19
use League\OAuth2\Client\Token\AccessToken;
20
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
21
use Psr\Http\Message\ResponseInterface;
22
23
class ORCID extends AbstractProvider
24
{
25
    use BearerAuthorizationTrait;
26
27
    /**
28
     * @var string Key used in the access token response to identify the resource owner.
29
     */
30
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'orcid';
31
32
    /**
33
     * @var string
34
     */
35
    public $sandbox = false;
36
37
    /**
38
     * @var string
39
     */
40
    public $member = false;
41
42
    /**
43
     * Returns the base URL for authorizing a client.
44
     *
45
     * @return string
46
     */
47
    public function getBaseAuthorizationUrl()
48
    {
49
        return 'https://' .
50
            (($this->sandbox) ? 'sandbox.' : '') .
51
            'orcid.org/oauth/authorize';
52
    }
53
54
    /**
55
     * Returns the base URL for requesting an access token.
56
     *
57
     * @param array $params
58
     * @return string
59
     */
60
    public function getBaseAccessTokenUrl(array $params)
61
    {
62
        return 'https://' .
63
            (($this->sandbox) ? 'sandbox.' : '') .
64
            'orcid.org/oauth/token';
65
    }
66
67
    /**
68
     * Returns the URL for requesting the resource owner's details.
69
     *
70
     * @param AccessToken $token
71
     *
72
     * @return string
73
     */
74
    public function getResourceOwnerDetailsUrl(AccessToken $token)
75
    {
76
        return 'https://' .
77
            (($this->member) ? 'api.' : 'pub.') .
78
            (($this->sandbox) ? 'sandbox.' : '') .
79
            'orcid.org/v2.0/' .
80
            $token->getResourceOwnerId() .
81
            '/record';
82
    }
83
84
    /**
85
     * Returns the default scopes used by this provider.
86
     *
87
     * This should only be the scopes that are required to request the details
88
     * of the resource owner, rather than all the available scopes.
89
     *
90
     * @return array
91
     */
92
    protected function getDefaultScopes()
93
    {
94
        return [ '/authenticate' ];
95
    }
96
97
    /**
98
     * Returns the string that should be used to separate scopes when building
99
     * the URL for requesting an access token.
100
     *
101
     * @return string Scope separator, defaults to ','
102
     */
103
    protected function getScopeSeparator()
104
    {
105
        return ' ';
106
    }
107
108
    /**
109
     * Returns the default headers used by this provider.
110
     *
111
     * Typically this is used to set 'Accept' or 'Content-Type' headers.
112
     *
113
     * @return array
114
     */
115
    protected function getDefaultHeaders()
116
    {
117
        return [ 'Accept' => 'application/json' ];
118
    }
119
120
121
    /**
122
     * Check a provider response for errors.
123
     *
124
     * @throws IdentityProviderException
125
     * @param  ResponseInterface $response
126
     * @param  string $data Parsed response data
127
     * @return void
128
     */
129
    protected function checkResponse(ResponseInterface $response, $data)
130
    {
131
        $error = false;
132
        $errcode = 0;
133
        $errmsg = '';
134
135
        if (!empty($data['error'])) {
136
            $error = true;
137
            $errmsg = $data['error'];
138
            if (!empty($data['error_description'])) {
139
                $errmsg .= ': ' . $data['error_description'];
140
            }
141
        } elseif (!empty($data['error-code'])) {
142
            $error = true;
143
            $errcode = (int)$data['error-code'];
144
            if (!empty($data['developer-message'])) {
145
                $errmsg = $data['developer-message'];
146
            }
147
        } elseif ($response->getStatusCode() >= 400) {
148
            $error = true;
149
            $errcode = $response->getStatusCode();
150
            $errmsg = $response->getReasonPhrase();
151
        }
152
153
        if ($error) {
154
            throw new IdentityProviderException($errmsg, $errcode, $data);
155
        }
156
    }
157
158
    /**
159
     * Generate a user object from a successful user details request.
160
     *
161
     * @param object $response
162
     * @param AccessToken $token
163
     * @return ORCIDResourceOwner
164
     */
165
    protected function createResourceOwner(array $response, AccessToken $token)
166
    {
167
        return new ORCIDResourceOwner($response);
168
    }
169
170
    /**
171
     * Returns a prepared request for requesting an access token.
172
     *
173
     * @param array $params Query string parameters
174
     * @return RequestInterface
175
     */
176
    protected function getAccessTokenRequest(array $params, $accesstoken = null)
177
    {
178
        $method  = $this->getAccessTokenMethod();
179
        $url     = $this->getAccessTokenUrl($params);
180
        $options = $this->optionProvider->getAccessTokenOptions($method, $params);
181
        if (is_null($accesstoken)) {
182
            return $this->getRequest($method, $url, $options);
183
        } else {
184
            return $this->getAuthenticatedRequest($method, $url, $accesstoken, $options);
185
        }
186
    }
187
    /**
188
     * Requests an access token using a specified grant and option set.
189
     *
190
     * @param  mixed $grant
191
     * @param  array $options
192
     * @return AccessToken
193
     */
194
    public function getAccessToken($grant, array $options = [], $accesstoken = null)
195
    {
196
        $grant = $this->verifyGrant($grant);
197
        $params = [
198
            'client_id'     => $this->clientId,
199
            'client_secret' => $this->clientSecret,
200
            'redirect_uri'  => $this->redirectUri,
201
        ];
202
        $params   = $grant->prepareRequestParameters($params, $options);
203
        $request  = $this->getAccessTokenRequest($params, $accesstoken);
204
        $response = $this->getParsedResponse($request);
205
        $prepared = $this->prepareAccessTokenResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getParsedResponse($request) on line 204 can also be of type null or string; however, League\OAuth2\Client\Pro...reAccessTokenResponse() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
206
        $token    = $this->createAccessToken($prepared, $grant);
207
        return $token;
208
    }
209
}
210