1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* OAuth2 client for yii2 to login with HIAM server |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/yii2-hiam-authclient |
7
|
|
|
* @package yii2-hiam-authclient |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiam\authclient; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HiamClient. |
16
|
|
|
* Allows OAuth2 authentication through HIAM server. |
17
|
|
|
* |
18
|
|
|
* In order to use HIAM client you must register your application at HIAM server. |
19
|
|
|
* Used identical to yii2-authclient Oauth2Client. |
20
|
|
|
* |
21
|
|
|
* Example application configuration: |
22
|
|
|
* ``` |
23
|
|
|
* 'components' => [ |
24
|
|
|
* 'authClientCollection' => [ |
25
|
|
|
* 'class' => \hiam\authclient\Collection::class, |
26
|
|
|
* 'clients' => [ |
27
|
|
|
* 'hiam' => [ |
28
|
|
|
* 'class' => \hiam\authclient\HiamClient::class, |
29
|
|
|
* 'site' => 'hiam.hipanel.com', |
30
|
|
|
* 'clientId' => 'client_id', |
31
|
|
|
* 'clientSecret' => 'client_secret', |
32
|
|
|
* ], |
33
|
|
|
* ], |
34
|
|
|
* ] |
35
|
|
|
* ... |
36
|
|
|
* ] |
37
|
|
|
* ``` |
38
|
|
|
*/ |
39
|
|
|
class HiamClient extends \yii\authclient\OAuth2 |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Site for urls generation. |
43
|
|
|
*/ |
44
|
|
|
public $site; |
45
|
|
|
|
46
|
|
|
public function buildUrl($path, array $params = []) |
47
|
|
|
{ |
48
|
|
|
$url = $this->site . '/' . $path; |
49
|
|
|
|
50
|
|
|
return $params ? $this->composeUrl($url, $params) : $url; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Inits Urls based on $site. |
55
|
|
|
*/ |
56
|
|
|
public function init() |
57
|
|
|
{ |
58
|
|
|
parent::init(); |
59
|
|
|
if (!$this->site) { |
60
|
|
|
$this->site = 'hiam.hipanel.com'; |
61
|
|
|
} |
62
|
|
|
if (strpos($this->site, '://') === false) { |
63
|
|
|
$this->site = 'https://' . $this->site; |
64
|
|
|
} |
65
|
|
|
$defaults = [ |
66
|
|
|
'authUrl' => 'oauth/authorize', |
67
|
|
|
'tokenUrl' => 'oauth/token', |
68
|
|
|
'apiBaseUrl' => 'api', |
69
|
|
|
]; |
70
|
|
|
foreach ($defaults as $k => $v) { |
71
|
|
|
if (!$this->{$k}) { |
72
|
|
|
$this->{$k} = $this->buildUrl($v); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** {@inheritdoc} */ |
78
|
|
|
protected function initUserAttributes() |
79
|
|
|
{ |
80
|
|
|
return $this->getAccessToken()->getParam('user_attributes'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** {@inheritdoc} */ |
84
|
|
|
protected function apiInternal($accessToken, $url, $method, array $params, array $headers) |
85
|
|
|
{ |
86
|
|
|
if (!isset($params['format'])) { |
87
|
|
|
$params['format'] = 'json'; |
88
|
|
|
} |
89
|
|
|
$params['oauth_token'] = $accessToken->getToken(); |
90
|
|
|
|
91
|
|
|
return $this->sendRequest($method, $url, $params, $headers); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** {@inheritdoc} */ |
95
|
|
|
protected function defaultName() |
96
|
|
|
{ |
97
|
|
|
return 'hiam'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** {@inheritdoc} */ |
101
|
|
|
protected function defaultTitle() |
102
|
|
|
{ |
103
|
|
|
return 'hiam'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function setState($key, $value) |
110
|
|
|
{ |
111
|
|
|
return parent::setState($key, $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function getState($key) |
118
|
|
|
{ |
119
|
|
|
return parent::getState($key); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function removeState($key) |
126
|
|
|
{ |
127
|
|
|
return parent::removeState($key); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.