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 $GLOBALS['gfcommon'].'include/User.class.php';
24 // from phpCAS (https://wiki.jasig.org/display/CASC/phpCAS)
25 require_once 'CAS.php';
28 * Authentication manager for FusionForge CASification
31 class AuthCASPlugin extends ForgeAuthPlugin {
32 function AuthCASPlugin () {
34 $this->ForgeAuthPlugin() ;
35 $this->name = "authcas";
36 $this->text = "CAS authentication";
38 $this->_addHook('display_auth_form');
39 $this->_addHook("check_auth_session");
40 $this->_addHook("fetch_authenticated_user");
41 $this->_addHook("close_auth_session");
43 $this->saved_login = '';
44 $this->saved_user = NULL;
46 $this->declareConfigVars();
49 private static $init = false;
56 // Uncomment this to activate phpCAS logs in /tmp
59 phpCAS::client(forge_get_config('cas_version', $this->name),
60 forge_get_config('cas_server', $this->name),
61 intval(forge_get_config('cas_port', $this->name)),
62 forge_get_config('cas_context', $this->name));
63 if (forge_get_config('validate_server_certificate', $this->name)) {
66 phpCAS::setNoCasServerValidation();
73 * Display a form to input credentials
74 * @param unknown_type $params
77 function displayAuthForm(&$params) {
78 if (!$this->isRequired() && !$this->isSufficient()) {
81 $return_to = $params['return_to'];
88 $result .= _('Cookies must be enabled past this point.');
91 $result .= '<form action="' . util_make_url('/plugins/authcas/post-login.php') . '" method="get">
92 <input type="hidden" name="form_key" value="' . form_generate_key() . '"/>
93 <input type="hidden" name="return_to" value="' . htmlspecialchars(stripslashes($return_to)) . '" />
94 <p><input type="submit" name="login" value="' . _('Login via CAS') . '" />
98 $params['html_snippets'][$this->name] = $result;
100 $params['transparent_redirect_urls'][$this->name] = util_make_url('/plugins/authcas/post-login.php?return_to='.htmlspecialchars(stripslashes($return_to)).'&login=1');
104 * Is there a valid session?
105 * @param unknown_type $params
107 function checkAuthSession(&$params) {
110 $this->saved_user = NULL;
113 // FIXME: couldn't we just check parent::checkAuthSession() to take into account auth_token ? or I missed something
114 // if we already have a session/user active, use it
115 $user_id_from_cookie = $this->checkSessionCookie();
116 if ($user_id_from_cookie) {
117 $user = user_get_object($user_id_from_cookie);
118 $this->saved_user = $user;
119 $this->setSessionCookie();
120 } elseif (phpCAS::isAuthenticated()) {
121 // otherwise, use the CAS user
122 $user = $this->startSession(phpCAS::getUser());
125 // TODO : document this
127 if ($this->isSufficient()) {
128 $this->saved_user = $user;
129 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_ACCEPT;
132 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
135 if ($this->isRequired()) {
136 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_REJECT;
138 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
144 * What GFUser is logged in?
145 * @param unknown_type $params
147 function fetchAuthUser(&$params) {
148 if ($this->saved_user && $this->isSufficient()) {
149 $params['results'] = $this->saved_user;
153 function closeAuthSession($params) {
156 if ($this->isSufficient() || $this->isRequired()) {
157 $this->unsetSessionCookie();
158 // logs user out from CAS
159 // TODO : make it optional to not mess with other apps' SSO sessions with CAS
160 phpCAS::logoutWithRedirectService(util_make_url('/'));
167 * Terminate an authentication session
168 * @param unknown_type $params
171 protected function declareConfigVars() {
172 parent::declareConfigVars();
174 forge_define_config_item ('cas_server', $this->name, 'cas.example.com');
175 forge_define_config_item ('cas_port', $this->name, 443);
176 forge_define_config_item ('cas_version', $this->name, '2.0');
177 forge_define_config_item ('cas_context', $this->name, '/cas');
179 forge_define_config_item('validate_server_certificate', $this->name, 'no');
180 forge_set_config_item_bool('validate_server_certificate', $this->name);
187 // c-file-style: "bsd"