Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EOpenIDService.php
1 <?php
2 /**
3  * EOpenIDService class file.
4  *
5  * @author Maxim Zemskov <nodge@yandex.ru>
6  * @link http://code.google.com/p/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  * @package application.extensions.eauth
15  */
16 abstract class EOpenIDService extends EAuthServiceBase implements IAuthService {
17 
18  /**
19  * @var EOpenID the openid library instance.
20  */
21  private $auth;
22 
23  /**
24  * @var string the OpenID authorization url.
25  */
26  protected $url;
27 
28  /**
29  * @var array the OpenID required attributes.
30  */
31  protected $requiredAttributes = array();
32 
33 
34  /**
35  * Initialize the component.
36  * @param EAuth $component the component instance.
37  * @param array $options properties initialization.
38  */
39  public function init($component, $options = array()) {
40  parent::init($component, $options);
41  $this->auth = Yii::app()->loid->load();
42  //$this->auth = new EOpenID();
43  }
44 
45  /**
46  * Authenticate the user.
47  * @return boolean whether user was successfuly authenticated.
48  */
49  public function authenticate() {
50  if (!empty($_REQUEST['openid_mode'])) {
51  switch ($_REQUEST['openid_mode']) {
52  case 'id_res':
53  try {
54  if ($this->auth->validate()) {
55  $this->attributes['id'] = $this->auth->identity;
56 
57  $attributes = $this->auth->getAttributes();
58  foreach ($this->requiredAttributes as $key => $attr) {
59  if (isset($attributes[$attr[1]])) {
60  $this->attributes[$key] = $attributes[$attr[1]];
61  }
62  else {
63  throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => ucfirst($this->getServiceName()))));
64  return false;
65  }
66  }
67 
68  $this->authenticated = true;
69  return true;
70  }
71  else {
72  throw new EAuthException(Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => ucfirst($this->getServiceName()))));
73  return false;
74  }
75  }
76  catch (Exception $e) {
77  throw new EAuthException($e->getMessage(), $e->getCode());
78  }
79  break;
80 
81  case 'cancel':
82  $this->cancel();
83  break;
84 
85  default:
86  throw new CHttpException(400, Yii::t('yii', 'Your request is invalid.'));
87  break;
88  }
89  }
90  else {
91  $this->auth->identity = $this->url; //Setting identifier
92  $this->auth->required = array(); //Try to get info from openid provider
93  foreach ($this->requiredAttributes as $attribute)
94  $this->auth->required[$attribute[0]] = $attribute[1];
95  $this->auth->realm = Yii::app()->request->hostInfo;
96  $this->auth->returnUrl = $this->auth->realm.Yii::app()->request->url; //getting return URL
97 
98  try {
99  $url = $this->auth->authUrl();
100  Yii::app()->request->redirect($url);
101  }
102  catch (Exception $e) {
103  throw new EAuthException($e->getMessage(), $e->getCode());
104  }
105  }
106 
107  return false;
108  }
109 }