ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSimpleSAMLphpWrapper.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'libs/composer/vendor/autoload.php';
5 require_once 'Services/Saml/interfaces/interface.ilSamlAuth.php';
6 
11 {
15  protected $config;
16 
20  protected $authSource;
21 
28  public function __construct($authSourceName, $configurationPath)
29  {
30  $this->initConfigFiles($configurationPath);
31 
32  SimpleSAML_Configuration::setConfigDir($configurationPath);
33  $this->config = SimpleSAML_Configuration::getInstance();
34 
35  $sessionHandler = $this->config->getString('session.handler', false);
36  $storageType = $this->config->getString('store.type', false);
37 
38  if (
39  $storageType == 'phpsession' || $sessionHandler == 'phpsession' ||
40  (empty($storageType) && empty($sessionHandler))
41  ) {
42  throw new RuntimeException('Invalid SimpleSAMLphp session handler: Must not be phpsession');
43  }
44 
45  $this->authSource = new SimpleSAML\Auth\Simple($authSourceName);
46  }
47 
51  protected function initConfigFiles($configurationPath)
52  {
53  global $DIC;
54 
55  $templateHandler = new ilSimpleSAMLphpConfigTemplateHandler($DIC->filesystem()->storage());
56  $templateHandler->copy('./Services/Saml/lib/config.php.dist', 'auth/saml/config/config.php', [
57  'DB_PATH' => rtrim($configurationPath, '/') . '/ssphp.sq3',
58  'SQL_INITIAL_PASSWORD' => function () {
59  require_once 'Services/Password/classes/class.ilPasswordUtils.php';
60  return substr(str_replace('+', '.', base64_encode(ilPasswordUtils::getBytes(20))), 0, 10);
61  },
62  'COOKIE_PATH' => IL_COOKIE_PATH,
63  'LOG_DIRECTORY' => ilLoggingDBSettings::getInstance()->getLogDir()
64  ]);
65  $templateHandler->copy('./Services/Saml/lib/authsources.php.dist', 'auth/saml/config/authsources.php', [
66  'RELAY_STATE' => rtrim(ILIAS_HTTP_PATH, '/') . '/saml.php',
67  'SP_ENTITY_ID' => rtrim(ILIAS_HTTP_PATH, '/') . '/Services/Saml/lib/metadata.php'
68  ]);
69  }
70 
74  public function getAuthId()
75  {
76  return $this->authSource->getAuthSource()->getAuthId();
77  }
78 
82  public function protectResource()
83  {
84  $this->authSource->requireAuth();
85  }
86 
90  public function storeParam($key, $value)
91  {
93  $session->setData('ilias', $key, $value);
94  }
95 
99  public function getParam($key)
100  {
102 
103  $value = $session->getData('ilias', $key);
104 
105  return $value;
106  }
107 
111  public function popParam($key)
112  {
114  $value = $this->getParam($key);
115  $session->deleteData('ilias', $key);
116 
117  return $value;
118  }
119 
123  public function isAuthenticated()
124  {
125  return $this->authSource->isAuthenticated();
126  }
127 
131  public function getAttributes()
132  {
133  return $this->authSource->getAttributes();
134  }
135 
139  public function logout($returnUrl = '')
140  {
141  ilSession::set('used_external_auth', false);
142 
143  $params = array(
144  'ReturnStateParam' => 'LogoutState',
145  'ReturnStateStage' => 'ilLogoutState'
146  );
147 
148  if (strlen($returnUrl) > 0) {
149  $params['ReturnTo'] = $returnUrl;
150  }
151 
152  $this->authSource->logout($params);
153  }
154 
158  public function getIdpDiscovery()
159  {
160  return new ilSimpleSAMLphplIdpDiscovery();
161  }
162 
166  public function getAuthDataArray()
167  {
168  return $this->authSource->getAuthDataArray();
169  }
170 }
global $DIC
Definition: saml.php:7
protectResource()
Protect a script resource with a SAML auth.
$session
static set($a_var, $a_val)
Set a value.
__construct($authSourceName, $configurationPath)
ilSimpleSAMLphpWrapper constructor.
static getBytes($length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.
static setConfigDir($path, $configSet='simplesaml')
Set the directory for configuration files for the given configuration set.
copy($sourcePath, $destinationPath, array $placeholders=[])
Interface ilSamlAuth.
const IL_COOKIE_PATH(isset($_GET["client_id"]))
Definition: metadata.php:32
static getInstance()
Get instance.
Class ilSimpleSAMLphpConfigTemplateHandler.
Class ilSimpleSAMLphpWrapper.
$key
Definition: croninfo.php:18
Class ilSimpleSAMLphplIdpDiscovery.
static getSessionFromRequest()
Retrieves the current session.
Definition: Session.php:241
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.