CILogon::createResourceOwner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the cilogon/oauth2-cilogon 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 2021 University of Illinois
11
 * @license   https://opensource.org/licenses/NCSA NCSA
12
 * @link      https://github.com/cilogon/oauth2-cilogon 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 Psr\Http\Message\ResponseInterface;
21
22
class CILogon extends AbstractProvider
23
{
24
    /**
25
     * @var string An alternate server to use, one of "test" or "dev".
26
     */
27
    public $server = '';
28
29
    /**
30
     * Returns the base URL for authorizing a client.
31
     *
32
     * @return string
33
     */
34
    public function getBaseAuthorizationUrl()
35
    {
36
        return 'https://' .
37
            ((strlen($this->server) > 0) ? $this->server . '.' : '') .
38
            'cilogon.org/authorize';
39
    }
40
41
    /**
42
     * Returns the base URL for requesting an access token.
43
     *
44
     * @param array $params
45
     * @return string
46
     */
47
    public function getBaseAccessTokenUrl(array $params)
48
    {
49
        return 'https://' .
50
            ((strlen($this->server) > 0) ? $this->server . '.' : '') .
51
            'cilogon.org/oauth2/token';
52
    }
53
54
    /**
55
     * Returns the URL for requesting the resource owner's details.
56
     *
57
     * @param AccessToken $token
58
     *
59
     * @return string
60
     */
61
    public function getResourceOwnerDetailsUrl(AccessToken $token)
62
    {
63
        return 'https://' .
64
            ((strlen($this->server) > 0) ? $this->server . '.' : '') .
65
            'cilogon.org/oauth2/userinfo?access_token=' . urlencode($token);
66
    }
67
68
    /**
69
     * Returns the default scopes used by this provider.
70
     *
71
     * This should only be the scopes that are required to request the details
72
     * of the resource owner, rather than all the available scopes.
73
     *
74
     * Other available scopes include: email, profile, org.cilogon.userinfo
75
     *
76
     * @return array
77
     */
78
    protected function getDefaultScopes()
79
    {
80
        return [
81
            'openid',
82
        ];
83
    }
84
85
     /**
86
     * Returns the string that should be used to separate scopes when building
87
     * the URL for requesting an access token.
88
     *
89
     * @return string Scope separator, defaults to space
90
     */
91
    protected function getScopeSeparator()
92
    {
93
        return ' ';
94
    }
95
96
    /**
97
     * Check a provider response for errors.
98
     *
99
     * @throws IdentityProviderException
100
     * @param  ResponseInterface $response
101
     * @param  string $data Parsed response data
102
     * @return void
103
     */
104
    protected function checkResponse(ResponseInterface $response, $data)
105
    {
106
        $error = false;
107
        $errcode = 0;
108
        $errmsg = '';
109
110
        if (!empty($data['error'])) {
111
            $error = true;
112
            $errmsg = $data['error'];
113
            if (!empty($data['error_description'])) {
114
                $errmsg .= ': ' . $data['error_description'];
115
            }
116
        } elseif ($response->getStatusCode() >= 400) {
117
            $error = true;
118
            $errcode = $response->getStatusCode();
119
            $errmsg = $response->getReasonPhrase();
120
        }
121
122
        if ($error) {
123
            throw new IdentityProviderException($errmsg, $errcode, $data);
124
        }
125
    }
126
127
    /**
128
     * Generate a user object from a successful user details request.
129
     *
130
     * @param object $response
131
     * @param AccessToken $token
132
     * @return CILogonResourceOwner
133
     */
134
    protected function createResourceOwner(array $response, AccessToken $token)
135
    {
136
        return new CILogonResourceOwner($response);
137
    }
138
}
139