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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
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 Map<String, PropertyUserStore> _propertyUserStores = new HashMap<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          if (_propertyUserStores.get(_filename) == null)
72          {
73              parseConfig(options);
74  
75              PropertyUserStore _propertyUserStore = new PropertyUserStore();
76              _propertyUserStore.setConfig(_filename);
77              _propertyUserStore.setRefreshInterval(_refreshInterval);
78              LOG.debug("setupPropertyUserStore: Starting new PropertyUserStore. PropertiesFile: " + _filename + " refreshInterval: " + _refreshInterval);
79  
80              try
81              {
82                  _propertyUserStore.start();
83              }
84              catch (Exception e)
85              {
86                  LOG.warn("Exception while starting propertyUserStore: ",e);
87              }
88  
89              _propertyUserStores.put(_filename,_propertyUserStore);
90          }
91      }
92  
93      private void parseConfig(Map<String, ?> options)
94      {
95          _filename = (String)options.get("file") != null?(String)options.get("file"):DEFAULT_FILENAME;
96          String refreshIntervalString = (String)options.get("refreshInterval");
97          _refreshInterval = refreshIntervalString == null?_refreshInterval:Integer.parseInt(refreshIntervalString);
98      }
99  
100     /**
101      * Don't implement this as we want to pre-fetch all of the users.
102      *
103      * @param userName
104      * @throws Exception
105      */
106     public UserInfo getUserInfo(String userName) throws Exception
107     {
108         PropertyUserStore propertyUserStore = _propertyUserStores.get(_filename);
109         if (propertyUserStore == null)
110             throw new IllegalStateException("PropertyUserStore should never be null here!");
111 
112         UserIdentity userIdentity = propertyUserStore.getUserIdentity(userName);
113         if(userIdentity==null)
114             return null;
115 
116         Set<Principal> principals = userIdentity.getSubject().getPrincipals();
117 
118         List<String> roles = new ArrayList<String>();
119 
120         for ( Principal principal : principals )
121         {
122             roles.add( principal.getName() );
123         }
124 
125         Credential credential = (Credential)userIdentity.getSubject().getPrivateCredentials().iterator().next();
126         LOG.debug("Found: " + userName + " in PropertyUserStore");
127         return new UserInfo(userName, credential, roles);
128     }
129 
130 }