2012-08-26 17:16:08 +02:00
|
|
|
/**
|
2012-09-13 15:47:13 +02:00
|
|
|
* Copyright (c) 2010, Sebastian Sdorra
|
|
|
|
|
* All rights reserved.
|
2012-08-26 17:16:08 +02:00
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
2012-09-13 15:47:13 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of SCM-Manager; nor the names of its
|
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
|
* software without specific prior written permission.
|
2012-08-26 17:16:08 +02:00
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
2012-09-13 15:47:13 +02:00
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-08-26 17:16:08 +02:00
|
|
|
*
|
|
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2012-10-02 16:21:53 +02:00
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2012-10-02 16:21:53 +02:00
|
|
|
import com.google.common.base.Joiner;
|
2012-08-26 17:16:08 +02:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.common.collect.Sets;
|
2013-01-31 15:04:03 +01:00
|
|
|
import com.google.common.eventbus.Subscribe;
|
2012-08-26 17:37:01 +02:00
|
|
|
import com.google.inject.Inject;
|
2012-10-11 16:12:06 +02:00
|
|
|
import com.google.inject.Provider;
|
2012-10-02 16:16:13 +02:00
|
|
|
import com.google.inject.Singleton;
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
import org.apache.shiro.authc.AccountException;
|
|
|
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
|
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
|
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
2012-08-26 17:27:13 +02:00
|
|
|
import org.apache.shiro.authc.DisabledAccountException;
|
2012-08-26 17:16:08 +02:00
|
|
|
import org.apache.shiro.authc.SimpleAuthenticationInfo;
|
|
|
|
|
import org.apache.shiro.authc.UnknownAccountException;
|
2012-10-11 16:12:06 +02:00
|
|
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
2012-08-26 17:16:08 +02:00
|
|
|
import org.apache.shiro.authc.pam.UnsupportedTokenException;
|
|
|
|
|
import org.apache.shiro.authz.AuthorizationInfo;
|
|
|
|
|
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
|
|
|
|
import org.apache.shiro.realm.AuthorizingRealm;
|
|
|
|
|
import org.apache.shiro.subject.PrincipalCollection;
|
|
|
|
|
import org.apache.shiro.subject.SimplePrincipalCollection;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import sonia.scm.HandlerEvent;
|
|
|
|
|
import sonia.scm.cache.Cache;
|
|
|
|
|
import sonia.scm.cache.CacheManager;
|
2012-08-26 17:27:13 +02:00
|
|
|
import sonia.scm.config.ScmConfiguration;
|
2013-01-31 15:04:03 +01:00
|
|
|
import sonia.scm.event.Subscriber;
|
2012-08-26 17:27:13 +02:00
|
|
|
import sonia.scm.group.Group;
|
|
|
|
|
import sonia.scm.group.GroupManager;
|
2012-08-30 10:38:56 +02:00
|
|
|
import sonia.scm.group.GroupNames;
|
2012-08-26 17:16:08 +02:00
|
|
|
import sonia.scm.repository.Permission;
|
2012-08-29 10:18:15 +02:00
|
|
|
import sonia.scm.repository.PermissionType;
|
2012-08-26 17:16:08 +02:00
|
|
|
import sonia.scm.repository.Repository;
|
|
|
|
|
import sonia.scm.repository.RepositoryDAO;
|
2013-01-31 15:04:03 +01:00
|
|
|
import sonia.scm.repository.RepositoryEvent;
|
2012-08-26 17:16:08 +02:00
|
|
|
import sonia.scm.user.User;
|
2012-10-04 11:00:12 +02:00
|
|
|
import sonia.scm.user.UserDAO;
|
2013-01-31 15:04:03 +01:00
|
|
|
import sonia.scm.user.UserEvent;
|
2012-10-04 11:14:46 +02:00
|
|
|
import sonia.scm.user.UserEventHack;
|
2012-08-26 17:27:13 +02:00
|
|
|
import sonia.scm.user.UserException;
|
2012-08-26 17:16:08 +02:00
|
|
|
import sonia.scm.user.UserManager;
|
2012-08-26 17:27:13 +02:00
|
|
|
import sonia.scm.util.Util;
|
2012-08-26 17:16:08 +02:00
|
|
|
import sonia.scm.web.security.AuthenticationManager;
|
|
|
|
|
import sonia.scm.web.security.AuthenticationResult;
|
|
|
|
|
import sonia.scm.web.security.AuthenticationState;
|
|
|
|
|
|
|
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2012-10-11 16:12:06 +02:00
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2013-02-05 17:32:29 +01:00
|
|
|
import sonia.scm.repository.RepositoryManager;
|
2012-08-26 17:27:13 +02:00
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
2012-10-02 16:16:13 +02:00
|
|
|
@Singleton
|
2013-01-31 15:04:03 +01:00
|
|
|
@Subscriber
|
2012-08-26 17:16:08 +02:00
|
|
|
public class ScmRealm extends AuthorizingRealm
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
2012-08-30 16:34:43 +02:00
|
|
|
public static final String NAME = "scm";
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
/** Field description */
|
2012-08-30 16:34:43 +02:00
|
|
|
private static final String CACHE_NAME = "sonia.cache.authorizing";
|
2012-08-26 17:16:08 +02:00
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String SCM_CREDENTIALS = "SCM_CREDENTIALS";
|
|
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
/**
|
|
|
|
|
* the logger for ScmRealm
|
|
|
|
|
*/
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ScmRealm.class);
|
|
|
|
|
|
|
|
|
|
//~--- constructors ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs ...
|
|
|
|
|
*
|
|
|
|
|
*
|
2012-08-26 17:27:13 +02:00
|
|
|
*
|
|
|
|
|
* @param configuration
|
2012-08-26 17:16:08 +02:00
|
|
|
* @param cacheManager
|
|
|
|
|
* @param userManager
|
2012-08-26 17:27:13 +02:00
|
|
|
* @param groupManager
|
2012-08-26 17:16:08 +02:00
|
|
|
* @param repositoryDAO
|
2012-10-04 11:00:12 +02:00
|
|
|
* @param userDAO
|
2012-08-26 17:16:08 +02:00
|
|
|
* @param authenticator
|
2012-10-11 16:12:06 +02:00
|
|
|
* @param requestProvider
|
|
|
|
|
* @param responseProvider
|
2012-08-26 17:16:08 +02:00
|
|
|
*/
|
2012-08-26 17:37:01 +02:00
|
|
|
@Inject
|
2012-08-26 17:27:13 +02:00
|
|
|
public ScmRealm(ScmConfiguration configuration, CacheManager cacheManager,
|
|
|
|
|
UserManager userManager, GroupManager groupManager,
|
2013-01-31 16:37:49 +01:00
|
|
|
RepositoryDAO repositoryDAO, UserDAO userDAO,
|
|
|
|
|
AuthenticationManager authenticator,
|
2013-02-05 17:32:29 +01:00
|
|
|
RepositoryManager manager,
|
2012-10-11 16:12:06 +02:00
|
|
|
Provider<HttpServletRequest> requestProvider,
|
|
|
|
|
Provider<HttpServletResponse> responseProvider)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2012-08-26 17:27:13 +02:00
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.userManager = userManager;
|
2012-08-26 17:48:45 +02:00
|
|
|
this.groupManager = groupManager;
|
2012-08-26 17:16:08 +02:00
|
|
|
this.repositoryDAO = repositoryDAO;
|
2012-10-04 11:00:12 +02:00
|
|
|
this.userDAO = userDAO;
|
2012-08-26 17:16:08 +02:00
|
|
|
this.authenticator = authenticator;
|
2012-10-11 16:12:06 +02:00
|
|
|
this.requestProvider = requestProvider;
|
|
|
|
|
this.responseProvider = responseProvider;
|
2012-10-03 14:01:00 +02:00
|
|
|
|
|
|
|
|
// init cache
|
2012-08-26 17:16:08 +02:00
|
|
|
this.cache = cacheManager.getCache(String.class, AuthorizationInfo.class,
|
|
|
|
|
CACHE_NAME);
|
2012-10-03 14:01:00 +02:00
|
|
|
|
|
|
|
|
// set token class
|
2012-10-11 16:12:06 +02:00
|
|
|
setAuthenticationTokenClass(UsernamePasswordToken.class);
|
2012-10-03 14:01:00 +02:00
|
|
|
|
|
|
|
|
// use own custom caching
|
|
|
|
|
setCachingEnabled(false);
|
|
|
|
|
setAuthenticationCachingEnabled(false);
|
|
|
|
|
setAuthorizationCachingEnabled(false);
|
|
|
|
|
|
|
|
|
|
// set components
|
2012-08-26 17:16:08 +02:00
|
|
|
setPermissionResolver(new RepositoryPermissionResolver());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param event
|
|
|
|
|
*/
|
2013-01-31 15:04:03 +01:00
|
|
|
@Subscribe
|
|
|
|
|
public void onEvent(RepositoryEvent event)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2013-01-31 15:04:03 +01:00
|
|
|
if (event.getEventType().isPost())
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2012-08-27 08:00:16 +02:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("clear cache, because repository {} has changed",
|
2013-01-31 15:04:03 +01:00
|
|
|
event.getItem().getName());
|
2012-08-27 08:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache.clear();
|
|
|
|
|
}
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param event
|
|
|
|
|
*/
|
2013-01-31 15:04:03 +01:00
|
|
|
@Subscribe
|
|
|
|
|
public void onEvent(UserEvent event)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2013-01-31 15:04:03 +01:00
|
|
|
if (event.getEventType().isPost())
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2013-01-31 15:04:03 +01:00
|
|
|
User user = event.getItem();
|
|
|
|
|
|
2012-08-27 08:00:16 +02:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug(
|
|
|
|
|
"clear cache of user {}, because user properties have changed",
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache.remove(user.getId());
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param authToken
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws AuthenticationException
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected AuthenticationInfo doGetAuthenticationInfo(
|
|
|
|
|
AuthenticationToken authToken)
|
|
|
|
|
throws AuthenticationException
|
|
|
|
|
{
|
2012-10-11 16:12:06 +02:00
|
|
|
if (!(authToken instanceof UsernamePasswordToken))
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
throw new UnsupportedTokenException("ScmAuthenticationToken is required");
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 16:12:06 +02:00
|
|
|
UsernamePasswordToken token = (UsernamePasswordToken) authToken;
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
AuthenticationInfo info = null;
|
|
|
|
|
AuthenticationResult result =
|
2012-10-11 16:12:06 +02:00
|
|
|
authenticator.authenticate(requestProvider.get(), responseProvider.get(),
|
|
|
|
|
token.getUsername(), new String(token.getPassword()));
|
2012-08-26 17:16:08 +02:00
|
|
|
|
2012-08-28 18:34:24 +02:00
|
|
|
if ((result != null) && (AuthenticationState.SUCCESS == result.getState()))
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
info = createAuthenticationInfo(token, result);
|
|
|
|
|
}
|
2012-08-28 18:34:24 +02:00
|
|
|
else if ((result != null)
|
|
|
|
|
&& (AuthenticationState.NOT_FOUND == result.getState()))
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
throw new UnknownAccountException(
|
|
|
|
|
"unknown account ".concat(token.getUsername()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new AccountException("authentication failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param principals
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected AuthorizationInfo doGetAuthorizationInfo(
|
|
|
|
|
PrincipalCollection principals)
|
|
|
|
|
{
|
|
|
|
|
User user = principals.oneByType(User.class);
|
|
|
|
|
|
2012-10-02 16:27:17 +02:00
|
|
|
AuthorizationInfo info = cache.get(user.getId());
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
if (info == null)
|
|
|
|
|
{
|
|
|
|
|
if (logger.isTraceEnabled())
|
|
|
|
|
{
|
2012-08-30 13:26:45 +02:00
|
|
|
logger.trace("collect AuthorizationInfo for user {}", user.getName());
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-30 10:38:56 +02:00
|
|
|
GroupNames groups = principals.oneByType(GroupNames.class);
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
info = createAuthorizationInfo(user, groups);
|
2012-10-02 16:27:17 +02:00
|
|
|
cache.put(user.getId(), info);
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
2012-10-02 16:27:17 +02:00
|
|
|
else if (logger.isTraceEnabled())
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2012-10-02 16:27:17 +02:00
|
|
|
logger.trace("retrieve AuthorizationInfo for user {} from cache",
|
2012-08-26 17:16:08 +02:00
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
* @param password
|
|
|
|
|
* @param ar
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Set<String> authenticate(HttpServletRequest request, String password,
|
|
|
|
|
AuthenticationResult ar)
|
|
|
|
|
{
|
|
|
|
|
Set<String> groupSet = null;
|
|
|
|
|
User user = ar.getUser();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
groupSet = createGroupSet(ar);
|
|
|
|
|
|
|
|
|
|
// check for admin user
|
|
|
|
|
checkForAuthenticatedAdmin(user, groupSet);
|
|
|
|
|
|
|
|
|
|
// store user
|
2012-10-04 11:00:12 +02:00
|
|
|
User dbUser = userDAO.get(user.getName());
|
2012-08-26 17:27:13 +02:00
|
|
|
|
|
|
|
|
if (dbUser != null)
|
|
|
|
|
{
|
|
|
|
|
checkDBForAdmin(user, dbUser);
|
|
|
|
|
checkDBForActive(user, dbUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create new user
|
2012-10-04 11:18:10 +02:00
|
|
|
else if (user.isValid())
|
2012-08-26 17:27:13 +02:00
|
|
|
{
|
2012-10-04 11:00:12 +02:00
|
|
|
user.setCreationDate(System.currentTimeMillis());
|
2012-10-04 11:18:10 +02:00
|
|
|
|
2012-10-04 11:14:46 +02:00
|
|
|
// TODO find a better way
|
|
|
|
|
UserEventHack.fireEvent(userManager, user, HandlerEvent.BEFORE_CREATE);
|
2012-10-04 11:00:12 +02:00
|
|
|
userDAO.add(user);
|
2012-10-04 11:14:46 +02:00
|
|
|
UserEventHack.fireEvent(userManager, user, HandlerEvent.CREATE);
|
2012-08-26 17:27:13 +02:00
|
|
|
}
|
2012-10-04 11:18:10 +02:00
|
|
|
else if (logger.isErrorEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not create user {}, beacause it is not valid",
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
2012-08-26 17:27:13 +02:00
|
|
|
|
|
|
|
|
if (user.isActive())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logGroups(user, groupSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// store encrypted credentials in session
|
|
|
|
|
String credentials = user.getName();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
credentials = credentials.concat(":").concat(password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
credentials = CipherUtil.getInstance().encode(credentials);
|
|
|
|
|
request.getSession(true).setAttribute(SCM_CREDENTIALS, credentials);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
String msg = "user ".concat(user.getName()).concat(" is deactivated");
|
|
|
|
|
|
|
|
|
|
if (logger.isWarnEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.warn(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new DisabledAccountException(msg);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error("authentication failed", ex);
|
|
|
|
|
|
|
|
|
|
throw new AuthenticationException("authentication failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param dbUser
|
|
|
|
|
*/
|
|
|
|
|
private void checkDBForActive(User user, User dbUser)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// user is deactivated by database
|
|
|
|
|
if (!dbUser.isActive())
|
|
|
|
|
{
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("user {} is marked as deactivated by local database",
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.setActive(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param dbUser
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
private void checkDBForAdmin(User user, User dbUser)
|
|
|
|
|
throws UserException, IOException
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// if database user is an admin, set admin for the current user
|
|
|
|
|
if (dbUser.isAdmin())
|
|
|
|
|
{
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("user {} of type {} is marked as admin by local database",
|
|
|
|
|
user.getName(), user.getType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.setAdmin(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// modify existing user, copy properties except password and admin
|
|
|
|
|
if (user.copyProperties(dbUser, false))
|
|
|
|
|
{
|
|
|
|
|
userManager.modify(dbUser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groupSet
|
|
|
|
|
*/
|
|
|
|
|
private void checkForAuthenticatedAdmin(User user, Set<String> groupSet)
|
|
|
|
|
{
|
|
|
|
|
if (!user.isAdmin())
|
|
|
|
|
{
|
|
|
|
|
user.setAdmin(isAdmin(user, groupSet));
|
|
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled() && user.isAdmin())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("user {} is marked as admin by configuration",
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("authenticator {} marked user {} as admin", user.getType(),
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private List<org.apache.shiro.authz.Permission> collectRepositoryPermissions(
|
2012-08-30 16:34:43 +02:00
|
|
|
User user, GroupNames groups)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
List<org.apache.shiro.authz.Permission> permissions = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
for (Repository repository : repositoryDAO.getAll())
|
|
|
|
|
{
|
2012-08-29 10:12:44 +02:00
|
|
|
if (logger.isTraceEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.trace("collect permissions for repository {} and user {}",
|
|
|
|
|
repository.getName(), user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
collectRepositoryPermissions(permissions, repository, user, groups);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return permissions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param permissions
|
|
|
|
|
* @param repository
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
*/
|
|
|
|
|
private void collectRepositoryPermissions(
|
|
|
|
|
List<org.apache.shiro.authz.Permission> permissions, Repository repository,
|
2012-08-30 16:34:43 +02:00
|
|
|
User user, GroupNames groups)
|
2012-08-29 10:12:44 +02:00
|
|
|
{
|
|
|
|
|
List<Permission> repositoryPermissions = repository.getPermissions();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(repositoryPermissions))
|
|
|
|
|
{
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
for (Permission permission : repositoryPermissions)
|
|
|
|
|
{
|
2012-10-04 11:09:12 +02:00
|
|
|
if (isUserPermission(user, groups, permission))
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
2012-08-29 09:32:13 +02:00
|
|
|
RepositoryPermission rp = new RepositoryPermission(repository,
|
|
|
|
|
permission.getType());
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
if (logger.isTraceEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.trace("add repository permission {} for user {}", rp,
|
|
|
|
|
user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
permissions.add(rp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-29 10:12:44 +02:00
|
|
|
else if (logger.isTraceEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.trace("repository {} has not permission entries",
|
|
|
|
|
repository.getName());
|
|
|
|
|
}
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param token
|
|
|
|
|
* @param result
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private AuthenticationInfo createAuthenticationInfo(
|
2012-10-11 16:12:06 +02:00
|
|
|
UsernamePasswordToken token, AuthenticationResult result)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
User user = result.getUser();
|
2012-10-11 16:12:06 +02:00
|
|
|
Collection<String> groups = authenticate(requestProvider.get(),
|
|
|
|
|
new String(token.getPassword()), result);
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
SimplePrincipalCollection collection = new SimplePrincipalCollection();
|
|
|
|
|
|
2012-08-29 17:58:41 +02:00
|
|
|
/*
|
|
|
|
|
* the first (primary) principal should be a unique identifier
|
|
|
|
|
*/
|
|
|
|
|
collection.add(user.getId(), NAME);
|
2012-08-26 17:16:08 +02:00
|
|
|
collection.add(user, NAME);
|
2012-08-30 10:38:56 +02:00
|
|
|
collection.add(new GroupNames(groups), NAME);
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
return new SimpleAuthenticationInfo(collection, token.getPassword());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2012-08-30 10:38:56 +02:00
|
|
|
private AuthorizationInfo createAuthorizationInfo(User user,
|
|
|
|
|
GroupNames groups)
|
2012-08-26 17:16:08 +02:00
|
|
|
{
|
|
|
|
|
Set<String> roles = Sets.newHashSet();
|
2012-08-29 10:18:15 +02:00
|
|
|
List<org.apache.shiro.authz.Permission> permissions = null;
|
2012-08-26 17:16:08 +02:00
|
|
|
|
2012-08-29 10:21:58 +02:00
|
|
|
roles.add(Role.USER);
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
if (user.isAdmin())
|
|
|
|
|
{
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("grant admin role for user {}", user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 10:21:58 +02:00
|
|
|
roles.add(Role.ADMIN);
|
2012-08-29 10:18:15 +02:00
|
|
|
permissions = Lists.newArrayList();
|
2012-08-29 10:21:58 +02:00
|
|
|
permissions.add(new RepositoryPermission(RepositoryPermission.WILDCARD,
|
|
|
|
|
PermissionType.OWNER));
|
2012-08-29 10:18:15 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-30 16:34:43 +02:00
|
|
|
permissions = collectRepositoryPermissions(user, groups);
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
|
|
|
|
|
|
2012-08-29 10:18:15 +02:00
|
|
|
info.addObjectPermissions(permissions);
|
2012-08-26 17:16:08 +02:00
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
2012-08-26 17:27:13 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param ar
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Set<String> createGroupSet(AuthenticationResult ar)
|
|
|
|
|
{
|
|
|
|
|
Set<String> groupSet = Sets.newHashSet();
|
|
|
|
|
|
|
|
|
|
// load external groups
|
|
|
|
|
Collection<String> extGroups = ar.getGroups();
|
|
|
|
|
|
|
|
|
|
if (extGroups != null)
|
|
|
|
|
{
|
|
|
|
|
groupSet.addAll(extGroups);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load internal groups
|
|
|
|
|
loadGroups(ar.getUser(), groupSet);
|
|
|
|
|
|
|
|
|
|
return groupSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groupSet
|
|
|
|
|
*/
|
|
|
|
|
private void loadGroups(User user, Set<String> groupSet)
|
|
|
|
|
{
|
|
|
|
|
Collection<Group> groupCollection =
|
|
|
|
|
groupManager.getGroupsForMember(user.getName());
|
|
|
|
|
|
|
|
|
|
if (groupCollection != null)
|
|
|
|
|
{
|
|
|
|
|
for (Group group : groupCollection)
|
|
|
|
|
{
|
|
|
|
|
groupSet.add(group.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
*/
|
|
|
|
|
private void logGroups(User user, Set<String> groups)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder msg = new StringBuilder("user ");
|
|
|
|
|
|
|
|
|
|
msg.append(user.getName());
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(groups))
|
|
|
|
|
{
|
|
|
|
|
msg.append(" is member of ");
|
|
|
|
|
|
2012-10-02 16:21:53 +02:00
|
|
|
Joiner.on(", ").appendTo(msg, groups);
|
2012-08-26 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
msg.append(" is not a member of a group");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.debug(msg.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- get methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private boolean isAdmin(User user, Collection<String> groups)
|
|
|
|
|
{
|
|
|
|
|
boolean result = false;
|
|
|
|
|
Set<String> adminUsers = configuration.getAdminUsers();
|
|
|
|
|
|
|
|
|
|
if (adminUsers != null)
|
|
|
|
|
{
|
|
|
|
|
result = adminUsers.contains(user.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
Set<String> adminGroups = configuration.getAdminGroups();
|
|
|
|
|
|
|
|
|
|
if (adminGroups != null)
|
|
|
|
|
{
|
|
|
|
|
result = Util.containsOne(adminGroups, groups);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2012-08-26 17:16:08 +02:00
|
|
|
|
2012-10-04 11:09:12 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param groups
|
|
|
|
|
* @param perm
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private boolean isUserPermission(User user, GroupNames groups,
|
|
|
|
|
Permission perm)
|
|
|
|
|
{
|
|
|
|
|
//J-
|
|
|
|
|
return (perm.isGroupPermission() && groups.contains(perm.getName()))
|
|
|
|
|
|| ((!perm.isGroupPermission()) && user.getName().equals(perm.getName()));
|
|
|
|
|
//J+
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private AuthenticationManager authenticator;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private Cache<String, AuthorizationInfo> cache;
|
|
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private ScmConfiguration configuration;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private GroupManager groupManager;
|
|
|
|
|
|
2012-08-26 17:16:08 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private RepositoryDAO repositoryDAO;
|
2012-08-26 17:27:13 +02:00
|
|
|
|
2012-10-11 16:12:06 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private Provider<HttpServletRequest> requestProvider;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private Provider<HttpServletResponse> responseProvider;
|
|
|
|
|
|
2012-10-04 11:00:12 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private UserDAO userDAO;
|
|
|
|
|
|
2012-08-26 17:27:13 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private UserManager userManager;
|
2012-08-26 17:16:08 +02:00
|
|
|
}
|