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;
35  
36  
37  import info.magnolia.cms.security.Realm;
38  import info.magnolia.cms.security.User;
39  import info.magnolia.cms.security.auth.callback.RealmCallback;
40  import info.magnolia.cms.security.auth.callback.UserCallback;
41  import info.magnolia.cms.util.BooleanUtil;
42  
43  import java.io.IOException;
44  import java.util.LinkedHashSet;
45  import java.util.Map;
46  import java.util.Set;
47  
48  import javax.security.auth.Subject;
49  import javax.security.auth.callback.Callback;
50  import javax.security.auth.callback.CallbackHandler;
51  import javax.security.auth.callback.NameCallback;
52  import javax.security.auth.callback.PasswordCallback;
53  import javax.security.auth.callback.UnsupportedCallbackException;
54  import javax.security.auth.login.LoginException;
55  import javax.security.auth.spi.LoginModule;
56  
57  import org.apache.commons.lang.ArrayUtils;
58  import org.apache.commons.lang.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  
63  /**
64   * @author Sameer Charles
65   * $Id: AbstractLoginModule.java 32667 2010-03-13 00:37:06Z gjoseph $
66   */
67  public abstract class AbstractLoginModule implements LoginModule {
68  
69      // magnolia specific option to define if "this" module needs to be
70      // skipped based on previous (in JAAS module chain) module status
71      public static final String OPTION_SKIP_ON_PREVIOUS_SUCCESS = "skip_on_previous_success";
72  
73      public static final String OPTION_REALM = "realm";
74  
75      public static final String OPTION_USE_REALM_CALLBACK= "use_realm_callback";
76  
77      public static final String STATUS = "statusValue";
78  
79      public static final int STATUS_SUCCEEDED = 1;
80  
81      /**
82       * @deprecated use STATUS_SUCCEEDED
83       */
84      public static final int STATUS_SUCCEDED = STATUS_SUCCEEDED;
85  
86      public static final int STATUS_FAILED = 2;
87  
88      public static final int STATUS_SKIPPED = 3;
89  
90      public static final int STATUS_UNAVAILABLE = 4;
91  
92      /**
93       * todo implement the following commonly supported flags to allow single signon with third party modules
94       * */
95  
96      //If true, the first LoginModule in the stack saves the password entered,
97      // and subsequent LoginModules also try to use it. If authentication fails,
98      // the LoginModules prompt for a new password and retry the authentication.
99      public static final String TRY_FIRST_PASS = "try_first_pass";
100 
101     //If true, the first LoginModule in the stack saves the password entered,
102     // and subsequent LoginModules also try to use it.
103     // LoginModules do not prompt for a new password if authentication fails (authentication simply fails).
104     public static final String USE_FIRST_PASS = "use_first_pass";
105 
106     //If true, the first LoginModule in the stack saves the password entered,
107     // and subsequent LoginModules attempt to map it into their service-specific password.
108     // If authentication fails, the LoginModules prompt for a new password and retry the authentication.
109     public static final String TRY_MAPPED_PASS = "try_mapped_pass";
110 
111     //If true, the first LoginModule in the stack saves the password entered,
112     // and subsequent LoginModules attempt to map it into their service-specific password.
113     // LoginModules do not prompt for a new password if authentication fails (authentication simply fails).
114     public static final String USE_MAPPED_PASS = "use_mapped_pass";
115 
116     public Subject subject;
117 
118     public CallbackHandler callbackHandler;
119 
120     public Map sharedState;
121 
122     public Map options;
123 
124     public String name;
125 
126     public char[] pswd;
127 
128     /**
129      * The realm we login into. Initialized by the option realm.
130      */
131     protected String realm = Realm.REALM_ALL;
132 
133     /**
134      * Allow the client to define the realm he logs into. Default value is false
135      */
136     protected boolean useRealmCallback;
137 
138     // this status is sent back to the LoginModule chain
139     public boolean success;
140 
141     protected Logger log = LoggerFactory.getLogger(getClass());
142 
143     private boolean skipOnPreviousSuccess;
144 
145 
146     /**
147      *
148      */
149     public AbstractLoginModule() {
150 
151     }
152 
153     public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
154         this.subject = subject;
155         this.callbackHandler = callbackHandler;
156         this.sharedState = sharedState;
157         this.options = options;
158         // don't overwrite group and roles set in the shared state
159         if (this.sharedState.get("groupNames") == null)
160         {
161             this.sharedState.put("groupNames", new LinkedHashSet());
162         }
163         if (this.sharedState.get("roleNames") == null)
164         {
165             this.sharedState.put("roleNames", new LinkedHashSet());
166         }
167         this.realm = StringUtils.defaultIfEmpty((String) options.get(OPTION_REALM), Realm.DEFAULT_REALM);
168 
169         this.useRealmCallback = BooleanUtil.toBoolean((String) options.get(OPTION_USE_REALM_CALLBACK), true);
170         this.skipOnPreviousSuccess = BooleanUtil.toBoolean((String) options.get(OPTION_SKIP_ON_PREVIOUS_SUCCESS), false);
171     }
172 
173     public boolean login() throws LoginException {
174         if (this.getSkip()) {
175             return true;
176         }
177 
178         if (this.callbackHandler == null) {
179             throw new LoginException("Error: no CallbackHandler available");
180         }
181 
182         Callback[] callbacks = new Callback[2];
183         callbacks[0] = new NameCallback("name");
184         callbacks[1] = new PasswordCallback("pswd", false);
185 
186         // if the realm is not defined in the jaas configuration
187         // we ask use a callback to get the value
188         if(this.useRealmCallback){
189             callbacks = (Callback[]) ArrayUtils.add(callbacks, new RealmCallback());
190         }
191 
192         this.success = false;
193         try {
194             this.callbackHandler.handle(callbacks);
195             this.name = ((NameCallback) callbacks[0]).getName();
196             this.pswd = ((PasswordCallback) callbacks[1]).getPassword();
197             if(this.useRealmCallback){
198                 this.realm = StringUtils.defaultIfEmpty(((RealmCallback)callbacks[2]).getRealm(), this.realm);
199             }
200 
201             this.validateUser();
202 
203             // FIXME we do that only to be compatible to the old way jaas modules were written
204             if(this instanceof UserAwareLoginModule){
205                 User user = ((UserAwareLoginModule)this).getUser();
206                 if(user != null){
207                     this.callbackHandler.handle(new Callback[]{new UserCallback(user)});
208                 }
209             }
210         } catch (IOException ioe) {
211             if (log.isDebugEnabled()) {
212                 log.debug("Exception caught", ioe);
213             }
214             throw new LoginException(ioe.toString());
215         } catch (UnsupportedCallbackException ce) {
216             if (log.isDebugEnabled()) {
217                 log.debug(ce.getMessage(), ce);
218             }
219             throw new LoginException(ce.getCallback().toString() + " not available");
220         }
221 
222         this.success = true;
223 
224         this.setSharedStatus(STATUS_SUCCEEDED);
225         return this.success;
226     }
227 
228 
229     /**
230      * Update subject with ACL and other properties
231      */
232     public boolean commit() throws LoginException {
233         /**
234          * If login module failed to authenticate then this method should simply return false
235          * instead of throwing an exception - refer to specs for more details
236          * */
237         if (!this.success) {
238             return false;
239         }
240         this.setEntity();
241         this.setACL();
242         return true;
243     }
244 
245     public boolean abort() throws LoginException {
246         return this.release();
247     }
248 
249     public boolean logout() throws LoginException {
250         return this.release();
251     }
252 
253     /**
254      * @return shared status value as set by this LoginModule
255      * */
256     public int getSharedStatus() {
257         Integer status = (Integer) this.sharedState.get(STATUS);
258         if (null != status) {
259             return status.intValue();
260         }
261         return STATUS_UNAVAILABLE;
262     }
263 
264     /**
265      * Set shared status value to be used by subsequent LoginModule(s)
266      * */
267     public void setSharedStatus(int status) {
268         this.sharedState.put(STATUS, new Integer(status));
269     }
270 
271     /**
272      * test if the option skip_on_previous_success is set to true
273      * and preceding LoginModule was successful
274      * */
275     protected boolean getSkip() {
276         return skipOnPreviousSuccess && this.getSharedStatus() == STATUS_SUCCEEDED;
277     }
278 
279     public void setGroupNames(Set names) {
280         this.getGroupNames().addAll(names);
281     }
282 
283     public void addGroupName(String groupName) {
284         getGroupNames().add(groupName);
285     }
286 
287     public Set getGroupNames() {
288         return (Set) this.sharedState.get("groupNames");
289     }
290 
291     public void setRoleNames(Set names) {
292         this.getRoleNames().addAll(names);
293     }
294 
295     public void addRoleName(String roleName) {
296         getRoleNames().add(roleName);
297     }
298 
299     public Set getRoleNames() {
300         return (Set) this.sharedState.get("roleNames");
301     }
302 
303     /**
304      * Releases all associated memory
305      */
306     public abstract boolean release();
307 
308     /**
309      * checks if the credentials exist in the repository
310      * @throws LoginException or specific subclasses to report failures.
311      */
312     public abstract void validateUser() throws LoginException;
313 
314     /**
315      * set user details
316      */
317     public abstract void setEntity();
318 
319     /**
320      * set access control list from the user, roles and groups
321      */
322     public abstract void setACL();
323 
324 }