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. FusionForge is free software;
7 * you can redistribute it and/or modify it under the terms of the
8 * GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the Licence, or (at your option)
12 * FusionForge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 require_once $gfcommon.'include/User.class.php';
23 require_once $gfcommon.'include/AuthPlugin.class.php';
26 * Authentication manager for FusionForge CASification
29 class AuthCASPlugin extends ForgeAuthPlugin {
30 function AuthCASPlugin () {
32 $this->ForgeAuthPlugin() ;
33 $this->name = "authcas";
34 $this->text = _("CAS authentication");
36 _("This plugin contains a CAS authentication mechanism for
37 FusionForge. It allows users to authenticate against an external CAS
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;
53 // from phpCAS (https://wiki.jasig.org/display/CASC/phpCAS)
54 require_once 'CAS.php';
60 // Uncomment this to activate phpCAS logs in /tmp
63 phpCAS::client(forge_get_config('cas_version', $this->name),
64 forge_get_config('cas_server', $this->name),
65 intval(forge_get_config('cas_port', $this->name)),
66 forge_get_config('cas_context', $this->name));
67 if (forge_get_config('validate_server_certificate', $this->name)) {
70 phpCAS::setNoCasServerValidation();
77 * Display a form to input credentials
78 * @param unknown_type $params
81 function displayAuthForm(&$params) {
82 if (!$this->isRequired() && !$this->isSufficient()) {
85 $return_to = $params['return_to'];
92 $result .= _('Cookies must be enabled past this point.');
95 $result .= '<form action="' . util_make_url('/plugins/authcas/post-login.php') . '" method="get">
96 <input type="hidden" name="form_key" value="' . form_generate_key() . '"/>
97 <input type="hidden" name="return_to" value="' . htmlspecialchars(stripslashes($return_to)) . '" />
98 <p><input type="submit" name="login" value="' . _('Login via CAS') . '" />
102 $params['html_snippets'][$this->name] = $result;
104 $params['transparent_redirect_urls'][$this->name] = util_make_url('/plugins/authcas/post-login.php?return_to='.htmlspecialchars(stripslashes($return_to)).'&login=1');
108 * Is there a valid session?
109 * @param unknown_type $params
111 function checkAuthSession(&$params) {
114 $this->saved_user = NULL;
117 // FIXME: couldn't we just check parent::checkAuthSession() to take into account auth_token ? or I missed something
118 // if we already have a session/user active, use it
119 $user_id_from_cookie = $this->checkSessionCookie();
120 if ($user_id_from_cookie) {
121 $user = user_get_object($user_id_from_cookie);
122 $this->saved_user = $user;
123 $this->setSessionCookie();
124 } elseif (phpCAS::isAuthenticated()) {
125 // otherwise, use the CAS user
126 $user = $this->startSession(phpCAS::getUser());
129 // TODO : document this
131 if ($this->isSufficient()) {
132 $this->saved_user = $user;
133 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_ACCEPT;
136 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
139 if ($this->isRequired()) {
140 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_REJECT;
142 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
148 * What FFUser is logged in?
149 * @param unknown_type $params
151 function fetchAuthUser(&$params) {
152 if ($this->saved_user && $this->isSufficient()) {
153 $params['results'] = $this->saved_user;
157 function closeAuthSession($params) {
160 if ($this->isSufficient() || $this->isRequired()) {
161 $this->unsetSessionCookie();
162 // logs user out from CAS
163 // TODO : make it optional to not mess with other apps' SSO sessions with CAS
164 phpCAS::logoutWithRedirectService(util_make_url('/'));
171 * Terminate an authentication session
172 * @param unknown_type $params
175 protected function declareConfigVars() {
176 parent::declareConfigVars();
178 forge_define_config_item ('cas_server', $this->name, 'cas.example.com');
179 forge_define_config_item ('cas_port', $this->name, 443);
180 forge_define_config_item ('cas_version', $this->name, '2.0');
181 forge_define_config_item ('cas_context', $this->name, '/cas');
183 forge_define_config_item('validate_server_certificate', $this->name, 'no');
184 forge_set_config_item_bool('validate_server_certificate', $this->name);
191 // c-file-style: "bsd"