View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.MgnlUserManager;
38  import info.magnolia.cms.security.SecuritySupport;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.cms.security.UserManager;
41  import info.magnolia.jaas.sp.AbstractLoginModule;
42  import info.magnolia.jaas.sp.UserAwareLoginModule;
43  
44  import java.io.Serializable;
45  import java.security.Principal;
46  import java.util.Collections;
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import javax.security.auth.login.AccountLockedException;
51  import javax.security.auth.login.AccountNotFoundException;
52  import javax.security.auth.login.FailedLoginException;
53  import javax.security.auth.login.LoginException;
54  
55  import org.apache.commons.codec.binary.Base64;
56  import org.apache.commons.lang3.StringUtils;
57  import org.apache.jackrabbit.core.security.UserPrincipal;
58  import org.apache.jackrabbit.core.security.principal.AdminPrincipal;
59  
60  /**
61   * Authentication module implementation using JCR to retrieve the users.
62   *
63   * @deprecated since 5.3.6 - use {@link info.magnolia.jaas.sp.jcr.JCRAuthenticationModule} instead
64   */
65  @Deprecated
66  // JR requires login module to be serializable!
67  public class MagnoliaAuthenticationModule extends AbstractLoginModule implements UserAwareLoginModule, Serializable {
68      protected User user;
69  
70      /**
71       * As silly as it seems this class sole purpose of existence is to implement Serializable required by JR itself.
72       */
73      public class MagnoliaJRAdminPrincipal extends AdminPrincipal implements Principal, Serializable {
74          public MagnoliaJRAdminPrincipal(String name) {
75              super(name);
76          }
77      }
78  
79      /**
80       * Checks is the credentials exist in the repository.
81       *
82       * @throws LoginException or specific subclasses (which will be handled further for user feedback)
83       */
84      @Override
85      public void validateUser() throws LoginException {
86          initUser();
87  
88          if (this.user == null) {
89              throw new AccountNotFoundException("User account " + this.name + " not found.");
90          }
91  
92          matchPassword();
93  
94          if (!this.user.isEnabled()) {
95              throw new AccountLockedException("User account " + this.name + " is locked.");
96          }
97  
98          if (!UserManager.ANONYMOUS_USER.equals(user.getName()) && !isAdmin()) {
99              // update last access date for all non anonymous users
100             getUserManager().updateLastAccessTimestamp(user);
101         }
102     }
103 
104     private UserManager getUserManager() {
105         // can't get the factory upfront and can't use IoC as this class is instantiated by JCR/JAAS before anything else is ready.
106         if (!"admin".equals(name)) {
107             log.debug("getting user manager for realm {}", realm.getName());
108         }
109         return SecuritySupport.Factory.getInstance().getUserManager(realm.getName());
110     }
111 
112 
113     protected void initUser() throws LoginException {
114         if (!"admin".equals(name)) {
115             log.debug("initializing user {}", name);
116         }
117         // we do not verify admin user, against our user base (which is in repo we are trying to access with this user), but we still check the password.
118         // This user has assigned no privileges, just dummy near empty user object (with name and pwd only).
119         if (isAdmin()) {
120             Map<String, String> props = new HashMap<String, String>();
121             //TODO: double check if we need to reset pwd here or if getAdminSession() is enough!!!
122             props.put(MgnlUserManager.PROPERTY_PASSWORD, new String(Base64.encodeBase64("admin".getBytes())));
123             MgnlUser user = new MgnlUser(name, null, Collections.EMPTY_LIST, Collections.EMPTY_LIST, props);
124             this.user = user;
125             // admin user is used by system and have no access to regular user stuff
126             return;
127         }
128 
129         long start = System.currentTimeMillis();
130         this.user = getUserManager().getUser(name);
131         if (!"admin".equals(name)) {
132             log.debug("initialized user {} in {}ms", name, (System.currentTimeMillis() - start));
133         }
134     }
135 
136     protected void matchPassword() throws LoginException {
137         String serverPassword = user.getPassword();
138 
139         if (StringUtils.isEmpty(serverPassword)) {
140             throw new FailedLoginException("we do not allow users with no password");
141         }
142 
143         if (!StringUtils.equals(serverPassword, new String(this.pswd))) {
144             throw new FailedLoginException("passwords do not match");
145         }
146     }
147 
148     /**
149      * Set user details.
150      */
151     @Override
152     public void setEntity() {
153         if (isAdmin()) {
154             // JR specific principal for repo only authentication. This part will be probably different per repo.
155             this.subject.getPrincipals().add(new MagnoliaJRAdminPrincipal(name));
156             // admin user is used by system and have no access to regular user stuff
157             return;
158         } else if ("superuser".equals(name)) {
159             // add admin principal also for our superuser to make sure system doesn't deny him any access
160             this.subject.getPrincipals().add(new MagnoliaJRAdminPrincipal(name));
161         } else {
162             // TODO: make JR dependency pluggable!!!
163             // and ordinary JR User principal for everybody else
164             this.subject.getPrincipals().add(new UserPrincipal(name));
165 
166         }
167 
168         // our user has our principal!!!!
169         this.subject.getPrincipals().add(this.user);
170         this.subject.getPrincipals().add(this.realm);
171 
172 
173         collectGroupNames();
174         collectRoleNames();
175     }
176 
177     private boolean isAdmin() {
178         return this.name != null && this.name.equals("admin");
179     }
180 
181     /**
182      * Set access control list from the user, roles and groups.
183      */
184     @Override
185     public void setACL() {
186     }
187 
188     /**
189      * Extract all the configured roles from the given node. (which can be the user node or a group node)
190      */
191     public void collectRoleNames() {
192         for (String role : this.user.getAllRoles()) {
193             addRoleName(role);
194         }
195     }
196 
197     /**
198      * Extract all the configured groups from the given node. (which can be the user node or a group node)
199      */
200     public void collectGroupNames() {
201         for (String group : this.user.getAllGroups()) {
202             addGroupName(group);
203         }
204     }
205 
206     @Override
207     public User getUser() {
208         return user;
209     }
210 }