0
|
1 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
2
|
|
3 import java.net.URI;
|
|
4 import java.net.URISyntaxException;
|
|
5 import java.util.Hashtable;
|
|
6
|
|
7 import javax.naming.NamingEnumeration;
|
|
8 import javax.naming.NamingException;
|
|
9 import javax.naming.directory.Attribute;
|
|
10 import javax.naming.directory.DirContext;
|
|
11 import javax.naming.directory.InitialDirContext;
|
|
12 import javax.naming.directory.SearchControls;
|
|
13 import javax.naming.directory.SearchResult;
|
|
14 import javax.security.auth.Subject;
|
|
15 import javax.security.auth.callback.Callback;
|
|
16 import javax.security.auth.callback.CallbackHandler;
|
|
17 import javax.security.auth.callback.NameCallback;
|
|
18 import javax.security.auth.callback.PasswordCallback;
|
|
19 import javax.security.auth.login.AppConfigurationEntry;
|
|
20 import javax.security.auth.login.Configuration;
|
|
21 import javax.security.auth.login.LoginContext;
|
|
22 import javax.security.auth.login.LoginException;
|
|
23
|
|
24 import org.apache.log4j.BasicConfigurator;
|
|
25 import org.apache.log4j.Level;
|
|
26 import org.apache.log4j.Logger;
|
|
27 import org.restlet.Application;
|
|
28 import org.restlet.Context;
|
|
29 import org.restlet.Request;
|
|
30 import org.restlet.Response;
|
|
31 import org.restlet.Restlet;
|
|
32
|
|
33 import org.restlet.data.ChallengeScheme;
|
|
34 import org.restlet.data.ClientInfo;
|
2
|
35 import org.restlet.engine.component.ChildContext;
|
0
|
36 import org.restlet.ext.jaas.JaasVerifier;
|
3
|
37 import org.restlet.routing.Redirector;
|
0
|
38 import org.restlet.routing.Router;
|
|
39 import org.restlet.routing.Template;
|
|
40 import org.restlet.routing.TemplateRoute;
|
|
41 import org.restlet.security.ChallengeAuthenticator;
|
|
42 import org.restlet.security.MapVerifier;
|
|
43 import org.restlet.security.User;
|
|
44 import org.restlet.security.Verifier;
|
|
45
|
|
46 import com.sun.org.apache.xalan.internal.xsltc.runtime.Attributes;
|
|
47 import com.sun.security.auth.login.ConfigFile;
|
|
48
|
|
49
|
|
50
|
|
51
|
|
52 public class RestServer extends Application {
|
|
53
|
|
54
|
|
55 private ChallengeAuthenticator authenticator;
|
|
56 private CallbackHandler callbackHandler;
|
|
57
|
|
58 /** Erzeuge einen Authenticator
|
|
59 * @return
|
|
60 */
|
|
61 private ChallengeAuthenticator createAuthenticator() {
|
|
62 Context context = getContext();
|
|
63 boolean optional = true;
|
|
64 ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
|
|
65 String realm = "Annotation Service";
|
|
66
|
|
67 JaasVerifier verifier = new JaasVerifier("BasicJaasAuthenticationApplication");
|
|
68
|
|
69
|
|
70 Configuration jaasConfig;
|
|
71 jaasConfig = createConfiguration();
|
|
72
|
|
73
|
|
74 verifier.setConfiguration(jaasConfig);
|
|
75 verifier.setUserPrincipalClassName("com.sun.security.auth.UserPrincipal");
|
|
76
|
|
77 ChallengeAuthenticator auth = new ChallengeAuthenticator(context, optional, challengeScheme, realm, verifier) {
|
|
78 @Override
|
|
79 protected boolean authenticate(Request request, Response response) {
|
|
80 if (request.getChallengeResponse() == null) {
|
|
81 return false;
|
|
82 } else {
|
|
83 return super.authenticate(request, response);
|
|
84 }
|
|
85 }
|
|
86 };
|
|
87
|
|
88 return auth;
|
|
89 }
|
|
90
|
2
|
91 /**
|
|
92 * Konfiguration fŸr den Authentificator in Jaas. Pfad zum JAAS-Konfigurationsfile liegt im Context-Parameter
|
|
93 * "de.mpiwg.itgroup.annotationManager.jaas.configFilePath".
|
|
94 * @return
|
|
95 */
|
0
|
96 protected Configuration createConfiguration() {
|
|
97 Configuration jaasConfig;
|
|
98 URI confUri;
|
2
|
99
|
|
100 Context context = getContext();
|
|
101 String configFilePath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.jaas.configFilePath");
|
|
102
|
|
103
|
0
|
104 try {
|
2
|
105 confUri = new URI(configFilePath);
|
0
|
106 } catch (URISyntaxException e) {
|
|
107 e.printStackTrace();
|
|
108 confUri = null;
|
|
109 }
|
|
110
|
|
111 jaasConfig= new ConfigFile(confUri);
|
|
112 return jaasConfig;
|
|
113 }
|
|
114
|
|
115 public RestServer(Context parentContext){
|
|
116 super(parentContext);
|
|
117
|
|
118 Logger rl = Logger.getRootLogger();
|
|
119 BasicConfigurator.configure();
|
|
120 rl.setLevel(Level.DEBUG);
|
|
121
|
|
122
|
|
123 }
|
|
124
|
|
125 public synchronized Restlet createInboundRoot(){
|
|
126 this.authenticator = createAuthenticator();
|
|
127
|
|
128
|
3
|
129 //String target = "{rh}/{rf}/XX";
|
|
130 //Redirector redirector = new Redirector(getContext().createChildContext(), target,
|
|
131 // Redirector.MODE_CLIENT_SEE_OTHER);
|
|
132
|
0
|
133 Router router = new Router(getContext());
|
|
134
|
3
|
135
|
0
|
136 router.attach("/annotations",AddAndSearchAnnotations.class);
|
|
137 router.attach("/search",AddAndSearchAnnotations.class); // annotator api askes for different uris for search and adding
|
|
138 router.attach("/dummy",Dummy.class);
|
|
139
|
3
|
140 //router.attach("",redirector);
|
|
141 router.attach("/",AnnotatorInfo.class);
|
0
|
142 authenticator.setNext(router);
|
|
143 return authenticator;
|
|
144
|
|
145
|
|
146
|
|
147 }
|
|
148
|
2
|
149 /**
|
|
150 * Authentifiziere den Benutzer aus dem Request (BasicAuthenfication)
|
|
151 * @param request
|
|
152 * @param response
|
|
153 * @return
|
|
154 */
|
|
155 public boolean authenticate(Request request, Response response) {
|
0
|
156 if (!request.getClientInfo().isAuthenticated()) {
|
|
157 authenticator.challenge(response, false);
|
|
158 return false;
|
|
159 }
|
|
160
|
|
161 if(request.getClientInfo().getUser()==null) //FIXME sometimes ist authenticated true, but no user
|
|
162 {
|
|
163 authenticator.challenge(response, false);
|
|
164 return false;
|
|
165 }
|
2
|
166
|
|
167
|
0
|
168 return true;
|
|
169 }
|
|
170
|
2
|
171 /**
|
|
172 * Authentifiziere den Benutzer
|
|
173 *
|
|
174 * @param username
|
|
175 * @param password
|
|
176 * @param request
|
|
177 * @return
|
|
178 */
|
0
|
179 public boolean authenticate(String username, String password,Request request) {
|
|
180 LoginContext lc;
|
|
181
|
|
182 try {
|
|
183 Configuration conf = createConfiguration();
|
|
184
|
|
185 lc = new LoginContext("BasicJaasAuthenticationApplication", null, new MyCallBackHandler(username,password),conf);
|
|
186 lc.login();
|
|
187 } catch (LoginException e) {
|
|
188 // TODO Auto-generated catch block
|
|
189 e.printStackTrace();
|
|
190 return false;
|
|
191 }
|
|
192
|
|
193 Subject subject = lc.getSubject();
|
|
194 ClientInfo clientInfo = new ClientInfo();
|
|
195 User user = new User(username);
|
|
196 clientInfo.setAuthenticated(true);
|
|
197 clientInfo.setUser(user);
|
|
198
|
|
199 request.setClientInfo(clientInfo);
|
|
200 return true;
|
|
201 }
|
|
202
|
2
|
203 /**
|
|
204 * Hole den vollen Benutzernamen aus dem LDAP
|
|
205 * @param creator
|
|
206 * @return
|
|
207 */
|
0
|
208 public String getUserNameFromLdap(String creator) {
|
|
209 String retString=creator; // falls nichts gefunden wird einfach den creator zurueckgeben
|
|
210 Hashtable<String,String> env = new Hashtable<String,String>();
|
|
211 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
|
|
212 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
|
|
213
|
1
|
214 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";//TODO should go into config file
|
0
|
215 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
|
|
216
|
|
217 DirContext dctx;
|
|
218 try {
|
|
219 dctx = new InitialDirContext(env);
|
|
220 } catch (NamingException e1) {
|
|
221 // TODO Auto-generated catch block
|
|
222 e1.printStackTrace();
|
|
223 return retString;
|
|
224 }
|
|
225
|
|
226 String base = "ou=People";
|
|
227
|
|
228 SearchControls sc = new SearchControls();
|
|
229 String[] attributeFilter = { "cn", "mail" };
|
|
230 sc.setReturningAttributes(attributeFilter);
|
|
231 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
232
|
|
233 String filter = "(uid="+creator+")";
|
|
234
|
|
235 try {
|
|
236 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc);
|
|
237 while (results.hasMore()) {
|
|
238 SearchResult sr = (SearchResult) results.next();
|
|
239 javax.naming.directory.Attributes attrs = sr.getAttributes();
|
|
240
|
|
241 Attribute attr = attrs.get("cn");
|
|
242 retString=(String) attr.get();
|
|
243 }
|
|
244 } catch (NamingException e) {
|
|
245 // TODO Auto-generated catch block
|
|
246 e.printStackTrace();
|
|
247 }
|
|
248
|
|
249 try {
|
|
250 dctx.close();
|
|
251 } catch (NamingException e) {
|
|
252 // TODO Auto-generated catch block
|
|
253 e.printStackTrace();
|
|
254 }
|
|
255 return retString;
|
|
256 }
|
|
257
|
|
258 }
|