Completed
Push — master ( c49dc0...0bed7f )
by Terrence
13:47
created

OAuth2Provider::__construct()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 30
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 16
nop 1
crap 56
1
<?php
2
3
namespace CILogon\Service;
4
5
use CILogon\Service\Util;
6
use League\OAuth2\Client\Provider\Github;
7
use League\OAuth2\Client\Provider\Google;
8
use CILogon\OAuth2\Client\Provider\ORCID;
9
10
/**
11
 * OAuth2Provider
12
 */
13
class OAuth2Provider
14
{
15
    /**
16
     * @var League\OAuth2\Client\Provider $provider Member variable for
17
     *      OAuth2 PHP provider object
18
     */
19
    public $provider = null;
20
21
    /**
22
     * @var array $authzUrlOpts An array of parameters to be passed to
23
     *      getAuthorizationUrl().
24
     */
25
    public $authzUrlOpts = array();
26
27
    /**
28
     * __construct
29
     *
30
     * Class constructor. Initializes the class variables using the passed-in
31
     * Identity Provider ($idp). Sets the class variables 'provider' (the
32
     * OAuth2 Client library provider object) and 'authzUrlOpts' (for use
33
     * with getAuthorizationUrl()).
34
     *
35
     * @param string $idp The Identity Provider to use for OAuth2 connection.
36
     */
37
    public function __construct($idp)
38
    {
39
        if (is_null($idp)) {
40
            $idp = Util::getSessionVar('idpname');
41
        }
42
        $idp = strtolower($idp);
43
44
        $client_id = '';
45
        $client_secret = '';
46
        $classname = '';
47
        $extraparams = array();
48
49
        // Set the client id and secret for the $idp
50
        if ($idp == 'google') {
51
            $client_id     = Util::getConfigVar('googleoauth2.clientid');
52
            $client_secret = Util::getConfigVar('googleoauth2.clientsecret');
53
            $classname     = 'League\OAuth2\Client\Provider\Google';
54
            $this->authzUrlOpts = [ 'scope' => ['openid','email','profile'] ];
55
            $extraparams = array('accessType' => 'offline');
56
        } elseif ($idp == 'github') {
57
            $client_id     = Util::getConfigVar('githuboauth2.clientid');
58
            $client_secret = Util::getConfigVar('githuboauth2.clientsecret');
59
            $classname     = 'League\OAuth2\Client\Provider\Github';
60
            $this->authzUrlOpts = [ 'scope' => ['user:email'] ];
61
        } elseif ($idp == 'orcid') {
62
            $client_id     = Util::getConfigVar('orcidoauth2.clientid');
63
            $client_secret = Util::getConfigVar('orcidoauth2.clientsecret');
64
            $classname     = 'League\OAuth2\Client\Provider\ORCID';
65
        }
66
67
        if ((strlen($client_id) > 0) && (strlen($client_secret) > 0)) {
68
            $this->provider = new $classname(array_merge(array(
69
                'clientId'     => $client_id,
70
                'clientSecret' => $client_secret,
71
                'redirectUri'  => 'https://' . Util::getHN() . '/getuser/'
72
            ), $extraparams));
73
        }
74
    }
75
}
76