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 protected $saved_login;
28 protected $saved_password;
29 protected $saved_user;
30 protected $saved_data;
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->cookie_name = 'forge_session_cas';
48 $this->declareConfigVars();
51 private static $init = false;
58 phpCAS::client(forge_get_config('cas_version', $this->name),
59 forge_get_config('cas_server', $this->name),
60 intval(forge_get_config('cas_port', $this->name)),
65 function displayAuthForm($params) {
66 if (!$this->isRequired() && !$this->isSufficient()) {
69 $return_to = $params['return_to'];
73 echo '<h2>'._('CAS authentication').'</h2>';
75 echo '<form action="' . util_make_url('/plugins/authcas/post-login.php') . '" method="get">
76 <input type="hidden" name="form_key" value="' . form_generate_key() . '"/>
77 <input type="hidden" name="return_to" value="' . htmlspecialchars(stripslashes($return_to)) . '" />
78 <p><input type="submit" name="login" value="' . _('Login via CAS') . '" />
83 function checkAuthSession(&$params) {
86 $this->saved_user = NULL;
89 $user_id_from_cookie = $this->checkSessionCookie();
90 if ($user_id_from_cookie) {
91 $user = user_get_object($user_id_from_cookie);
92 } elseif (phpCAS::isAuthenticated()) {
93 $user = user_get_object_by_name(phpCAS::getUser());
98 if ($this->isSufficient()) {
99 $this->saved_user = $user;
100 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_ACCEPT;
103 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
106 if ($this->isRequired()) {
107 $params['results'][$this->name] = FORGE_AUTH_AUTHORITATIVE_REJECT;
109 $params['results'][$this->name] = FORGE_AUTH_NOT_AUTHORITATIVE;
114 function fetchAuthUser(&$params) {
115 if ($this->saved_user && $this->isSufficient()) {
116 $params['results'] = $this->saved_user;
120 function closeAuthSession($params) {
123 if ($this->isSufficient() || $this->isRequired()) {
124 $this->unsetSessionCookie();
125 phpCAS::logoutWithRedirectService(util_make_url('/'));
131 protected function declareConfigVars() {
132 parent::declareConfigVars();
134 forge_define_config_item ('cas_server', $this->name, 'cas.example.com');
135 forge_define_config_item ('cas_port', $this->name, 443);
136 forge_define_config_item ('cas_version', $this->name, '2.0');
144 // c-file-style: "bsd"