1 package org.codehaus.redback.jsecurity;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.util.HashSet;
23 import java.util.Iterator;
26 import org.apache.archiva.redback.users.User;
27 import org.apache.archiva.redback.users.UserNotFoundException;
28 import org.codehaus.plexus.redback.policy.AccountLockedException;
29 import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
30 import org.codehaus.plexus.redback.rbac.Permission;
31 import org.codehaus.plexus.redback.rbac.RBACManager;
32 import org.codehaus.plexus.redback.rbac.RbacManagerException;
33 import org.codehaus.plexus.redback.rbac.UserAssignment;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.jsecurity.authc.AuthenticationException;
36 import org.jsecurity.authc.AuthenticationInfo;
37 import org.jsecurity.authc.AuthenticationToken;
38 import org.jsecurity.authc.SimpleAuthenticationInfo;
39 import org.jsecurity.authc.UsernamePasswordToken;
40 import org.jsecurity.authc.credential.CredentialsMatcher;
41 import org.jsecurity.authz.AuthorizationInfo;
42 import org.jsecurity.authz.SimpleAuthorizationInfo;
43 import org.jsecurity.realm.AuthorizingRealm;
44 import org.jsecurity.subject.PrincipalCollection;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class RedbackRealm extends AuthorizingRealm
50 private Logger log = LoggerFactory.getLogger(RedbackRealm.class);
52 private final UserManager userManager;
54 private final RBACManager rbacManager;
56 private final UserSecurityPolicy securityPolicy;
58 public RedbackRealm(UserManager userManager, RBACManager rbacManager, UserSecurityPolicy securityPolicy)
60 this.userManager = userManager;
61 this.rbacManager = rbacManager;
62 this.securityPolicy = securityPolicy;
66 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals)
68 final String username = (String) principals.fromRealm(getName()).iterator().next();
72 final UserAssignment assignment = rbacManager.getUserAssignment(username);
73 final Set<String> roleNames = new HashSet<String>(assignment.getRoleNames());
74 final Set<String> permissions = new HashSet<String>();
76 for (Iterator<Permission> it = rbacManager.getAssignedPermissions(username).iterator(); it.hasNext();)
78 Permission permission = it.next();
79 permissions.add(permission.getName());
82 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(roleNames);
83 authorizationInfo.setStringPermissions(permissions);
85 return authorizationInfo;
87 catch (RbacManagerException e)
89 log.error("Could not authenticate against data source", e);
96 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
97 throws AuthenticationException
101 throw new AuthenticationException("AuthenticationToken cannot be null");
104 final UsernamePasswordToken passwordToken = (UsernamePasswordToken)token;
109 user = userManager.findUser(passwordToken.getUsername());
111 catch (UserNotFoundException e)
113 log.error("Could not find user " + passwordToken.getUsername());
121 if ( user.isLocked() && !user.isPasswordChangeRequired() )
123 throw new PrincipalLockedException("User " + user.getPrincipal() + " is locked.");
126 if ( user.isPasswordChangeRequired() )
128 throw new PrincipalPasswordChangeRequiredException("Password change is required for user " + user.getPrincipal());
131 return new RedbackAuthenticationInfo(user, getName());
135 public CredentialsMatcher getCredentialsMatcher()
137 return new CredentialsMatcher()
139 public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
141 final String credentials = new String((char[])token.getCredentials());
142 final boolean match = securityPolicy.getPasswordEncoder().encodePassword(credentials).equals((String)info.getCredentials());
145 User user = ((RedbackAuthenticationInfo)info).getUser();
148 securityPolicy.extensionExcessiveLoginAttempts( user );
150 catch (AccountLockedException e)
152 log.info("User{} has been locked", user.getUsername(), e);
158 userManager.updateUser( user );
160 catch (UserNotFoundException e)
162 log.error("The user to be updated could not be found", e);
171 final class RedbackAuthenticationInfo extends SimpleAuthenticationInfo
173 private final User user;
175 public RedbackAuthenticationInfo(User user, String realmName)
177 super(user.getPrincipal(), user.getEncodedPassword(), realmName);
181 public User getUser()