forked from Nodge/yii-eauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EOpenIDService.php
109 lines (95 loc) · 2.95 KB
/
EOpenIDService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* EOpenIDService class file.
*
* @author Maxim Zemskov <[email protected]>
* @link http://github.com/Nodge/yii-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
require_once 'EAuthServiceBase.php';
/**
* EOpenIDService is a base class for all OpenID providers.
* @package application.extensions.eauth
*/
abstract class EOpenIDService extends EAuthServiceBase implements IAuthService {
/**
* @var EOpenID the openid library instance.
*/
private $auth;
/**
* @var string the OpenID authorization url.
*/
protected $url;
/**
* @var array the OpenID required attributes.
*/
protected $requiredAttributes = array();
/**
* Initialize the component.
* @param EAuth $component the component instance.
* @param array $options properties initialization.
*/
public function init($component, $options = array()) {
parent::init($component, $options);
$this->auth = Yii::app()->loid->load();
//$this->auth = new EOpenID();
}
/**
* Authenticate the user.
* @return boolean whether user was successfuly authenticated.
*/
public function authenticate() {
if (!empty($_REQUEST['openid_mode'])) {
switch ($_REQUEST['openid_mode']) {
case 'id_res':
try {
if ($this->auth->validate()) {
$this->attributes['id'] = $this->auth->identity;
$attributes = $this->auth->getAttributes();
foreach ($this->requiredAttributes as $key => $attr) {
if (isset($attributes[$attr[1]])) {
$this->attributes[$key] = $attributes[$attr[1]];
}
else {
throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => $this->getServiceTitle())));
return false;
}
}
$this->authenticated = true;
return true;
}
else {
throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => $this->getServiceTitle())));
return false;
}
}
catch (Exception $e) {
throw new EAuthException($e->getMessage(), $e->getCode());
}
break;
case 'cancel':
$this->cancel();
break;
default:
throw new CHttpException(400, Yii::t('yii', 'Your request is invalid.'));
break;
}
}
else {
$this->auth->identity = $this->url; //Setting identifier
$this->auth->required = array(); //Try to get info from openid provider
foreach ($this->requiredAttributes as $attribute)
$this->auth->required[$attribute[0]] = $attribute[1];
$this->auth->realm = Yii::app()->request->hostInfo;
$this->auth->returnUrl = $this->auth->realm.Yii::app()->request->url; //getting return URL
try {
$url = $this->auth->authUrl();
Yii::app()->request->redirect($url);
}
catch (Exception $e) {
throw new EAuthException($e->getMessage(), $e->getCode());
}
}
return false;
}
}