2 /** External authentication via CAS for FusionForge
3 * Copyright 2007, Benoit Lavenier <benoit.lavenier@ifremer.fr>
4 * Copyright 2011, Roland Mas
6 * This file is part of FusionForge
8 * This plugin, like FusionForge, is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with FusionForge; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
23 require_once $GLOBALS['gfcommon'].'include/User.class.php';
25 // from phpCAS (https://wiki.jasig.org/display/CASC/phpCAS)
26 require_once 'CAS.php';
29 * Authentication manager for FusionForge CASification
32 class AuthCASPlugin extends ForgeAuthPlugin {
33 function AuthCASPlugin () {
35 $this->ForgeAuthPlugin() ;
36 $this->name = "authcas";
37 $this->text = "CAS authentication";
39 $this->_addHook('display_auth_form');
40 $this->_addHook("check_auth_session");
41 $this->_addHook("fetch_authenticated_user");
42 $this->_addHook("close_auth_session");
44 $this->saved_login = '';
45 $this->saved_user = NULL;
47 $this->declareConfigVars();
50 private static $init = false;
57 phpCAS::client(forge_get_config('cas_version', $this->name),
58 forge_get_config('cas_server', $this->name),
59 intval(forge_get_config('cas_port', $this->name)),
61 if (forge_get_config('validate_server_certificate', $this->name)) {
64 phpCAS::setNoCasServerValidation();
71 * Display a form to input credentials
72 * @param unknown_type $params
75 function displayAuthForm(&$params) {
76 if (!$this->isRequired() && !$this->isSufficient()) {
79 $return_to = $params['return_to'];
86 $result .= _('Cookies must be enabled past this point.');
89 $result .= '<form action="' . util_make_url('/plugins/authcas/post-login.php') . '" method="get">
90 <input type="hidden" name="form_key" value="' . form_generate_key() . '"/>
91 <input type="hidden" name="return_to" value="' . htmlspecialchars(stripslashes($return_to)) . '" />
92 <p><input type="submit" name="login" value="' . _('Login via CAS') . '" />
96 $params['html_snippets'][$this->name] = $result;
98 $params['transparent_redirect_urls'][$this->name] = util_make_url('/plugins/authcas/post-login.php?return_to='.htmlspecialchars(stripslashes($return_to)).'&login=1');
102 * Is there a valid session?
103 * @param unknown_type $params
105 function checkAuthSession(&$params) {
108 $this->saved_user = NULL;
111 $user_id_from_cookie = $this->checkSessionCookie();
112 if ($user_id_from_cookie) {
113 $user = user_get_object($user_id_from_cookie);
114 $this->saved_user = $user;
115 $this->setSessionCookie();
116 } elseif (phpCAS::isAuthenticated()) {
117 $user = $this->startSession(phpCAS::getUser());
121 if ($this->isSufficient()) {
122 $this->saved_user = $user;
123 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_ACCEPT;
126 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
129 if ($this->isRequired()) {
130 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_REJECT;
132 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
138 * What GFUser is logged in?
139 * @param unknown_type $params
141 function fetchAuthUser(&$params) {
142 if ($this->saved_user && $this->isSufficient()) {
143 $params['results'] = $this->saved_user;
147 function closeAuthSession($params) {
150 if ($this->isSufficient() || $this->isRequired()) {
151 $this->unsetSessionCookie();
152 // logs user out from CAS
153 // TODO : make it optional to not mess with other apps' SSO sessions with CAS
154 phpCAS::logoutWithRedirectService(util_make_url('/'));
161 * Terminate an authentication session
162 * @param unknown_type $params
165 protected function declareConfigVars() {
166 parent::declareConfigVars();
168 forge_define_config_item ('cas_server', $this->name, 'cas.example.com');
169 forge_define_config_item ('cas_port', $this->name, 443);
170 forge_define_config_item ('cas_version', $this->name, '2.0');
172 forge_define_config_item('validate_server_certificate', $this->name, 'no');
173 forge_set_config_item_bool('validate_server_certificate', $this->name);
180 // c-file-style: "bsd"