-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth-store.html
186 lines (162 loc) · 4.71 KB
/
auth-store.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../promise-polyfill/promise-polyfill-lite.html">
<script src="../jwt-decode/build/jwt-decode.min.js"></script>
<script>
(function() {
'use strict';
var state = {
data: null,
token: null,
refresh: function() {
return Promise.reject('no token refresh function set via <auth-store token-refresh="fn" ...>');
},
promise: null,
subscribers: []
};
/**
* `Polymer.AuthTokenStoreBehavior` manages access to the JWT auth token
*
* @polymerBehavior Polymer.AuthTokenStoreBehavior
*/
Polymer.AuthTokenStoreBehavior = {
// return a promise to the auth token
getAuthToken: function() {
if (!state.promise) {
state.promise = state.refresh();
}
return state.promise;
}
};
/**
* `Polymer.AuthTokenReadBehavior` provides read-access to the auth token
*
* @polymerBehavior Polymer.AuthTokenReadBehavior
*/
Polymer.AuthTokenReadBehavior = {
properties: {
// decoded JWT token data
data: {
type: Object,
readOnly: true,
notify: true,
value: function() {
return state.data;
}
}
},
attached: function() {
state.subscribers.push(this);
},
detached: function() {
var index = state.subscribers.indexOf(this);
if (index > -1) {
state.subscribers.splice(index, 1);
}
}
};
/**
* `auth-store` enables the app to set the token refresh function
*
* @demo demo/index.html
*/
Polymer({
is: 'auth-store',
hostAttributes: {
hidden: true
},
properties: {
// JWT access token
token: {
type: String,
observer: '_tokenChanged'
},
// decoded JWT token data
data: {
type: Object,
readOnly: true,
notify: true
},
// token expiry timestamp
expiry: {
type: Date,
readOnly: true,
notify: true,
computed: '_computeExpiry(data)'
},
// token expiry offset to allow for clock skew
offset: {
type: Number,
value: 300
},
// whether the token is valid
isValid: {
type: Boolean,
readOnly: true,
notify: true
}
},
observers: [
'_expiryChanged(expiry, offset)'
],
// set the token refresh function
setTokenRefresh: function(fn) {
state.refresh = this.refreshToken(fn);
},
// refresh the access token
refreshToken: function(fn) {
return function() {
if (this.isValid) {
return Promise.resolve(this.token);
}
return fn().then(function(token) {
this.token = token;
return token;
}.bind(this));
}.bind(this);
},
// decodes JWT token data and updates instances
_tokenChanged: function(newVal, oldVal) {
state.token = this.token;
state.data = this.token ? jwt_decode(this.token) : null;
this._setData(state.data);
// notify subscribers
for (var i = 0; i < state.subscribers.length; i++) {
state.subscribers[i]._setData(state.data);
}
},
// compute the token expiry time from the JWT data
_computeExpiry: function(data) {
if (data === null || typeof data.exp === 'undefined') {
return null;
}
var date = new Date(0);
date.setUTCSeconds(data.exp);
return date;
},
// set a timeout function to clear the token valid state when expired
_expiryChanged: function(expiry, offset) {
if (this.timeout) window.clearTimeout(this.timeout);
if (expiry === null) return;
var countdown = expiry.valueOf() - new Date().valueOf() - (offset * 1000);
var seconds = Math.floor(countdown / 1000);
var minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
console.log('token valid for', minutes + 'm', seconds + 's');
if (countdown < 0) {
this._setIsValid(false);
this._tokenExpired();
} else {
this._setIsValid(true);
this.timeout = window.setTimeout(this._tokenExpired.bind(this), countdown);
}
},
// clear the token valid flag and reset the promise to force a token refresh
// for any future AJAX auth operations
_tokenExpired: function() {
console.log('token expired');
this._setIsValid(false);
state.promise = null;
}
});
})();
</script>