ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Simple.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleSAML\Auth;
4 
5 use \SimpleSAML_Auth_Source as Source;
6 use \SimpleSAML_Auth_State as State;
7 use \SimpleSAML_Configuration as Configuration;
8 use \SimpleSAML_Error_AuthSource as AuthSourceError;
9 use \SimpleSAML\Module;
10 use \SimpleSAML_Session as Session;
11 use \SimpleSAML\Utils\HTTP;
12 
18 class Simple
19 {
20 
26  protected $authSource;
27 
31  protected $app_config;
32 
38  public function __construct($authSource)
39  {
40  assert('is_string($authSource)');
41 
42  $this->authSource = $authSource;
43  $this->app_config = Configuration::getInstance()->getConfigItem('application', null);
44  }
45 
46 
54  public function getAuthSource()
55  {
56  $as = Source::getById($this->authSource);
57  if ($as === null) {
58  throw new AuthSourceError($this->authSource, 'Unknown authentication source.');
59  }
60  return $as;
61  }
62 
63 
72  public function isAuthenticated()
73  {
74  $session = Session::getSessionFromRequest();
75 
76  return $session->isValid($this->authSource);
77  }
78 
79 
93  public function requireAuth(array $params = array())
94  {
95 
96  $session = Session::getSessionFromRequest();
97 
98  if ($session->isValid($this->authSource)) {
99  // Already authenticated
100  return;
101  }
102 
103  $this->login($params);
104  }
105 
106 
121  public function login(array $params = array())
122  {
123 
124  if (array_key_exists('KeepPost', $params)) {
125  $keepPost = (bool) $params['KeepPost'];
126  } else {
127  $keepPost = true;
128  }
129 
130  if (array_key_exists('ReturnTo', $params)) {
131  $returnTo = (string) $params['ReturnTo'];
132  } else {
133  if (array_key_exists('ReturnCallback', $params)) {
134  $returnTo = (array) $params['ReturnCallback'];
135  } else {
136  $returnTo = HTTP::getSelfURL();
137  }
138  }
139 
140  if (is_string($returnTo) && $keepPost && $_SERVER['REQUEST_METHOD'] === 'POST') {
142  }
143 
144  if (array_key_exists('ErrorURL', $params)) {
145  $errorURL = (string) $params['ErrorURL'];
146  } else {
147  $errorURL = null;
148  }
149 
150 
151  if (!isset($params[State::RESTART]) && is_string($returnTo)) {
152  /*
153  * A URL to restart the authentication, in case the user bookmarks
154  * something, e.g. the discovery service page.
155  */
156  $restartURL = $this->getLoginURL($returnTo);
157  $params[State::RESTART] = $restartURL;
158  }
159 
160  $as = $this->getAuthSource();
161  $as->initLogin($returnTo, $errorURL, $params);
162  assert('false');
163  }
164 
165 
181  public function logout($params = null)
182  {
183  assert('is_array($params) || is_string($params) || is_null($params)');
184 
185  if ($params === null) {
186  $params = HTTP::getSelfURL();
187  }
188 
189  if (is_string($params)) {
190  $params = array(
191  'ReturnTo' => $params,
192  );
193  }
194 
195  assert('is_array($params)');
196  assert('isset($params["ReturnTo"]) || isset($params["ReturnCallback"])');
197 
198  if (isset($params['ReturnStateParam']) || isset($params['ReturnStateStage'])) {
199  assert('isset($params["ReturnStateParam"]) && isset($params["ReturnStateStage"])');
200  }
201 
202  $session = Session::getSessionFromRequest();
203  if ($session->isValid($this->authSource)) {
204  $state = $session->getAuthData($this->authSource, 'LogoutState');
205  if ($state !== null) {
206  $params = array_merge($state, $params);
207  }
208 
209  $session->doLogout($this->authSource);
210 
211  $params['LogoutCompletedHandler'] = array(get_class(), 'logoutCompleted');
212 
213  $as = Source::getById($this->authSource);
214  if ($as !== null) {
215  $as->logout($params);
216  }
217  }
218 
219  self::logoutCompleted($params);
220  }
221 
222 
230  public static function logoutCompleted($state)
231  {
232  assert('is_array($state)');
233  assert('isset($state["ReturnTo"]) || isset($state["ReturnCallback"])');
234 
235  if (isset($state['ReturnCallback'])) {
236  call_user_func($state['ReturnCallback'], $state);
237  assert('false');
238  } else {
239  $params = array();
240  if (isset($state['ReturnStateParam']) || isset($state['ReturnStateStage'])) {
241  assert('isset($state["ReturnStateParam"]) && isset($state["ReturnStateStage"])');
242  $stateID = State::saveState($state, $state['ReturnStateStage']);
243  $params[$state['ReturnStateParam']] = $stateID;
244  }
246  }
247  }
248 
249 
258  public function getAttributes()
259  {
260 
261  if (!$this->isAuthenticated()) {
262  // Not authenticated
263  return array();
264  }
265 
266  // Authenticated
267  $session = Session::getSessionFromRequest();
268  return $session->getAuthData($this->authSource, 'Attributes');
269  }
270 
271 
279  public function getAuthData($name)
280  {
281  assert('is_string($name)');
282 
283  if (!$this->isAuthenticated()) {
284  return null;
285  }
286 
287  $session = Session::getSessionFromRequest();
288  return $session->getAuthData($this->authSource, $name);
289  }
290 
291 
297  public function getAuthDataArray()
298  {
299 
300  if (!$this->isAuthenticated()) {
301  return null;
302  }
303 
304  $session = Session::getSessionFromRequest();
305  return $session->getAuthState($this->authSource);
306  }
307 
308 
317  public function getLoginURL($returnTo = null)
318  {
319  assert('is_null($returnTo) || is_string($returnTo)');
320 
321  if ($returnTo === null) {
322  $returnTo = HTTP::getSelfURL();
323  }
324 
325  $login = Module::getModuleURL('core/as_login.php', array(
326  'AuthId' => $this->authSource,
327  'ReturnTo' => $returnTo,
328  ));
329 
330  return $login;
331  }
332 
333 
342  public function getLogoutURL($returnTo = null)
343  {
344  assert('is_null($returnTo) || is_string($returnTo)');
345 
346  if ($returnTo === null) {
347  $returnTo = HTTP::getSelfURL();
348  }
349 
350  $logout = Module::getModuleURL('core/as_logout.php', array(
351  'AuthId' => $this->authSource,
352  'ReturnTo' => $returnTo,
353  ));
354 
355  return $logout;
356  }
357 
358 
369  protected function getProcessedURL($url = null)
370  {
371  if ($url === null) {
372  $url = HTTP::getSelfURL();
373  }
374 
375  $scheme = parse_url($url, PHP_URL_SCHEME);
376  $host = parse_url($url, PHP_URL_HOST) ?: HTTP::getSelfHost();
377  $port = parse_url($url, PHP_URL_PORT) ?: (
378  $scheme ? '' : trim(HTTP::getServerPort(), ':')
379  );
380  $scheme = $scheme ?: (HTTP::getServerHTTPS() ? 'https' : 'http');
381  $path = parse_url($url, PHP_URL_PATH) ?: '/';
382  $query = parse_url($url, PHP_URL_QUERY) ?: '';
383  $fragment = parse_url($url, PHP_URL_FRAGMENT) ?: '';
384 
385  $port = !empty($port) ? ':'.$port : '';
386  if (($scheme === 'http' && $port === ':80') || ($scheme === 'https' && $port === ':443')) {
387  $port = '';
388  }
389 
390  if (is_null($this->app_config)) {
391  // nothing more we can do here
392  return $scheme.'://'.$host.$port.$path.($query ? '?'.$query : '').($fragment ? '#'.$fragment : '');
393  }
394 
395  $base = trim($this->app_config->getString(
396  'baseURL',
397  $scheme.'://'.$host.$port
398  ), '/');
399  return $base.$path.($query ? '?'.$query : '').($fragment ? '#'.$fragment : '');
400  }
401 }
$params
Definition: disable.php:11
Add rich text string
if(!isset($_REQUEST['ReturnTo'])) $returnTo
Definition: authpage.php:16
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
getAuthDataArray()
Retrieve all authentication data.
Definition: Simple.php:297
static getServerHTTPS()
Retrieve HTTPS status from $_SERVER environment variables.
Definition: HTTP.php:84
getLoginURL($returnTo=null)
Retrieve a URL that can be used to log the user in.
Definition: Simple.php:317
isAuthenticated()
Check if the user is authenticated.
Definition: Simple.php:72
$session
getAuthData($name)
Retrieve authentication data.
Definition: Simple.php:279
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:962
login(array $params=array())
Start an authentication process.
Definition: Simple.php:121
logout($params=null)
Log the user out.
Definition: Simple.php:181
getAttributes()
Retrieve attributes of the current user.
Definition: Simple.php:258
$base
Definition: index.php:4
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:303
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
if($format !==null) $name
Definition: metadata.php:146
__construct($authSource)
Create an instance with the specified authsource.
Definition: Simple.php:38
getAuthSource()
Retrieve the implementing authentication source.
Definition: Simple.php:54
$as
getProcessedURL($url=null)
Process a URL and modify it according to the application/baseURL configuration option, if present.
Definition: Simple.php:369
static getSelfHost()
Retrieve our own host.
Definition: HTTP.php:699
requireAuth(array $params=array())
Require the user to be authenticated.
Definition: Simple.php:93
$query
Create styles array
The data for the language used.
"Manueller" Session-Fallback mit PHP4
static getServerPort()
Retrieve the port number from $_SERVER environment variables.
Definition: HTTP.php:109
getLogoutURL($returnTo=null)
Retrieve a URL that can be used to log the user out.
Definition: Simple.php:342
$url
$_POST["username"]
static getPOSTRedirectURL($destination, $data)
Create a link which will POST data.
Definition: HTTP.php:668
static logoutCompleted($state)
Called when logout operation completes.
Definition: Simple.php:230