0
|
1 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
2
|
9
|
3 import java.io.File;
|
|
4 import java.io.FileInputStream;
|
|
5 import java.io.FileNotFoundException;
|
|
6 import java.io.IOException;
|
|
7 import java.io.InputStream;
|
0
|
8 import java.net.URI;
|
|
9 import java.net.URISyntaxException;
|
|
10 import java.util.Hashtable;
|
9
|
11 import java.util.Properties;
|
0
|
12
|
|
13 import javax.naming.NamingEnumeration;
|
|
14 import javax.naming.NamingException;
|
|
15 import javax.naming.directory.Attribute;
|
|
16 import javax.naming.directory.DirContext;
|
|
17 import javax.naming.directory.InitialDirContext;
|
|
18 import javax.naming.directory.SearchControls;
|
|
19 import javax.naming.directory.SearchResult;
|
|
20 import javax.security.auth.Subject;
|
|
21 import javax.security.auth.login.Configuration;
|
|
22 import javax.security.auth.login.LoginContext;
|
|
23 import javax.security.auth.login.LoginException;
|
9
|
24 import javax.servlet.ServletContext;
|
0
|
25
|
|
26 import org.apache.log4j.BasicConfigurator;
|
|
27 import org.apache.log4j.Level;
|
|
28 import org.apache.log4j.Logger;
|
|
29 import org.restlet.Application;
|
|
30 import org.restlet.Context;
|
|
31 import org.restlet.Request;
|
|
32 import org.restlet.Response;
|
|
33 import org.restlet.Restlet;
|
|
34 import org.restlet.data.ChallengeScheme;
|
|
35 import org.restlet.data.ClientInfo;
|
|
36 import org.restlet.ext.jaas.JaasVerifier;
|
|
37 import org.restlet.routing.Router;
|
|
38 import org.restlet.security.ChallengeAuthenticator;
|
|
39 import org.restlet.security.User;
|
|
40
|
|
41 import com.sun.security.auth.login.ConfigFile;
|
|
42
|
|
43 public class RestServer extends Application {
|
|
44
|
9
|
45 private ChallengeAuthenticator authenticator;
|
|
46
|
|
47 /**
|
|
48 * Properties holding consumer keys and secrets
|
|
49 */
|
|
50 private Properties consumerKeys;
|
|
51 public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
|
|
52
|
|
53 /**
|
|
54 * constructor
|
|
55 *
|
|
56 * @param parentContext
|
|
57 */
|
|
58 public RestServer(Context parentContext) {
|
|
59 super(parentContext);
|
0
|
60
|
9
|
61 Logger rl = Logger.getRootLogger();
|
|
62 BasicConfigurator.configure();
|
|
63 rl.setLevel(Level.DEBUG);
|
|
64 // read consumerKeys from webapp
|
|
65 consumerKeys = new Properties();
|
|
66 ServletContext sc = (ServletContext) getContext().getServerDispatcher()
|
|
67 .getContext().getAttributes()
|
|
68 .get("org.restlet.ext.servlet.ServletContext");
|
|
69 if (sc != null) {
|
|
70 InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH);
|
|
71 if (ps == null) {
|
|
72 // try as file
|
|
73 File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH));
|
|
74 if (pf != null) {
|
|
75 rl.debug("trying file for consumer keys: "+pf);
|
|
76 try {
|
|
77 ps = new FileInputStream(pf);
|
|
78 } catch (FileNotFoundException e) {
|
|
79 // TODO Auto-generated catch block
|
|
80 e.printStackTrace();
|
|
81 }
|
|
82 }
|
|
83 }
|
|
84 if (ps != null) {
|
|
85 rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
|
|
86 try {
|
|
87 consumerKeys.load(ps);
|
|
88 } catch (IOException e) {
|
|
89 // TODO Auto-generated catch block
|
|
90 e.printStackTrace();
|
|
91 }
|
|
92 rl.debug("consumer keys: "+consumerKeys);
|
|
93 } else {
|
|
94 rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
|
|
95 }
|
|
96 } else {
|
|
97 rl.error("Unable to get ServletContext!");
|
|
98 }
|
|
99
|
|
100 }
|
|
101
|
|
102 /**
|
|
103 * returns consumer secret for consumer key.
|
|
104 * returns null if consumer key doesn't exist.
|
|
105 * @param consumerKey
|
|
106 * @return
|
|
107 */
|
|
108 public String getConsumerSecret(String consumerKey) {
|
|
109 return consumerKeys.getProperty(consumerKey);
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Erzeuge einen Authenticator
|
|
114 *
|
0
|
115 * @return
|
|
116 */
|
|
117 private ChallengeAuthenticator createAuthenticator() {
|
|
118 Context context = getContext();
|
|
119 boolean optional = true;
|
|
120 ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
|
|
121 String realm = "Annotation Service";
|
|
122
|
9
|
123 JaasVerifier verifier = new JaasVerifier(
|
|
124 "BasicJaasAuthenticationApplication");
|
|
125 // JaasVerifier verifier = new JaasVerifier("DummyAuthentication");
|
|
126
|
0
|
127 Configuration jaasConfig;
|
|
128 jaasConfig = createConfiguration();
|
9
|
129
|
|
130 verifier.setConfiguration(jaasConfig);
|
0
|
131 verifier.setUserPrincipalClassName("com.sun.security.auth.UserPrincipal");
|
9
|
132
|
|
133 ChallengeAuthenticator auth = new ChallengeAuthenticator(context,
|
|
134 optional, challengeScheme, realm, verifier) {
|
0
|
135 @Override
|
|
136 protected boolean authenticate(Request request, Response response) {
|
|
137 if (request.getChallengeResponse() == null) {
|
|
138 return false;
|
|
139 } else {
|
|
140 return super.authenticate(request, response);
|
|
141 }
|
|
142 }
|
|
143 };
|
|
144
|
|
145 return auth;
|
|
146 }
|
|
147
|
9
|
148 /**
|
|
149 * Konfiguration fuer den Authentificator in Jaas. Pfad zum
|
|
150 * JAAS-Konfigurationsfile liegt im Context-Parameter
|
|
151 * "de.mpiwg.itgroup.annotationManager.jaas.configFilePath".
|
|
152 *
|
|
153 * @return
|
|
154 */
|
|
155 protected Configuration createConfiguration() {
|
|
156 Configuration jaasConfig;
|
|
157 URI confUri;
|
|
158
|
|
159 Context context = getContext();
|
|
160 String configFilePath = context.getParameters().getFirstValue(
|
|
161 "de.mpiwg.itgroup.annotationManager.jaas.configFilePath");
|
0
|
162
|
9
|
163 try {
|
|
164 confUri = new URI(configFilePath);
|
|
165 } catch (URISyntaxException e) {
|
|
166 e.printStackTrace();
|
|
167 confUri = null;
|
|
168 }
|
|
169
|
|
170 jaasConfig = new ConfigFile(confUri);
|
|
171 return jaasConfig;
|
|
172 }
|
|
173
|
|
174 /*
|
|
175 * (non-Javadoc)
|
|
176 *
|
|
177 * @see org.restlet.Application#createInboundRoot()
|
|
178 */
|
|
179 public synchronized Restlet createInboundRoot() {
|
|
180 this.authenticator = createAuthenticator();
|
|
181
|
|
182 // String target = "{rh}/{rf}/XX";
|
|
183 // Redirector redirector = new
|
|
184 // Redirector(getContext().createChildContext(), target,
|
|
185 // Redirector.MODE_CLIENT_SEE_OTHER);
|
0
|
186
|
9
|
187 Router router = new Router(getContext());
|
|
188
|
13
|
189 router.attach("/annotator/annotations", AnnotatorAnnotations.class);
|
15
|
190 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
|
|
191 router.attach("/annotator/search", AnnotatorSearch.class);
|
|
192
|
9
|
193 router.attach("/dummy", Dummy.class);
|
|
194
|
|
195 // router.attach("",redirector);
|
|
196 router.attach("/annotator", ExtendedAnnotationInput.class);
|
|
197 router.attach("/", AnnotatorInfo.class);
|
|
198 authenticator.setNext(router);
|
|
199 return authenticator;
|
|
200 }
|
0
|
201
|
9
|
202 /**
|
|
203 * Authentifiziere den Benutzer aus dem Request (BasicAuthenfication)
|
|
204 *
|
|
205 * @param request
|
|
206 * @param response
|
|
207 * @return
|
|
208 */
|
|
209 public boolean authenticate(Request request, Response response) {
|
|
210 if (!request.getClientInfo().isAuthenticated()) {
|
|
211 authenticator.challenge(response, false);
|
|
212 return false;
|
|
213 }
|
|
214
|
|
215 if (request.getClientInfo().getUser() == null) // FIXME sometimes ist
|
|
216 // authenticated true,
|
|
217 // but no user
|
|
218 {
|
|
219 authenticator.challenge(response, false);
|
|
220 return false;
|
|
221 }
|
|
222 return true;
|
|
223 }
|
0
|
224
|
9
|
225 /**
|
|
226 * Authentifiziere den Benutzer
|
|
227 *
|
|
228 * @param username
|
|
229 * @param password
|
|
230 * @param request
|
|
231 * @return
|
|
232 */
|
|
233 public boolean authenticate(String username, String password,
|
|
234 Request request) {
|
|
235 LoginContext lc;
|
|
236
|
|
237 try {
|
|
238 Configuration conf = createConfiguration();
|
0
|
239
|
9
|
240 lc = new LoginContext("BasicJaasAuthenticationApplication", null,
|
|
241 new MyCallBackHandler(username, password), conf);
|
|
242 // lc = new LoginContext("DummyAuthentication", null, new
|
|
243 // MyCallBackHandler(username,password),conf);
|
|
244 lc.login();
|
|
245 } catch (LoginException e) {
|
|
246 // TODO Auto-generated catch block
|
|
247 e.printStackTrace();
|
|
248 return false;
|
|
249 }
|
|
250
|
|
251 Subject subject = lc.getSubject();
|
|
252 ClientInfo clientInfo = new ClientInfo();
|
|
253 User user = new User(username);
|
|
254 clientInfo.setAuthenticated(true);
|
|
255 clientInfo.setUser(user);
|
|
256
|
|
257 request.setClientInfo(clientInfo);
|
|
258 return true;
|
|
259 }
|
|
260
|
|
261 /**
|
|
262 * Hole den vollen Benutzernamen aus dem LDAP
|
|
263 *
|
|
264 * @param creator
|
|
265 * @return
|
|
266 */
|
|
267 public String getUserNameFromLdap(String creator) {
|
|
268 String retString = creator; // falls nichts gefunden wird einfach den
|
|
269 // creator zurueckgeben
|
|
270 Hashtable<String, String> env = new Hashtable<String, String>();
|
|
271 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
|
|
272 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
|
0
|
273
|
9
|
274 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";// TODO
|
|
275 // should
|
|
276 // go
|
|
277 // into
|
|
278 // config
|
|
279 // file
|
|
280 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
|
0
|
281
|
9
|
282 DirContext dctx;
|
|
283 try {
|
|
284 dctx = new InitialDirContext(env);
|
|
285 } catch (NamingException e1) {
|
|
286 // TODO Auto-generated catch block
|
|
287 e1.printStackTrace();
|
|
288 return retString;
|
|
289 }
|
0
|
290
|
9
|
291 String base = "ou=People";
|
0
|
292
|
9
|
293 SearchControls sc = new SearchControls();
|
|
294 String[] attributeFilter = { "cn", "mail" };
|
|
295 sc.setReturningAttributes(attributeFilter);
|
|
296 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
0
|
297
|
9
|
298 String filter = "(uid=" + creator + ")";
|
|
299
|
|
300 try {
|
|
301 NamingEnumeration<SearchResult> results = dctx.search(base, filter,
|
|
302 sc);
|
|
303 while (results.hasMore()) {
|
|
304 SearchResult sr = (SearchResult) results.next();
|
|
305 javax.naming.directory.Attributes attrs = sr.getAttributes();
|
0
|
306
|
9
|
307 Attribute attr = attrs.get("cn");
|
|
308 retString = (String) attr.get();
|
|
309 }
|
|
310 } catch (NamingException e) {
|
|
311 // TODO Auto-generated catch block
|
|
312 e.printStackTrace();
|
|
313 }
|
|
314
|
|
315 try {
|
|
316 dctx.close();
|
|
317 } catch (NamingException e) {
|
|
318 // TODO Auto-generated catch block
|
|
319 e.printStackTrace();
|
|
320 }
|
|
321 return retString;
|
|
322 }
|
0
|
323
|
|
324 }
|