ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
18  private $settings = null;
19 
20 
26  {
27  parent::__construct($credentials);
29  }
30 
34  public function handleLogout()
35  {
36  if ($this->settings->getLogoutScope() == ilOpenIdConnectSettings::LOGOUT_SCOPE_LOCAL) {
37  return false;
38  }
39 
40  $auth_token = ilSession::get('oidc_auth_token');
41  $this->getLogger()->debug('Using token: ' . $auth_token);
42 
43  if (strlen($auth_token)) {
44  ilSession::set('oidc_auth_token', '');
45  $oidc = $this->initClient();
46  $oidc->signOut(
47  $auth_token,
48  ILIAS_HTTP_PATH . '/logout.php'
49  );
50  }
51  }
52 
59  {
60  try {
61  $oidc = $this->initClient();
62  $oidc->setRedirectURL(ILIAS_HTTP_PATH . '/openidconnect.php');
63 
64  $this->getLogger()->debug(
65  'Redirect url is: ' .
66  $oidc->getRedirectURL()
67  );
68 
69  $oidc->setResponseTypes(
70  [
71  'id_token'
72  ]
73  );
74  $oidc->addScope(
75  [
76  'openid',
77  'profile',
78  'email',
79  'roles'
80  ]
81  );
82 
83 
84  $oidc->addAuthParam(['response_mode' => 'form_post']);
85  switch ($this->settings->getLoginPromptType()) {
87  $oidc->addAuthParam(['prompt' => 'login']);
88  break;
89  }
90  $oidc->setAllowImplicitFlow(true);
91 
92  $oidc->authenticate();
93  // user is authenticated, otherwise redirected to authorization endpoint or exception
94  $this->getLogger()->dump($_REQUEST, \ilLogLevel::DEBUG);
95 
96  $claims = $oidc->getVerifiedClaims(null);
97  $this->getLogger()->dump($claims, \ilLogLevel::DEBUG);
98  $status = $this->handleUpdate($status, $claims);
99 
100  // @todo : provide a general solution for all authentication methods
101  $_GET['target'] = (string) $this->getCredentials()->getRedirectionTarget();
102 
103  if ($this->settings->getLogoutScope() == ilOpenIdConnectSettings::LOGOUT_SCOPE_GLOBAL) {
104  $token = $oidc->requestClientCredentialsToken();
105  ilSession::set('oidc_auth_token', $token->access_token);
106  }
107  return true;
108  } catch (Exception $e) {
109  $this->getLogger()->warning($e->getMessage());
110  $this->getLogger()->warning($e->getCode());
112  $status->setTranslatedReason($e->getMessage());
113  return false;
114  }
115  }
116 
117 
122  private function handleUpdate(ilAuthStatus $status, $user_info)
123  {
124  if (!is_object($user_info)) {
125  $this->getLogger()->error('Received invalid user credentials: ');
126  $this->getLogger()->dump($user_info, ilLogLevel::ERROR);
128  $status->setReason('err_wrong_login');
129  return false;
130  }
131 
132  $uid_field = $this->settings->getUidField();
133  $ext_account = $user_info->$uid_field;
134 
135  $this->getLogger()->debug('Authenticated external account: ' . $ext_account);
136 
137 
140  $ext_account
141  );
142 
143  try {
144  $sync = new ilOpenIdConnectUserSync($this->settings, $user_info);
145  if (!is_string($ext_account)) {
147  $status->setReason('err_wrong_login');
148  return $status;
149  }
150  $sync->setExternalAccount($ext_account);
151  $sync->setInternalAccount($int_account);
152  $sync->updateUser();
153 
154  $user_id = $sync->getUserId();
155  ilSession::set('used_external_auth', true);
158 
159  // @todo : provide a general solution for all authentication methods
160  $_GET['target'] = (string) $this->getCredentials()->getRedirectionTarget();
163  $status->setReason('err_wrong_login');
164  }
165 
166  return $status;
167  }
168 
172  private function initClient() : OpenIDConnectClient
173  {
175  $this->settings->getProvider(),
176  $this->settings->getClientId(),
177  $this->settings->getSecret()
178  );
179  return $oidc;
180  }
181 }
doAuthentication(\ilAuthStatus $status)
Do authentication.
settings()
Definition: settings.php:2
Interface of auth credentials.
Copyright MITRE 2012.
$_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.
$sync
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.
handleUpdate(ilAuthStatus $status, $user_info)
Auth status implementation.
__construct(ilAuthCredentials $credentials)
ilAuthProviderOpenIdConnect constructor.
Require the CURL and JSON PHP extensions to be installed.