source: main/trunk/greenstone3/web/interfaces/oran/js/annotator/pkg/annotator.auth.min.js@ 24771

Last change on this file since 24771 was 24771, checked in by papitha, 13 years ago

Changes to JS

File size: 4.7 KB
Line 
1(function() {
2 var createDateFromISO8601;
3 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4 for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5 function ctor() { this.constructor = child; }
6 ctor.prototype = parent.prototype;
7 child.prototype = new ctor;
8 child.__super__ = parent.prototype;
9 return child;
10 };
11 createDateFromISO8601 = function(string) {
12 var d, date, offset, regexp, time, _ref;
13 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})))?)?)?)?";
14 d = string.match(new RegExp(regexp));
15 offset = 0;
16 date = new Date(d[1], 0, 1);
17 if (d[3]) {
18 date.setMonth(d[3] - 1);
19 }
20 if (d[5]) {
21 date.setDate(d[5]);
22 }
23 if (d[7]) {
24 date.setHours(d[7]);
25 }
26 if (d[8]) {
27 date.setMinutes(d[8]);
28 }
29 if (d[10]) {
30 date.setSeconds(d[10]);
31 }
32 if (d[12]) {
33 date.setMilliseconds(Number("0." + d[12]) * 1000);
34 }
35 if (d[14]) {
36 offset = (Number(d[16]) * 60) + Number(d[17]);
37 offset *= (_ref = d[15] === '-') != null ? _ref : {
38 1: -1
39 };
40 }
41 offset -= date.getTimezoneOffset();
42 time = Number(date) + (offset * 60 * 1000);
43 date.setTime(Number(time));
44 return date;
45 };
46 Annotator.Plugin.Auth = (function() {
47 __extends(Auth, Annotator.Plugin);
48 Auth.prototype.options = {
49 token: null,
50 tokenUrl: '/auth/token',
51 autoFetch: true
52 };
53 function Auth(element, options) {
54 this.haveValidToken = __bind(this.haveValidToken, this); Auth.__super__.constructor.apply(this, arguments);
55 this.element.data('annotator:auth', this);
56 this.waitingForToken = [];
57 if (this.options.token) {
58 this.setToken(this.options.token);
59 } else {
60 this.requestToken();
61 }
62 }
63 Auth.prototype.requestToken = function() {
64 this.requestInProgress = true;
65 return $.getJSON(this.options.tokenUrl, __bind(function(data, status, xhr) {
66 if (status !== 'success') {
67 return console.error("Couldn't get auth token: " + status, xhr);
68 } else {
69 this.setToken(data);
70 return this.requestInProgress = false;
71 }
72 }, this));
73 };
74 Auth.prototype.setToken = function(token) {
75 var _results;
76 this.token = token;
77 if (this.haveValidToken()) {
78 if (this.options.autoFetch) {
79 this.refreshTimeout = setTimeout((__bind(function() {
80 return this.requestToken();
81 }, this)), (this.timeToExpiry() - 2) * 1000);
82 }
83 this.updateHeaders();
84 _results = [];
85 while (this.waitingForToken.length > 0) {
86 _results.push(this.waitingForToken.pop().apply());
87 }
88 return _results;
89 } else {
90 console.warn("Didn't get a valid token.");
91 if (this.options.autoFetch) {
92 console.warn("Getting a new token in 10s.");
93 return setTimeout((__bind(function() {
94 return this.requestToken();
95 }, this)), 10 * 1000);
96 }
97 }
98 };
99 Auth.prototype.haveValidToken = function() {
100 var allFields;
101 allFields = this.token && this.token.authToken && this.token.authTokenIssueTime && this.token.authTokenTTL && this.token.accountId && this.token.userId;
102 return allFields && this.timeToExpiry() > 0;
103 };
104 Auth.prototype.timeToExpiry = function() {
105 var expiry, issue, now, timeToExpiry;
106 now = new Date().getTime() / 1000;
107 issue = createDateFromISO8601(this.token.authTokenIssueTime).getTime() / 1000;
108 expiry = issue + this.token.authTokenTTL;
109 timeToExpiry = expiry - now;
110 if (timeToExpiry > 0) {
111 return timeToExpiry;
112 } else {
113 return 0;
114 }
115 };
116 Auth.prototype.updateHeaders = function() {
117 var current;
118 current = this.element.data('annotator:headers');
119 return this.element.data('annotator:headers', $.extend(current, {
120 'x-annotator-auth-token': this.token.authToken,
121 'x-annotator-auth-token-issue-time': this.token.authTokenIssueTime,
122 'x-annotator-auth-token-ttl': this.token.authTokenTTL,
123 'x-annotator-account-id': this.token.accountId,
124 'x-annotator-user-id': this.token.userId
125 }));
126 };
127 Auth.prototype.withToken = function(callback) {
128 if (!(callback != null)) {
129 return;
130 }
131 if (this.haveValidToken()) {
132 return callback();
133 } else {
134 this.waitingForToken.push(callback);
135 if (!this.requestInProgress) {
136 return this.requestToken();
137 }
138 }
139 };
140 return Auth;
141 })();
142}).call(this);
Note: See TracBrowser for help on using the repository browser.