View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.MgnlUser;
37  import info.magnolia.cms.security.SecuritySupport;
38  import info.magnolia.cms.security.User;
39  import info.magnolia.cms.security.UserManager;
40  import info.magnolia.cms.security.auth.Entity;
41  import info.magnolia.jaas.principal.EntityImpl;
42  import info.magnolia.jaas.sp.AbstractLoginModule;
43  import info.magnolia.jaas.sp.UserAwareLoginModule;
44  import org.apache.commons.lang.StringUtils;
45  
46  import javax.security.auth.login.FailedLoginException;
47  import javax.security.auth.login.LoginException;
48  import javax.security.auth.login.AccountNotFoundException;
49  import javax.security.auth.login.AccountLockedException;
50  import java.util.Iterator;
51  
52  
53  /**
54   * @author Sameer Charles $Id: JCRAuthenticationModule.java 32667 2010-03-13 00:37:06Z gjoseph $
55   */
56  public class JCRAuthenticationModule extends AbstractLoginModule implements UserAwareLoginModule {
57      protected User user;
58  
59      /**
60       * Releases all associated memory.
61       */
62      public boolean release() {
63          return true;
64      }
65  
66      /**
67       * Checks is the credentials exist in the repository.
68       * @throws LoginException or specific subclasses (which will be handled further for user feedback)
69       */
70      public void validateUser() throws LoginException {
71          initUser();
72  
73          if (this.user == null) {
74              throw new AccountNotFoundException("User account " + this.name + " not found.");
75          }
76  
77          matchPassword();
78  
79          if (!this.user.isEnabled()) {
80              throw new AccountLockedException("User account " + this.name + " is locked.");
81          }
82  
83          if (!UserManager.ANONYMOUS_USER.equals(user.getName()) && user instanceof MgnlUser)
84          {
85              ((MgnlUser) user).setLastAccess();
86          }
87      }
88  
89      protected void initUser() {
90          user = getUserManager().getUser(name);
91      }
92  
93      protected void matchPassword() throws LoginException {
94          String serverPassword = user.getPassword();
95  
96          if (StringUtils.isEmpty(serverPassword)) {
97              throw new FailedLoginException("we do not allow users with no password");
98          }
99  
100         if (!StringUtils.equals(serverPassword, new String(this.pswd))) {
101             throw new FailedLoginException("passwords do not match");
102         }
103     }
104 
105     /**
106      * Override this to support any configured/non-configured user manager.
107      */
108     public UserManager getUserManager() {
109         SecuritySupport securitySupport = SecuritySupport.Factory.getInstance();
110         return securitySupport.getUserManager(this.realm);
111     }
112 
113     /**
114      * Set user details.
115      */
116     public void setEntity() {
117         EntityImpl entity = new EntityImpl();
118         entity.addProperty(Entity.LANGUAGE, this.user.getLanguage());
119         entity.addProperty(Entity.NAME, this.user.getName());
120 
121         String fullName = this.user.getProperty("title");
122         if(fullName != null){
123             entity.addProperty(Entity.FULL_NAME, fullName);
124         }
125         entity.addProperty(Entity.PASSWORD, new String(this.pswd));
126         this.subject.getPrincipals().add(entity);
127 
128         collectGroupNames();
129         collectRoleNames();
130     }
131 
132     /**
133      * Set access control list from the user, roles and groups.
134      */
135     public void setACL() {
136     }
137 
138     /**
139      * Extract all the configured roles from the given node. (which can be the user node or a group node)
140      */
141     public void collectRoleNames() {
142         for (Iterator iter = this.user.getAllRoles().iterator(); iter.hasNext();) {
143             addRoleName((String)iter.next());
144         }
145     }
146 
147     /**
148      * Extract all the configured groups from the given node. (which can be the user node or a group node)
149      */
150     public void collectGroupNames() {
151         for (Iterator iter = this.user.getAllGroups().iterator(); iter.hasNext();) {
152             addGroupName((String) iter.next());
153         }
154     }
155 
156     public User getUser() {
157         return user;
158     }
159 
160 }