|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EOpenIDService class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Maxim Zemskov <[email protected]> |
|
6
|
|
|
* @link http://github.com/Nodge/yii-eauth/ |
|
7
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
require_once 'EAuthServiceBase.php'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* EOpenIDService is a base class for all OpenID providers. |
|
14
|
|
|
* |
|
15
|
|
|
* @package application.extensions.eauth |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class EOpenIDService extends EAuthServiceBase implements IAuthService { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string a pattern that represents the part of URL-space for which an OpenID Authentication request is valid. |
|
21
|
|
|
* See the spec for more info: http://openid.net/specs/openid-authentication-2_0.html#realms |
|
22
|
|
|
* Note: a pattern can be without http(s):// part |
|
23
|
|
|
*/ |
|
24
|
|
|
public $realm; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var LightOpenID the openid library instance. |
|
28
|
|
|
*/ |
|
29
|
|
|
private $auth; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string the OpenID authorization url. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $url; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array the OpenID required attributes. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $requiredAttributes = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array the OpenID optional attributes. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $optionalAttributes = array(); |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initialize the component. |
|
49
|
|
|
* |
|
50
|
|
|
* @param EAuth $component the component instance. |
|
51
|
|
|
* @param array $options properties initialization. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function init($component, $options = array()) { |
|
54
|
|
|
parent::init($component, $options); |
|
55
|
|
|
$this->auth = Yii::app()->loid->load(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Authenticate the user. |
|
60
|
|
|
* |
|
61
|
|
|
* @return boolean whether user was successfuly authenticated. |
|
62
|
|
|
* @throws EAuthException |
|
63
|
|
|
* @throws CHttpException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function authenticate() { |
|
66
|
|
|
|
|
67
|
|
|
if (!empty($_REQUEST['openid_mode'])) { |
|
68
|
|
|
switch ($_REQUEST['openid_mode']) { |
|
69
|
|
|
case 'id_res': |
|
70
|
|
|
try { |
|
71
|
|
|
$this->auth->returnUrl = $this->getState('returnUrl'); |
|
72
|
|
|
if ($this->auth->validate()) { |
|
73
|
|
|
$this->attributes['id'] = $this->auth->identity; |
|
74
|
|
|
|
|
75
|
|
|
$attributes = $this->auth->getAttributes(); |
|
76
|
|
|
foreach ($this->requiredAttributes as $key => $attr) { |
|
77
|
|
|
if (isset($attributes[$attr[1]])) { |
|
78
|
|
|
$this->attributes[$key] = $attributes[$attr[1]]; |
|
79
|
|
|
} |
|
80
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
81
|
|
|
throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => $this->getServiceTitle()))); |
|
82
|
|
|
return false; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->optionalAttributes as $key => $attr) { |
|
87
|
|
|
if (isset($attributes[$attr[1]])) { |
|
88
|
|
|
$this->attributes[$key] = $attributes[$attr[1]]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->authenticated = true; |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
96
|
|
|
throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => $this->getServiceTitle()))); |
|
97
|
|
|
return false; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} catch (Exception $e) { |
|
100
|
|
|
throw new EAuthException($e->getMessage(), $e->getCode()); |
|
101
|
|
|
} |
|
102
|
|
|
break; |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
case 'cancel': |
|
105
|
|
|
$this->cancel(); |
|
106
|
|
|
break; |
|
107
|
|
|
|
|
108
|
|
|
default: |
|
109
|
|
|
throw new CHttpException(400, Yii::t('yii', 'Your request is invalid.')); |
|
110
|
|
|
break; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
else { |
|
114
|
|
|
$this->auth->identity = $this->url; //Setting identifier |
|
115
|
|
|
$this->auth->required = array(); //Try to get info from openid provider |
|
116
|
|
|
foreach ($this->requiredAttributes as $attribute) { |
|
117
|
|
|
$this->auth->required[$attribute[0]] = $attribute[1]; |
|
118
|
|
|
} |
|
119
|
|
|
foreach ($this->optionalAttributes as $attribute) { |
|
120
|
|
|
$this->auth->required[$attribute[0]] = $attribute[1]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (isset($this->realm)) { |
|
124
|
|
|
if (!preg_match('#^[a-z]+\://#', $this->realm)) { |
|
125
|
|
|
$this->auth->realm = 'http' . (Yii::app()->request->getIsSecureConnection() ? 's' : '') . '://' . $this->realm; |
|
126
|
|
|
} |
|
127
|
|
|
else { |
|
128
|
|
|
$this->auth->realm = $this->realm; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
else { |
|
132
|
|
|
$this->auth->realm = Yii::app()->request->hostInfo; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->auth->returnUrl = Yii::app()->request->hostInfo . Yii::app()->request->url; //getting return URL |
|
136
|
|
|
$this->setState('returnUrl', $this->auth->returnUrl); |
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
$url = $this->auth->authUrl(); |
|
140
|
|
|
Yii::app()->request->redirect($url); |
|
141
|
|
|
} catch (Exception $e) { |
|
142
|
|
|
throw new EAuthException($e->getMessage(), $e->getCode()); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
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.