1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
55
56
57
58
59
60
61
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
102
103
104
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 }