comparison webapp/src/main/webapp/jquery/jquery.digilib.annotator.js @ 1120:8bd10cd04169

authentication for annotations with password works now. (permissions are still flaky.)
author robcast
date Wed, 07 Nov 2012 18:09:57 +0100
parents 036efe7361c8
children efe4b0f18cf8
comparison
equal deleted inserted replaced
1119:036efe7361c8 1120:8bd10cd04169
106 106
107 /** 107 /**
108 * set user account for annotations 108 * set user account for annotations
109 */ 109 */
110 setAnnotationUser : function (data, user, password) { 110 setAnnotationUser : function (data, user, password) {
111 var settings = data.settings; 111 var annotator = data.annotator;
112 if (user == null) { 112 var auth = annotator.plugins.Auth;
113 // user name entered in JS-prompt 113 if (auth == null) {
114 user = window.prompt("User name:", settings.annotationUser); 114 console.error("setAnnotationUser: No Auth plugin!!");
115 if (user != null) { 115 return;
116 // password entered in JS-prompt 116 }
117 password = window.prompt("Password:", ''); 117 setAnnotationUser(data, auth, user, password);
118 settings.annotationUser = user; 118 // get new token
119 data.dlOpts.annotationUser = user; 119 auth.token = null;
120 digilib.fn.storeOptions(data); 120 auth._unsafeToken = null;
121 loadAnnotationToken(data, password); 121 auth.requestToken();
122 } 122 // save new token in cookie
123 } else { 123 auth.withToken(function (tkn) {
124 settings.annotationUser = user; 124 data.dlOpts.annotationToken = auth.token;
125 data.dlOpts.annotationUser = user; 125 digilib.fn.storeOptions(data);
126 digilib.fn.storeOptions(data); 126 // reload annotations
127 loadAnnotationToken(data, password); 127 data.annotations = [];
128 } 128 renderAnnotations(data);
129 annotator.plugins.Store.pluginInit();
130 });
129 }, 131 },
130 132
131 /** 133 /**
132 * set a mark-annotation by clicking (or giving a position and a text) 134 * set a mark-annotation by clicking (or giving a position and a text)
133 * 135 *
153 var url = data.settings.digilibBaseUrl + '/jquery/digilib.html?'; 155 var url = data.settings.digilibBaseUrl + '/jquery/digilib.html?';
154 url += digilib.fn.getParamString(data.settings, ['fn', 'pn'], digilib.defaults); 156 url += digilib.fn.getParamString(data.settings, ['fn', 'pn'], digilib.defaults);
155 return url; 157 return url;
156 }; 158 };
157 159
160 /**
161 * sets annotation user and password in digilib and Annotator.Auth plugin.
162 * auth is Auth plugin instance.
163 */
164 var setAnnotationUser = function (data, auth, user, password) {
165 if (user == null) {
166 // user name entered in JS-prompt
167 user = window.prompt("Please authenticate: User name", data.settings.annotationUser);
168 if (user != null && user != 'anonymous') {
169 // password entered in JS-prompt
170 password = window.prompt("Please authenticate: Password", '');
171 // set params for Auth plugin
172 auth.options.requestData.password = password;
173 } else {
174 // use anonymous user
175 user = 'anonymous';
176 delete auth.options.requestData.password;
177 }
178 }
179 // set user in digilib
180 data.settings.annotationUser = user;
181 data.dlOpts.annotationUser = user;
182 digilib.fn.storeOptions(data);
183 // set params for Auth plugin
184 auth.options.requestData.user = user;
185 // set params for Permissions plugin
186 var perms = data.annotator.plugins.Permissions;
187 if (perms != null) {
188 perms.options.user = user;
189 }
190 };
191
192
158 /** 193 /**
159 * add a mark-annotation where clicked. 194 * add a mark-annotation where clicked.
160 */ 195 */
161 var setAnnotationMark = function(data) { 196 var setAnnotationMark = function(data) {
162 var $scaler = data.$scaler; 197 var $scaler = data.$scaler;
268 break; 303 break;
269 } 304 }
270 } 305 }
271 }; 306 };
272 }; 307 };
273 308
274 /** 309 /**
275 * Get an authentication token from the token server. 310 * returns handleUnauthorized function for Annotator.Auth plugin.
276 * 311 */
277 * Stores the token and loads annotations on success. 312 var getHandleUnauthorized = function (data) {
278 */ 313 return function (auth) {
279 var loadAnnotationToken = function(data, password) { 314 // prompt for user name and set user
280 return; 315 setAnnotationUser(data, auth);
281 var settings = data.settings; 316 // then try again
282 var url = settings.annotationTokenUrl; 317 return true;
283 var params = {'user': settings.annotationUser}; 318 };
284 if (password != null) { 319 };
285 params.password = password; 320
286 }
287 // TODO: better error handling
288 $.post(url, params)
289 .done(function (authToken, authStatus) {
290 console.debug("got auth token data=", authToken);
291 data.annotationToken = authToken;
292 data.dlOpts.annotationToken = authToken;
293 digilib.fn.storeOptions(data);
294 //loadAnnotations(data);
295 })
296 .fail(function (xhr, status) {
297 console.error("got auth token error:", xhr);
298 data.annotationToken = null;
299 data.settings.annotationUser = "anonymous";
300 //loadAnnotations(data);
301 });
302 };
303
304 /** 321 /**
305 * install additional buttons 322 * install additional buttons
306 */ 323 */
307 var installButtons = function(data) { 324 var installButtons = function(data) {
308 var settings = data.settings; 325 var settings = data.settings;
343 } 360 }
344 if (data.dlOpts.annotationUser != null) { 361 if (data.dlOpts.annotationUser != null) {
345 // get annotation user from cookie 362 // get annotation user from cookie
346 data.settings.annotationUser = data.dlOpts.annotationUser; 363 data.settings.annotationUser = data.dlOpts.annotationUser;
347 } 364 }
348 if (data.dlOpts.annotationToken != null) {
349 // get annotation token from cookie
350 data.annotationToken = data.dlOpts.annotationToken;
351 }
352 // install event handler 365 // install event handler
353 $data.bind('setup', handleSetup); 366 $data.bind('setup', handleSetup);
354 $data.bind('update', handleUpdate); 367 $data.bind('update', handleUpdate);
355 }; 368 };
356 369
358 * setup creates Annotator object. 371 * setup creates Annotator object.
359 */ 372 */
360 var handleSetup = function(evt) { 373 var handleSetup = function(evt) {
361 console.debug("annotations: handleSetup"); 374 console.debug("annotations: handleSetup");
362 var data = this; 375 var data = this;
363 /* if (data.annotationToken == null) {
364 loadAnnotationToken(data);
365 } */
366 // set up annotator (after html has been set up) 376 // set up annotator (after html has been set up)
367 var uri = getAnnotationPageUrl(data); 377 var uri = getAnnotationPageUrl(data);
368 var annotator = new Annotator(data.$elem.get(0)); 378 var annotator = new Annotator(data.$elem.get(0));
369 // set plugin parameters 379 // set plugin parameters
370 var pluginParams = { 380 var pluginParams = {
371 'Auth' : { 381 'Auth' : {
372 'token' : data.annotationToken, 382 'token' : data.dlOpts.annotationToken,
373 'tokenUrl' : data.settings.annotationTokenUrl, 383 'tokenUrl' : data.settings.annotationTokenUrl,
374 'autoFetch' : true, 384 'autoFetch' : true,
375 'requestMethod' : 'POST', 385 'requestMethod' : 'POST',
376 'requestData' : { 386 'requestData' : {
377 'user': data.settings.annotationUser, 387 'user': data.settings.annotationUser,
378 'password': data.annotationPassword
379 }, 388 },
380 'unauthorizedCallback' : function (that) { 389 'unauthorizedCallback' : getHandleUnauthorized(data)
381 window.alert("401");
382 return false;
383 }
384 }, 390 },
385 'Permissions' : { 391 'Permissions' : {
386 'user' : data.settings.annotationUser 392 'user' : data.settings.annotationUser
387 }, 393 },
388 'Store' : { 394 'Store' : {
408 console.debug("plugin:", name, params); 414 console.debug("plugin:", name, params);
409 annotator.addPlugin(name, params); 415 annotator.addPlugin(name, params);
410 }); 416 });
411 // save annotator reference 417 // save annotator reference
412 data.annotator = annotator; 418 data.annotator = annotator;
419 // save annotation token in cookie
420 var auth = annotator.plugins.Auth;
421 // save new token in cookie
422 auth.withToken(function (tkn) {
423 data.dlOpts.annotationToken = auth.token;
424 digilib.fn.storeOptions(data);
425 });
413 }; 426 };
414 427
415 /** 428 /**
416 * update renders all annotations. 429 * update renders all annotations.
417 */ 430 */