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
|
|
|
|