ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAuthProviderOpenIdConnect.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 
14 {
15  const OIDC_AUTH_IDTOKEN = "oidc_auth_idtoken";
19  private $settings = null;
20 
21  private $lng = null;
22 
23 
29  {
30  global $DIC;
31  parent::__construct($credentials);
33  $this->lng = $DIC->language();
34  }
35 
39  public function handleLogout()
40  {
41  if ($this->settings->getLogoutScope() == ilOpenIdConnectSettings::LOGOUT_SCOPE_LOCAL) {
42  return false;
43  }
44 
45  $id_token = ilSession::get(self::OIDC_AUTH_IDTOKEN);
46  $this->getLogger()->debug('Logging out with token: ' . $id_token);
47 
48 
49  if (is_string($id_token) && $id_token !== '') {
50  ilSession::set(self::OIDC_AUTH_IDTOKEN, '');
51  $oidc = $this->initClient();
52  try {
53  $oidc->signOut(
54  $id_token,
55  ILIAS_HTTP_PATH . '/logout.php'
56  );
57  } catch (\Jumbojett\OpenIDConnectClientException $e) {
58  $this->getLogger()->warning("Logging out of OIDC provider failed with: " . $e->getMessage());
59  }
60  }
61  }
62 
69  {
70  try {
71  $oidc = $this->initClient();
72  $oidc->setRedirectURL(ILIAS_HTTP_PATH . '/openidconnect.php');
73 
75  if ($proxy->isActive()) {
76  $host = $proxy->getHost();
77  $port = $proxy->getPort();
78  if ($port) {
79  $host .= ":" . $port;
80  }
81  $oidc->setHttpProxy($host);
82  }
83 
84  $this->getLogger()->debug(
85  'Redirect url is: ' .
86  $oidc->getRedirectURL()
87  );
88 
89  $oidc->addScope($this->settings->getAllScopes());
90  switch ($this->settings->getLoginPromptType()) {
92  $oidc->addAuthParam(['prompt' => 'login']);
93  break;
94  }
95 
96  $oidc->authenticate();
97  // user is authenticated, otherwise redirected to authorization endpoint or exception
98  $this->getLogger()->dump($_REQUEST, \ilLogLevel::DEBUG);
99 
100  $claims = $oidc->getVerifiedClaims(null);
101  $this->getLogger()->dump($claims, \ilLogLevel::DEBUG);
102  $status = $this->handleUpdate($status, $claims);
103 
104  // @todo : provide a general solution for all authentication methods
105  $_GET['target'] = (string) $this->getCredentials()->getRedirectionTarget();
106 
107  if ($this->settings->getLogoutScope() == ilOpenIdConnectSettings::LOGOUT_SCOPE_GLOBAL) {
108  $token = $oidc->requestUserInfo();
109  ilSession::set(self::OIDC_AUTH_IDTOKEN, $oidc->getIdToken());
110  }
111  return true;
112  } catch (Exception $e) {
113  $this->getLogger()->warning($e->getMessage());
114  $this->getLogger()->warning($e->getCode());
116  $status->setTranslatedReason($this->lng->txt("auth_oidc_failed"));
117  return false;
118  }
119  }
120 
121 
126  private function handleUpdate(ilAuthStatus $status, $user_info)
127  {
128  if (!is_object($user_info)) {
129  $this->getLogger()->error('Received invalid user credentials: ');
130  $this->getLogger()->dump($user_info, ilLogLevel::ERROR);
132  $status->setReason('err_wrong_login');
133  return false;
134  }
135 
136  $uid_field = $this->settings->getUidField();
137  $ext_account = $user_info->$uid_field;
138 
139  $this->getLogger()->debug('Authenticated external account: ' . $ext_account);
140 
141 
144  $ext_account
145  );
146 
147  try {
148  $sync = new ilOpenIdConnectUserSync($this->settings, $user_info);
149  if (!is_string($ext_account)) {
151  $status->setReason('err_wrong_login');
152  return $status;
153  }
154  $sync->setExternalAccount($ext_account);
155  $sync->setInternalAccount($int_account);
156  $sync->updateUser();
157 
158  $user_id = $sync->getUserId();
159  ilSession::set('used_external_auth', true);
162 
163  // @todo : provide a general solution for all authentication methods
164  $_GET['target'] = (string) $this->getCredentials()->getRedirectionTarget();
167  $status->setReason('err_wrong_login');
168  }
169 
170  return $status;
171  }
172 
176  private function initClient() : OpenIDConnectClient
177  {
178  $oidc = new OpenIDConnectClient(
179  $this->settings->getProvider(),
180  $this->settings->getClientId(),
181  $this->settings->getSecret()
182  );
183  return $oidc;
184  }
185 }
doAuthentication(\ilAuthStatus $status)
Do authentication.
settings()
Definition: settings.php:2
Interface of auth credentials.
$_GET["client_id"]
const STATUS_AUTHENTICATION_FAILED
static get($a_var)
Get a value.
static set($a_var, $a_val)
Set a value.
setTranslatedReason($a_reason)
Set translated reason.
static getInstance()
Get singleton instance.
setAuthenticatedUserId($a_id)
Base class for authentication providers (radius, ldap, apache, ...)
Standard interface for auth provider implementations.
setStatus($a_status)
Set auth status.
Class ilAuthProviderOpenIdConnect.
$token
Definition: xapitoken.php:52
global $DIC
Definition: goto.php:24
setReason($a_reason)
Set reason.
static _checkExternalAuthAccount($a_auth, $a_account, $tryFallback=true)
check whether external account and authentication method matches with a user
getLogger()
Get logger.
Class ilOpenIdConnectSettingsGUI.
__construct(Container $dic, ilPlugin $plugin)
handleUpdate(ilAuthStatus $status, $user_info)
Auth status implementation.
static _getInstance()
Getter for unique instance.
__construct(ilAuthCredentials $credentials)
ilAuthProviderOpenIdConnect constructor.