ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
authmemcookie.php
Go to the documentation of this file.
1 <?php
2 
15 require_once('_include.php');
16 
17 try {
18  // load SimpleSAMLphp configuration
20 
21  // check if this module is enabled
22  if (!$globalConfig->getBoolean('enable.authmemcookie', false)) {
23  throw new SimpleSAML_Error_Error('NOACCESS');
24  }
25 
26  // load Auth MemCookie configuration
28 
29  $sourceId = $amc->getAuthSource();
30  $s = new \SimpleSAML\Auth\Simple($sourceId);
31 
32  // check if the user is authorized. We attempt to authenticate the user if not
33  $s->requireAuth();
34 
35  // generate session id and save it in a cookie
37  $cookieName = $amc->getCookieName();
39 
40  // generate the authentication information
41  $attributes = $s->getAttributes();
42 
44 
45  // username
46  $usernameAttr = $amc->getUsernameAttr();
47  if (!array_key_exists($usernameAttr, $attributes)) {
48  throw new Exception(
49  "The user doesn't have an attribute named '".$usernameAttr.
50  "'. This attribute is expected to contain the username."
51  );
52  }
54 
55  // groups
56  $groupsAttr = $amc->getGroupsAttr();
57  if ($groupsAttr !== null) {
58  if (!array_key_exists($groupsAttr, $attributes)) {
59  throw new Exception(
60  "The user doesn't have an attribute named '".$groupsAttr.
61  "'. This attribute is expected to contain the groups the user is a member of."
62  );
63  }
64  $authData['Groups'] = $attributes[$groupsAttr];
65  } else {
66  $authData['Groups'] = array();
67  }
68 
69  $authData['RemoteIP'] = $_SERVER['REMOTE_ADDR'];
70 
71  foreach ($attributes as $n => $v) {
72  $authData['ATTR_'.$n] = $v;
73  }
74 
75  // store the authentication data in the memcache server
76  $data = '';
77  foreach ($authData as $name => $values) {
78  if (is_array($values)) {
79  foreach ($values as $i => $value) {
80  if (!is_a($value, 'DOMNodeList')) {
81  continue;
82  }
83  /* @var \DOMNodeList $value */
84  if ($value->length === 0) {
85  continue;
86  }
87  $values[$i] = new \SAML2\XML\saml\AttributeValue($value->item(0)->parentNode);
88  }
89  $values = implode(':', $values);
90  }
91  $data .= $name.'='.$values."\r\n";
92  }
93 
94  $memcache = $amc->getMemcache();
95  $expirationTime = $s->getAuthData('Expire');
97 
98  // register logout handler
100  $session->registerLogoutHandler($sourceId, 'SimpleSAML_AuthMemCookie', 'logoutHandler');
101 
102  // redirect the user back to this page to signal that the login is completed
103  \SimpleSAML\Utils\HTTP::redirectTrustedURL(\SimpleSAML\Utils\HTTP::getSelfURL());
104 } catch (Exception $e) {
105  throw new SimpleSAML_Error_Error('CONFIG', $e);
106 }
static generateID()
Generate a random identifier, ID_LENGTH bytes long.
Definition: Random.php:26
foreach($authData as $name=> $values) $memcache
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$groupsAttr
foreach($attributes as $n=> $v) $data
$sessionID
static getInstance()
This function is used to retrieve the singleton instance of this class.
$expirationTime
$attributes
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:962
$cookieName
Attribute-related utility methods.
if($format !==null) $name
Definition: metadata.php:146
static setCookie($name, $value, $params=null, $throw=true)
Set a cookie.
Definition: HTTP.php:1107
if(! $globalConfig->getBoolean('enable.authmemcookie', false)) $amc
$session
$usernameAttr
$n
Definition: RandomTest.php:85
Create styles array
The data for the language used.
$globalConfig
$authData
$i
Definition: disco.tpl.php:19
static getSessionFromRequest()
Retrieves the current session.
Definition: Session.php:243
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$sourceId