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