FusionDirectory
class_LoginCAS.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2017-2018 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
24 class LoginCAS extends LoginMethod
25 {
27  static function getLabel ()
28  {
29  return _('CAS');
30  }
31 
33  static function initCAS ()
34  {
35  global $config;
36 
37  require_once('CAS.php');
38  /* Move FD autoload after CAS autoload */
39  spl_autoload_unregister('fusiondirectory_autoload');
40  spl_autoload_register('fusiondirectory_autoload');
41 
42  if ($config->get_cfg_value('CasVerbose') == 'TRUE') {
43  phpCAS::setVerbose(TRUE);
44  }
45 
46  // Initialize CAS with proper library and call.
47  if ($config->get_cfg_value('CasLibraryBool') === 'TRUE') {
48  phpCAS::client(
49  CAS_VERSION_2_0,
50  $config->get_cfg_value('CasHost', 'localhost'),
51  (int) ($config->get_cfg_value('CasPort', 443)),
52  $config->get_cfg_value('CasContext'),
53  $config->get_cfg_value('CasClientServiceName')
54  );
55  } else {
56  phpCAS::client(
57  CAS_VERSION_2_0,
58  $config->get_cfg_value('CasHost', 'localhost'),
59  (int) ($config->get_cfg_value('CasPort', 443)),
60  $config->get_cfg_value('CasContext')
61  );
62  }
63 
64  // Set the CA certificate that is the issuer of the cert
65  phpCAS::setCasServerCACert($config->get_cfg_value('CasServerCaCertPath'));
66  }
67 
69  static function loginProcess ()
70  {
71  global $config, $message, $ui;
72 
73  static::init();
74 
75  static::initCAS();
76 
77  /* Reset error messages */
78  $message = '';
79 
80  /* Remove query string from redirection URL to avoid signout loops */
81  phpCAS::setFixedServiceURL(preg_replace('/\?.*$/', '', phpCAS::getServiceURL()));
82 
83  /* Force CAS authentication */
84  phpCAS::forceAuthentication();
85  static::$username = phpCAS::getUser();
86 
87  $ui = userinfo::getLdapUser(static::$username);
88 
89  if ($ui === FALSE) {
90  throw new FatalError(
91  htmlescape(sprintf(
92  _('CAS user "%s" could not be found in LDAP'),
93  static::$username
94  ))
95  );
96  } elseif (is_string($ui)) {
97  throw new FatalError(
98  htmlescape(sprintf(
99  _('Login with user "%s" triggered error: %s'),
100  static::$username,
101  $ui
102  ))
103  );
104  }
105 
106  $ui->loadACL();
107 
108  $success = static::runSteps([
109  'checkForLockingBranch',
110  'loginAndCheckExpired',
111  'runSchemaCheck',
112  ]);
113 
114  if ($success) {
115  /* Everything went well, redirect to main.php */
116  static::redirect();
117  } else {
119  if (!empty($message)) {
120  throw new FatalError(
121  htmlescape(sprintf(
122  _('Login with user "%s" triggered error: %s'),
123  static::$username,
124  $message
125  ))
126  );
127  }
128  exit();
129  }
130  }
131 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
static getLdapUser(string $username)
Get user from LDAP directory.
static get_dialogs()
Accessor of the message dialog rendered HTML.
static getLabel()
Displayed name.
Login via CAS.
Base class for login methods.
Fatal error class. Does not extend FusionDirectoryError.
static initCAS()
Initialize phpCAS library.
static loginProcess()
All login steps in the right order for CAS login.