ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilSimpleSAMLphpWrapper.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
12 protected $config;
13
17 protected $authSource;
18
25 public function __construct($authSourceName, $configurationPath)
26 {
27 $this->initConfigFiles($configurationPath);
28
29 SimpleSAML\Configuration::setConfigDir($configurationPath);
30 $this->config = SimpleSAML\Configuration::getInstance();
31
32 $sessionHandler = $this->config->getString('session.handler', false);
33 $storageType = $this->config->getString('store.type', false);
34
35 if (
36 $storageType == 'phpsession' || $sessionHandler == 'phpsession' ||
37 (empty($storageType) && empty($sessionHandler))
38 ) {
39 throw new RuntimeException('Invalid SimpleSAMLphp session handler: Must not be phpsession');
40 }
41
42 $this->authSource = new SimpleSAML\Auth\Simple($authSourceName);
43 }
44
48 protected function initConfigFiles($configurationPath)
49 {
50 global $DIC;
51
52 $templateHandler = new ilSimpleSAMLphpConfigTemplateHandler($DIC->filesystem()->storage());
53 $templateHandler->copy('./Services/Saml/lib/config.php.dist', 'auth/saml/config/config.php', [
54 'DB_PATH' => rtrim($configurationPath, '/') . '/ssphp.sq3',
55 'SQL_INITIAL_PASSWORD' => function () {
56 return substr(str_replace('+', '.', base64_encode(ilPasswordUtils::getBytes(20))), 0, 10);
57 },
58 'COOKIE_PATH' => IL_COOKIE_PATH,
59 'LOG_DIRECTORY' => ilLoggingDBSettings::getInstance()->getLogDir()
60 ]);
61 $templateHandler->copy('./Services/Saml/lib/authsources.php.dist', 'auth/saml/config/authsources.php', [
62 'RELAY_STATE' => rtrim(ILIAS_HTTP_PATH, '/') . '/saml.php',
63 'SP_ENTITY_ID' => rtrim(ILIAS_HTTP_PATH, '/') . '/Services/Saml/lib/metadata.php'
64 ]);
65 }
66
70 public function getAuthId() : string
71 {
72 return $this->authSource->getAuthSource()->getAuthId();
73 }
74
78 public function protectResource() : void
79 {
80 $this->authSource->requireAuth();
81 }
82
86 public function storeParam($key, $value)
87 {
88 $session = SimpleSAML\Session::getSessionFromRequest();
89 $session->setData('ilias', $key, $value);
90 }
91
95 public function getParam(string $key)
96 {
97 $session = SimpleSAML\Session::getSessionFromRequest();
98
99 $value = $session->getData('ilias', $key);
100
101 return $value;
102 }
103
107 public function popParam(string $key)
108 {
109 $session = SimpleSAML\Session::getSessionFromRequest();
110 $value = $this->getParam($key);
111 $session->deleteData('ilias', $key);
112
113 return $value;
114 }
115
119 public function isAuthenticated() : bool
120 {
121 return $this->authSource->isAuthenticated();
122 }
123
127 public function getAttributes() : array
128 {
129 return $this->authSource->getAttributes();
130 }
131
135 public function logout(string $returnUrl = '') : void
136 {
137 ilSession::set('used_external_auth', false);
138
139 $params = [
140 'ReturnStateParam' => 'LogoutState',
141 'ReturnStateStage' => 'ilLogoutState'
142 ];
143
144 if (strlen($returnUrl) > 0) {
145 $params['ReturnTo'] = $returnUrl;
146 }
147
148 $this->authSource->logout($params);
149 }
150
155 {
156 return new ilSimpleSAMLphplIdpDiscovery();
157 }
158
162 public function getAuthDataArray() : array
163 {
164 return $this->authSource->getAuthDataArray();
165 }
166}
An exception for terminatinating execution or to throw for unit testing.
static getInstance()
Get instance.
static getBytes($length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.
static set($a_var, $a_val)
Set a value.
Class ilSimpleSAMLphpWrapper.
protectResource()
Protect a script resource with a SAML auth.
__construct($authSourceName, $configurationPath)
ilSimpleSAMLphpWrapper constructor.
Class ilSimpleSAMLphplIdpDiscovery.
global $DIC
Definition: goto.php:24
Interface ilSamlAuth.
const IL_COOKIE_PATH(isset($_GET["client_id"]))
Definition: metadata.php:47
$session