1
2
3
4
5
6
7
8
9
10
11
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
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
51
52
53
54
55
56
57
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
98
99
100
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 }