1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.plus.jaas.spi;
20
21 import java.security.Principal;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.security.auth.Subject;
30 import javax.security.auth.callback.CallbackHandler;
31
32 import org.eclipse.jetty.util.security.Credential;
33 import org.eclipse.jetty.security.PropertyUserStore;
34 import org.eclipse.jetty.server.UserIdentity;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
37
38
39
40
41
42
43 public class PropertyFileLoginModule extends AbstractLoginModule
44 {
45 public static final String DEFAULT_FILENAME = "realm.properties";
46
47 private static final Logger LOG = Log.getLogger(PropertyFileLoginModule.class);
48
49 private static Map<String, PropertyUserStore> _propertyUserStores = new HashMap<String, PropertyUserStore>();
50
51 private int _refreshInterval = 0;
52 private String _filename = DEFAULT_FILENAME;
53
54
55
56
57
58
59
60
61
62
63
64 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options)
65 {
66 super.initialize(subject,callbackHandler,sharedState,options);
67 setupPropertyUserStore(options);
68 }
69
70 private void setupPropertyUserStore(Map<String, ?> options)
71 {
72 if (_propertyUserStores.get(_filename) == null)
73 {
74 parseConfig(options);
75
76 PropertyUserStore _propertyUserStore = new PropertyUserStore();
77 _propertyUserStore.setConfig(_filename);
78 _propertyUserStore.setRefreshInterval(_refreshInterval);
79 LOG.debug("setupPropertyUserStore: Starting new PropertyUserStore. PropertiesFile: " + _filename + " refreshInterval: " + _refreshInterval);
80
81 try
82 {
83 _propertyUserStore.start();
84 }
85 catch (Exception e)
86 {
87 LOG.warn("Exception while starting propertyUserStore: ",e);
88 }
89
90 _propertyUserStores.put(_filename,_propertyUserStore);
91 }
92 }
93
94 private void parseConfig(Map<String, ?> options)
95 {
96 _filename = (String)options.get("file") != null?(String)options.get("file"):DEFAULT_FILENAME;
97 String refreshIntervalString = (String)options.get("refreshInterval");
98 _refreshInterval = refreshIntervalString == null?_refreshInterval:Integer.parseInt(refreshIntervalString);
99 }
100
101
102
103
104
105
106
107 public UserInfo getUserInfo(String userName) throws Exception
108 {
109 PropertyUserStore propertyUserStore = _propertyUserStores.get(_filename);
110 if (propertyUserStore == null)
111 throw new IllegalStateException("PropertyUserStore should never be null here!");
112
113 UserIdentity userIdentity = propertyUserStore.getUserIdentity(userName);
114 if(userIdentity==null)
115 return null;
116
117 Set<Principal> principals = userIdentity.getSubject().getPrincipals();
118
119 List<String> roles = new ArrayList<String>();
120
121 for ( Principal principal : principals )
122 {
123 roles.add( principal.getName() );
124 }
125
126 Credential credential = (Credential)userIdentity.getSubject().getPrivateCredentials().iterator().next();
127 LOG.debug("Found: " + userName + " in PropertyUserStore");
128 return new UserInfo(userName, credential, roles);
129 }
130
131 }