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';
24 require_once 'CAS.php';
26 class AuthCASPlugin extends ForgeAuthPlugin {
27 function AuthCASPlugin () {
29 $this->ForgeAuthPlugin() ;
30 $this->name = "authcas";
31 $this->text = "CAS authentication";
33 $this->_addHook('display_auth_form');
34 $this->_addHook("check_auth_session");
35 $this->_addHook("fetch_authenticated_user");
36 $this->_addHook("close_auth_session");
38 $this->saved_login = '';
39 $this->saved_user = NULL;
41 $this->cookie_name = 'forge_session_cas';
43 $this->declareConfigVars();
46 private static $init = false;
53 phpCAS::client(forge_get_config('cas_version', $this->name),
54 forge_get_config('cas_server', $this->name),
55 intval(forge_get_config('cas_port', $this->name)),
57 if (forge_get_config('validate_server_certificate', $this->name)) {
60 phpCAS::setNoCasServerValidation();
66 function displayAuthForm($params) {
67 if (!$this->isRequired() && !$this->isSufficient()) {
70 $return_to = $params['return_to'];
74 echo '<h2>'._('CAS authentication').'</h2>';
76 echo '<form action="' . util_make_url('/plugins/authcas/post-login.php') . '" method="get">
77 <input type="hidden" name="form_key" value="' . form_generate_key() . '"/>
78 <input type="hidden" name="return_to" value="' . htmlspecialchars(stripslashes($return_to)) . '" />
79 <p><input type="submit" name="login" value="' . _('Login via CAS') . '" />
84 function checkAuthSession(&$params) {
87 $this->saved_user = NULL;
90 $user_id_from_cookie = $this->checkSessionCookie();
91 if ($user_id_from_cookie) {
92 $user = user_get_object($user_id_from_cookie);
93 $this->login($user->getUnixName());
94 } elseif (phpCAS::isAuthenticated()) {
95 $this->login(phpCAS::getUser());
99 if ($this->isSufficient()) {
100 $this->saved_user = $user;
101 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_ACCEPT;
104 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
107 if ($this->isRequired()) {
108 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_REJECT;
110 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
115 function fetchAuthUser(&$params) {
116 if ($this->saved_user && $this->isSufficient()) {
117 $params['results'] = $this->saved_user;
121 function closeAuthSession($params) {
124 if ($this->isSufficient() || $this->isRequired()) {
125 $this->unsetSessionCookie();
126 phpCAS::logoutWithRedirectService(util_make_url('/'));
132 protected function declareConfigVars() {
133 parent::declareConfigVars();
135 forge_define_config_item ('cas_server', $this->name, 'cas.example.com');
136 forge_define_config_item ('cas_port', $this->name, 443);
137 forge_define_config_item ('cas_version', $this->name, '2.0');
139 forge_define_config_item('validate_server_certificate', $this->name, 'no');
140 forge_set_config_item_bool('validate_server_certificate', $this->name);
147 // c-file-style: "bsd"