/* * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package com.xpn.xwiki.user.impl.xwiki; import java.text.MessageFormat; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.user.api.XWikiUser; /** * Implements a authentication mechanism which is trusting the App Server authentication. If it fails it falls back to * the standard XWiki authentication */ public class AppServerTrustedKerberosAuthServiceImpl extends XWikiAuthServiceImpl { private static final Log LOG = LogFactory.getLog(AppServerTrustedKerberosAuthServiceImpl.class); /** * @see com.xpn.xwiki.user.impl.xwiki.XWikiAuthServiceImpl#checkAuth(com.xpn.xwiki.XWikiContext) */ @Override public XWikiUser checkAuth(XWikiContext context) throws XWikiException { String user = context.getRequest().getRemoteUser(); LOG.debug(MessageFormat.format("Checking auth for remote user {0}", user)); if ((user == null) || user.equals("")) return super.checkAuth(context); else { if (user.contains("\\")) if (user.lastIndexOf("\\") + 1 < user.length()) user = user.substring(user.lastIndexOf("\\") + 1); else { user = ""; return super.checkAuth(context); } if (user.contains("@")) { user = StringUtils.substringBeforeLast(user, "@"); } LOG.debug("Launching create user for " + user); user = createUser(user, context); LOG.debug("Create user done for " + user); user = "XWiki." + user; } context.setUser(user); return new XWikiUser(user); } /** * @see com.xpn.xwiki.user.impl.xwiki.XWikiAuthServiceImpl#checkAuth(java.lang.String, java.lang.String, * java.lang.String, com.xpn.xwiki.XWikiContext) */ @Override public XWikiUser checkAuth(String username, String password, String rememberme, XWikiContext context) throws XWikiException { String user = context.getRequest().getRemoteUser(); LOG.debug(MessageFormat.format("Checking auth for remote user {0}", user)); if ((user == null) || user.equals("")) { return super.checkAuth(username, password, rememberme, context); } else { if (user.contains("\\")) user = StringUtils.substringBeforeLast(user, "\\"); else { user = ""; return super.checkAuth(username, password, rememberme, context); } if (user.contains("@")) { user = StringUtils.substringBeforeLast(user, "@"); } user = createUser(user, context); user = "XWiki." + user; } context.setUser(user); return new XWikiUser(user); } }