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 NopeClient. |
16
|
|
|
* Allows OAuth2 authentication through Nope server. |
17
|
|
|
* |
18
|
|
|
* In order to use NOPE client you must register your application at NOPE 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
|
|
|
* 'nope' => [ |
28
|
|
|
* 'class' => \hiam\authclient\NopeClient::class, |
29
|
|
|
* 'site' => 'nope.somewhere.com', |
30
|
|
|
* 'clientId' => 'client_id', |
31
|
|
|
* 'clientSecret' => 'client_secret', |
32
|
|
|
* ], |
33
|
|
|
* ], |
34
|
|
|
* ] |
35
|
|
|
* ... |
36
|
|
|
* ] |
37
|
|
|
* ``` |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
class NopeClient 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
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function buildAuthUrl(array $params = []) |
57
|
|
|
{ |
58
|
|
|
$params['language'] = \Yii::$app->language; |
59
|
|
|
|
60
|
|
|
return parent::buildAuthUrl($params); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Inits Urls based on $site. |
65
|
|
|
*/ |
66
|
|
|
public function init() |
67
|
|
|
{ |
68
|
|
|
parent::init(); |
69
|
|
|
if (!$this->site) { |
70
|
|
|
throw new \Exception('Nope site must not be empty!'); |
71
|
|
|
} |
72
|
|
|
if (strpos($this->site, '://') === false) { |
73
|
|
|
$this->site = 'https://' . $this->site; |
74
|
|
|
} |
75
|
|
|
$defaults = [ |
76
|
|
|
'authUrl' => 'oauth/authorize', |
77
|
|
|
'tokenUrl' => 'oauth/token', |
78
|
|
|
'apiBaseUrl' => 'api', |
79
|
|
|
]; |
80
|
|
|
foreach ($defaults as $k => $v) { |
81
|
|
|
if (!$this->{$k}) { |
82
|
|
|
$this->{$k} = $this->buildUrl($v); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** {@inheritdoc} */ |
88
|
|
|
protected function initUserAttributes() |
89
|
|
|
{ |
90
|
|
|
return $this->getAccessToken()->getParam('user_attributes'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** {@inheritdoc} */ |
94
|
|
|
protected function apiInternal($accessToken, $url, $method, array $params, array $headers) |
95
|
|
|
{ |
96
|
|
|
if (!isset($params['format'])) { |
97
|
|
|
$params['format'] = 'json'; |
98
|
|
|
} |
99
|
|
|
$params['oauth_token'] = $accessToken->getToken(); |
100
|
|
|
|
101
|
|
|
return $this->sendRequest($method, $url, $params, $headers); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** {@inheritdoc} */ |
105
|
|
|
protected function defaultName() |
106
|
|
|
{ |
107
|
|
|
return 'nope'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** {@inheritdoc} */ |
111
|
|
|
protected function defaultTitle() |
112
|
|
|
{ |
113
|
|
|
return 'nope'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function setState($key, $value) |
120
|
|
|
{ |
121
|
|
|
return parent::setState($key, $value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function getState($key) |
128
|
|
|
{ |
129
|
|
|
return parent::getState($key); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function removeState($key) |
136
|
|
|
{ |
137
|
|
|
return parent::removeState($key); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.