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.jaas.spi;
20  
21  import java.security.Principal;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import javax.security.auth.Subject;
29  import javax.security.auth.callback.CallbackHandler;
30  
31  import org.eclipse.jetty.security.PropertyUserStore;
32  import org.eclipse.jetty.server.UserIdentity;
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
35  import org.eclipse.jetty.util.security.Credential;
36  
37  /**
38   * PropertyFileLoginModule
39   *
40   *
41   */
42  public class PropertyFileLoginModule extends AbstractLoginModule
43  {
44      public static final String DEFAULT_FILENAME = "realm.properties";
45  
46      private static final Logger LOG = Log.getLogger(PropertyFileLoginModule.class);
47  
48      private static ConcurrentHashMap<String, PropertyUserStore> _propertyUserStores = new ConcurrentHashMap<String, PropertyUserStore>();
49  
50      private int _refreshInterval = 0;
51      private String _filename = DEFAULT_FILENAME;
52  
53      /**
54       * Read contents of the configured property file.
55       *
56       * @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map,
57       *      java.util.Map)
58       * @param subject
59       * @param callbackHandler
60       * @param sharedState
61       * @param options
62       */
63      public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options)
64      {
65          super.initialize(subject,callbackHandler,sharedState,options);
66          setupPropertyUserStore(options);
67      }
68  
69      private void setupPropertyUserStore(Map<String, ?> options)
70      {
71          parseConfig(options);
72  
73          if (_propertyUserStores.get(_filename) == null)
74          {
75              PropertyUserStore propertyUserStore = new PropertyUserStore();
76              propertyUserStore.setConfig(_filename);
77              propertyUserStore.setRefreshInterval(_refreshInterval);
78  
79              PropertyUserStore prev = _propertyUserStores.putIfAbsent(_filename, propertyUserStore);
80              if (prev == null)
81              {
82                  LOG.debug("setupPropertyUserStore: Starting new PropertyUserStore. PropertiesFile: " + _filename + " refreshInterval: " + _refreshInterval);
83  
84                  try
85                  {
86                      propertyUserStore.start();
87                  }
88                  catch (Exception e)
89                  {
90                      LOG.warn("Exception while starting propertyUserStore: ",e);
91                  }
92              }
93          }
94      }
95  
96      private void parseConfig(Map<String, ?> options)
97      {
98          String tmp = (String)options.get("file");
99          _filename = (tmp == null? DEFAULT_FILENAME : tmp);
100         tmp = (String)options.get("refreshInterval");
101         _refreshInterval = (tmp == null?_refreshInterval:Integer.parseInt(tmp));
102     }
103 
104     /**
105      * Don't implement this as we want to pre-fetch all of the users.
106      *
107      * @param userName
108      * @throws Exception
109      */
110     public UserInfo getUserInfo(String userName) throws Exception
111     {
112         PropertyUserStore propertyUserStore = _propertyUserStores.get(_filename);
113         if (propertyUserStore == null)
114             throw new IllegalStateException("PropertyUserStore should never be null here!");
115         
116         LOG.debug("Checking PropertyUserStore "+_filename+" for "+userName);
117         UserIdentity userIdentity = propertyUserStore.getUserIdentity(userName);
118         if (userIdentity==null)
119             return null;
120 
121         Set<Principal> principals = userIdentity.getSubject().getPrincipals();
122 
123         List<String> roles = new ArrayList<String>();
124 
125         for ( Principal principal : principals )
126         {
127             roles.add( principal.getName() );
128         }
129 
130         Credential credential = (Credential)userIdentity.getSubject().getPrivateCredentials().iterator().next();
131         LOG.debug("Found: " + userName + " in PropertyUserStore "+_filename);
132         return new UserInfo(userName, credential, roles);
133     }
134 
135 }