ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 {
18  private $settings = null;
19 
20  private $lng = null;
21 
22 
28  {
29  global $DIC;
30  parent::__construct($credentials);
32  $this->lng = $DIC->language();
33  }
34 
38  public function handleLogout()
39  {
40  if ($this->settings->getLogoutScope() == ilOpenIdConnectSettings::LOGOUT_SCOPE_LOCAL) {
41  return false;
42  }
43 
44  $auth_token = ilSession::get('oidc_auth_token');
45  $this->getLogger()->debug('Using token: ' . $auth_token);
46 
47  if (strlen($auth_token)) {
48  ilSession::set('oidc_auth_token', '');
49  $oidc = $this->initClient();
50  $oidc->signOut(
51  $auth_token,
52  ILIAS_HTTP_PATH . '/logout.php'
53  );
54  }
55  }
56 
63  {
64  try {
65  $oidc = $this->initClient();
66  $oidc->setRedirectURL(ILIAS_HTTP_PATH . '/openidconnect.php');
67 
68  $this->getLogger()->debug(
69  'Redirect url is: ' .
70  $oidc->getRedirectURL()
71  );
72 
73  $oidc->setResponseTypes(
74  [
75  'id_token'
76  ]
77  );
78  $oidc->addScope(
79  [
80  'openid',
81  'profile',
82  'email',
83  'roles'
84  ]
85  );
86 
87 
88  $oidc->addAuthParam(['response_mode' => 'form_post']);
89  switch ($this->settings->getLoginPromptType()) {
91  $oidc->addAuthParam(['prompt' => 'login']);
92  break;
93  }
94  $oidc->setAllowImplicitFlow(true);
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->requestClientCredentialsToken();
109  ilSession::set('oidc_auth_token', $token->access_token);
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:57
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)
$DIC
Definition: xapitoken.php:46
handleUpdate(ilAuthStatus $status, $user_info)
Auth status implementation.
__construct(ilAuthCredentials $credentials)
ilAuthProviderOpenIdConnect constructor.