annotate annotator_files/lib/plugin/auth.js @ 3:6356e78ccf5c

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children 6979313586cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
1 var base64Decode, base64UrlDecode, createDateFromISO8601, parseToken,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
2 __hasProp = Object.prototype.hasOwnProperty,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
3 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
4
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
5 createDateFromISO8601 = function(string) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
6 var d, date, offset, regexp, time, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
7 regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" + "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
8 d = string.match(new RegExp(regexp));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
9 offset = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
10 date = new Date(d[1], 0, 1);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
11 if (d[3]) date.setMonth(d[3] - 1);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
12 if (d[5]) date.setDate(d[5]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
13 if (d[7]) date.setHours(d[7]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
14 if (d[8]) date.setMinutes(d[8]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
15 if (d[10]) date.setSeconds(d[10]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
16 if (d[12]) date.setMilliseconds(Number("0." + d[12]) * 1000);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
17 if (d[14]) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
18 offset = (Number(d[16]) * 60) + Number(d[17]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
19 offset *= (_ref = d[15] === '-') != null ? _ref : {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
20 1: -1
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
21 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
22 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
23 offset -= date.getTimezoneOffset();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
24 time = Number(date) + (offset * 60 * 1000);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
25 date.setTime(Number(time));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
26 return date;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
27 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
28
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
29 base64Decode = function(data) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
30 var ac, b64, bits, dec, h1, h2, h3, h4, i, o1, o2, o3, tmp_arr;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
31 if (typeof atob !== "undefined" && atob !== null) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
32 return atob(data);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
33 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
34 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
35 i = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
36 ac = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
37 dec = "";
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
38 tmp_arr = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
39 if (!data) return data;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
40 data += '';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
41 while (i < data.length) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
42 h1 = b64.indexOf(data.charAt(i++));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
43 h2 = b64.indexOf(data.charAt(i++));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
44 h3 = b64.indexOf(data.charAt(i++));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
45 h4 = b64.indexOf(data.charAt(i++));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
46 bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
47 o1 = bits >> 16 & 0xff;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
48 o2 = bits >> 8 & 0xff;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
49 o3 = bits & 0xff;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
50 if (h3 === 64) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
51 tmp_arr[ac++] = String.fromCharCode(o1);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
52 } else if (h4 === 64) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
53 tmp_arr[ac++] = String.fromCharCode(o1, o2);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
54 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
55 tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
56 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
57 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
58 return tmp_arr.join('');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
59 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
60 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
61
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
62 base64UrlDecode = function(data) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
63 var i, m, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
64 m = data.length % 4;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
65 if (m !== 0) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
66 for (i = 0, _ref = 4 - m; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
67 data += '=';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
68 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
69 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
70 data = data.replace(/-/g, '+');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
71 data = data.replace(/_/g, '/');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
72 return base64Decode(data);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
73 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
74
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
75 parseToken = function(token) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
76 var head, payload, sig, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
77 _ref = token.split('.'), head = _ref[0], payload = _ref[1], sig = _ref[2];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
78 return JSON.parse(base64UrlDecode(payload));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
79 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
80
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
81 Annotator.Plugin.Auth = (function(_super) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
82
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
83 __extends(Auth, _super);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
84
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
85 Auth.prototype.options = {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
86 token: null,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
87 tokenUrl: '/auth/token',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
88 autoFetch: true
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
89 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
90
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
91 function Auth(element, options) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
92 Auth.__super__.constructor.apply(this, arguments);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
93 this.waitingForToken = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
94 if (this.options.token) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
95 this.setToken(this.options.token);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
96 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
97 this.requestToken();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
98 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
99 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
100
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
101 Auth.prototype.requestToken = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
102 var _this = this;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
103 this.requestInProgress = true;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
104 return $.ajax({
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
105 url: this.options.tokenUrl,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
106 dataType: 'text',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
107 xhrFields: {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
108 withCredentials: true
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
109 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
110 }).done(function(data, status, xhr) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
111 return _this.setToken(data);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
112 }).fail(function(xhr, status, err) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
113 var msg;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
114 msg = Annotator._t("Couldn't get auth token:");
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
115 console.error("" + msg + " " + err, xhr);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
116 return Annotator.showNotification("" + msg + " " + xhr.responseText, Annotator.Notification.ERROR);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
117 }).always(function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
118 return _this.requestInProgress = false;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
119 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
120 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
121
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
122 Auth.prototype.setToken = function(token) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
123 var _results,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
124 _this = this;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
125 this.token = token;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
126 this._unsafeToken = parseToken(token);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
127 if (this.haveValidToken()) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
128 if (this.options.autoFetch) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
129 this.refreshTimeout = setTimeout((function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
130 return _this.requestToken();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
131 }), (this.timeToExpiry() - 2) * 1000);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
132 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
133 this.updateHeaders();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
134 _results = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
135 while (this.waitingForToken.length > 0) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
136 _results.push(this.waitingForToken.pop()(this._unsafeToken));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
137 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
138 return _results;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
139 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
140 console.warn(Annotator._t("Didn't get a valid token."));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
141 if (this.options.autoFetch) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
142 console.warn(Annotator._t("Getting a new token in 10s."));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
143 return setTimeout((function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
144 return _this.requestToken();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
145 }), 10 * 1000);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
146 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
147 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
148 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
149
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
150 Auth.prototype.haveValidToken = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
151 var allFields;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
152 allFields = this._unsafeToken && this._unsafeToken.issuedAt && this._unsafeToken.ttl && this._unsafeToken.consumerKey;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
153 return allFields && this.timeToExpiry() > 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
154 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
155
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
156 Auth.prototype.timeToExpiry = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
157 var expiry, issue, now, timeToExpiry;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
158 now = new Date().getTime() / 1000;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
159 issue = createDateFromISO8601(this._unsafeToken.issuedAt).getTime() / 1000;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
160 expiry = issue + this._unsafeToken.ttl;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
161 timeToExpiry = expiry - now;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
162 if (timeToExpiry > 0) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
163 return timeToExpiry;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
164 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
165 return 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
166 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
167 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
168
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
169 Auth.prototype.updateHeaders = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
170 var current;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
171 current = this.element.data('annotator:headers');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
172 return this.element.data('annotator:headers', $.extend(current, {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
173 'x-annotator-auth-token': this.token
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
174 }));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
175 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
176
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
177 Auth.prototype.withToken = function(callback) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
178 if (!(callback != null)) return;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
179 if (this.haveValidToken()) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
180 return callback(this._unsafeToken);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
181 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
182 this.waitingForToken.push(callback);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
183 if (!this.requestInProgress) return this.requestToken();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
184 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
185 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
186
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
187 return Auth;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
188
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
189 })(Annotator.Plugin);