View Javadoc

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