ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAuthContainerOpenId.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Auth/Container.php';
5 
14 {
15  private $settings = null;
16 
17  private $response_data = array();
18  private $force_creation = false;
19 
24  public function __construct()
25  {
27 
28  $this->initSettings();
29  }
30 
38  public function fetchData($a_username,$a_password,$isChallengeResponse = false)
39  {
40  global $ilLog;
41 
42  $ilLog->write(__METHOD__.': Fetch Data called');
43 
44  $response = $this->settings->getConsumer()->complete($this->settings->getReturnLocation());
45 
46  switch($response->status)
47  {
48  case Auth_OpenID_CANCEL:
49  die("Auth cancelled");
50 
52  die("Auth failed with message: ".$response->message);
53 
55  $openid = $response->getDisplayIdentifier();
56  $esc_identity = htmlentities($openid);
57  $ilLog->write(__METHOD__.': Auth success with identity '.$esc_identity);
58 
59  if($response->endpoint->canonicalID)
60  {
61  $escaped_canonicalID = htmlentities($response->endpoint->canonicalID);
62  $ilLog->write(__METHOD__.': Auth success with canonical id: '.$esc_identity);
63 
64  }
65  include_once 'Auth/OpenID/SReg.php';
66 
67  // Check if simple registration is supported
68  if(Auth_OpenID_supportsSReg($response->endpoint))
69  {
70  $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response,true);
71  $this->response_data = $sreg_resp->contents();
72 
73 
74  $ilLog->write(__METHOD__.' auth data: '.print_r($this->response_data,true));
75  return true;
76  }
77  else
78  {
79  // Try to fetch response values
80  foreach($response->message->args->keys as $key => $mapping)
81  {
82  if($mapping[1] == 'sreg.nickname')
83  {
84  $this->response_data['nickname'] = $response->message->args->values[$key];
85  }
86  if($mapping[1] == 'sreg.email')
87  {
88  $this->response_data['email'] = $response->message->args->values[$key];
89  }
90  }
91  }
92  return true;
93 
94  }
95  return false;
96  }
97 
105  public function forceCreation($a_status)
106  {
107  $this->force_creation = true;
108  }
109 
110 
114  public function loginObserver($a_username,$a_auth)
115  {
116  global $ilLog;
117 
118  $this->initSettings();
119  $this->response_data['ilInternalAccount'] = ilObjUser::_checkExternalAuthAccount(
120  "openid",
121  $this->response_data['nickname']
122  );
123  if(!$this->response_data['ilInternalAccount'])
124  {
125  if($this->settings->isCreationEnabled())
126  {
127  if($this->settings->isAccountMigrationEnabled() and !$this->force_creation and !$_SESSION['force_creation'])
128  {
129  $a_auth->logout();
130  $_SESSION['tmp_auth_mode'] = 'openid';
131  $_SESSION['tmp_oid_username'] = urldecode($_GET['openid_identity']);
132  $_SESSION['tmp_external_account'] = $this->response_data['nickname'];
133  $_SESSION['tmp_pass'] = $_POST['password'];
134  $_SESSION['tmp_roles'] = array(0 => $this->settings->getDefaultRole());
135 
136  ilUtil::redirect('ilias.php?baseClass=ilStartUpGUI&cmd=showAccountMigration&cmdClass=ilstartupgui');
137  }
138 
139  include_once './Services/OpenId/classes/class.ilOpenIdAttributeToUser.php';
141  $new_name = $new_user->create($this->response_data['nickname'],$this->response_data);
142 
143  $a_auth->setAuth($new_name);
144  return true;
145  }
146  else
147  {
148  // No syncronisation allowed => create Error
149  $a_auth->status = AUTH_OPENID_NO_ILIAS_USER;
150  $a_auth->logout();
151  return false;
152  }
153 
154  }
155  else
156  {
157  $a_auth->setAuth($this->response_data['ilInternalAccount']);
158  return true;
159  }
160  return false;
161  }
162 
167  protected function initSettings()
168  {
169  include_once './Services/OpenId/classes/class.ilOpenIdSettings.php';
170  $this->settings = ilOpenIdSettings::getInstance();
171  $this->settings->initConsumer();
172  }
173 }
174 ?>