View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.jaas.sp.jcr;
35  
36  import info.magnolia.cms.security.SecuritySupport;
37  import info.magnolia.cms.security.User;
38  import info.magnolia.cms.security.UserManager;
39  import info.magnolia.jaas.sp.AbstractLoginModule;
40  import info.magnolia.jaas.sp.UserAwareLoginModule;
41  
42  import java.io.Serializable;
43  
44  import javax.security.auth.login.AccountLockedException;
45  import javax.security.auth.login.AccountNotFoundException;
46  import javax.security.auth.login.FailedLoginException;
47  import javax.security.auth.login.LoginException;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  /**
52   * Authentication module implementation using JCR to retrieve the users.
53   * @author Sameer Charles $Id: JCRAuthenticationModule.java 50211 2011-10-20 10:33:41Z tmattsson $
54   */
55  public class JCRAuthenticationModule extends AbstractLoginModule implements UserAwareLoginModule, Serializable {
56  
57      protected User user;
58  
59  
60      /**
61       * Checks is the credentials exist in the repository.
62       * @throws LoginException or specific subclasses (which will be handled further for user feedback)
63       */
64      @Override
65      public void validateUser() throws LoginException {
66          initUser();
67  
68          if (this.user == null) {
69              throw new AccountNotFoundException("User account " + this.name + " not found.");
70          }
71  
72          matchPassword();
73  
74          if (!this.user.isEnabled()) {
75              throw new AccountLockedException("User account " + this.name + " is locked.");
76          }
77  
78          if (!UserManager.ANONYMOUS_USER.equals(user.getName())) {
79              // update last access date for all non anonymous users
80              getUserManager().updateLastAccessTimestamp(user);
81          }
82      }
83  
84      private UserManager getUserManager() {
85          // can't get the factory upfront and can't use IoC as this class is instantiated by JCR/JAAS before anything else is ready.
86          log.debug("getting user manager for realm " + realm.getName());
87          return SecuritySupport.Factory.getInstance().getUserManager(realm.getName());
88      }
89  
90  
91  
92      protected void initUser() throws LoginException {
93          log.debug("initializing user {}", name);
94  
95          long start = System.currentTimeMillis();
96          this.user = getUserManager().getUser(name);
97          log.debug("initialized user {} in {}ms", name, (System.currentTimeMillis() - start));
98      }
99  
100     protected void matchPassword() throws LoginException {
101         String serverPassword = user.getPassword();
102 
103         if (StringUtils.isEmpty(serverPassword)) {
104             throw new FailedLoginException("we do not allow users with no password");
105         }
106 
107         if (!StringUtils.equals(serverPassword, new String(this.pswd))) {
108             throw new FailedLoginException("passwords do not match");
109         }
110     }
111 
112     /**
113      * Set user details.
114      */
115     @Override
116     public void setEntity() {
117 
118         this.subject.getPrincipals().add(this.user);
119         this.subject.getPrincipals().add(this.realm);
120 
121         collectGroupNames();
122         collectRoleNames();
123     }
124 
125     /**
126      * Set access control list from the user, roles and groups.
127      */
128     @Override
129     public void setACL() {
130     }
131 
132     /**
133      * Extract all the configured roles from the given node. (which can be the user node or a group node)
134      */
135     public void collectRoleNames() {
136         for (String role : this.user.getAllRoles()) {
137             addRoleName(role);
138         }
139     }
140 
141     /**
142      * Extract all the configured groups from the given node. (which can be the user node or a group node)
143      */
144     public void collectGroupNames() {
145         for (String group : this.user.getAllGroups()) {
146             addGroupName(group);
147         }
148     }
149 
150     @Override
151     public User getUser() {
152         return user;
153     }
154 }