View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.jaas.spi;
20  
21  import java.security.Principal;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.security.auth.Subject;
30  import javax.security.auth.callback.CallbackHandler;
31  
32  import org.eclipse.jetty.util.security.Credential;
33  import org.eclipse.jetty.security.PropertyUserStore;
34  import org.eclipse.jetty.server.UserIdentity;
35  import org.eclipse.jetty.util.log.Log;
36  import org.eclipse.jetty.util.log.Logger;
37  
38  /**
39   * PropertyFileLoginModule
40   * 
41   * 
42   */
43  public class PropertyFileLoginModule extends AbstractLoginModule
44  {
45      public static final String DEFAULT_FILENAME = "realm.properties";
46  
47      private static final Logger LOG = Log.getLogger(PropertyFileLoginModule.class);
48  
49      private static Map<String, PropertyUserStore> _propertyUserStores = new HashMap<String, PropertyUserStore>();
50  
51      private int _refreshInterval = 0;
52      private String _filename = DEFAULT_FILENAME;
53  
54      /**
55       * Read contents of the configured property file.
56       * 
57       * @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map,
58       *      java.util.Map)
59       * @param subject
60       * @param callbackHandler
61       * @param sharedState
62       * @param options
63       */
64      public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options)
65      {
66          super.initialize(subject,callbackHandler,sharedState,options);
67          setupPropertyUserStore(options);
68      }
69  
70      private void setupPropertyUserStore(Map<String, ?> options)
71      {
72          if (_propertyUserStores.get(_filename) == null)
73          {
74              parseConfig(options);
75  
76              PropertyUserStore _propertyUserStore = new PropertyUserStore();
77              _propertyUserStore.setConfig(_filename);
78              _propertyUserStore.setRefreshInterval(_refreshInterval);
79              LOG.debug("setupPropertyUserStore: Starting new PropertyUserStore. PropertiesFile: " + _filename + " refreshInterval: " + _refreshInterval);
80  
81              try
82              {
83                  _propertyUserStore.start();
84              }
85              catch (Exception e)
86              {
87                  LOG.warn("Exception while starting propertyUserStore: ",e);
88              }
89  
90              _propertyUserStores.put(_filename,_propertyUserStore);
91          }
92      }
93  
94      private void parseConfig(Map<String, ?> options)
95      {
96          _filename = (String)options.get("file") != null?(String)options.get("file"):DEFAULT_FILENAME;
97          String refreshIntervalString = (String)options.get("refreshInterval");
98          _refreshInterval = refreshIntervalString == null?_refreshInterval:Integer.parseInt(refreshIntervalString);
99      }
100 
101     /**
102      * Don't implement this as we want to pre-fetch all of the users.
103      * 
104      * @param userName
105      * @throws Exception
106      */
107     public UserInfo getUserInfo(String userName) throws Exception
108     {
109         PropertyUserStore propertyUserStore = _propertyUserStores.get(_filename);
110         if (propertyUserStore == null)
111             throw new IllegalStateException("PropertyUserStore should never be null here!");
112         
113         UserIdentity userIdentity = propertyUserStore.getUserIdentity(userName);
114         if(userIdentity==null)
115             return null;
116         
117         Set<Principal> principals = userIdentity.getSubject().getPrincipals();
118         
119         List<String> roles = new ArrayList<String>();
120         
121         for ( Principal principal : principals )
122         {
123             roles.add( principal.getName() );
124         }
125         
126         Credential credential = (Credential)userIdentity.getSubject().getPrivateCredentials().iterator().next();
127         LOG.debug("Found: " + userName + " in PropertyUserStore");
128         return new UserInfo(userName, credential, roles);
129     }
130 
131 }