annotate annotator_files/lib/plugin/auth.js @ 4:6979313586cf

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