@@ -24,7 +24,7 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
24
24
} ,
25
25
} ;
26
26
27
- const TOKEN_AGE = 14 ; // days
27
+ const DEFAULT_TOKEN_LIFE = 14 * 24 * 60 * 60 * 1000 ; // millis
28
28
29
29
/**
30
30
* Meant for managing auth or session cookies (mainly auth cookies as set by
@@ -33,10 +33,10 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
33
33
* If it's missing name or value, does nothing.
34
34
* Default expiration time is 14 days.
35
35
* domain, expires, and max-age are optional
36
- * expires is expected to be in days
37
- * auto set fields are:
36
+ * expires is expected to be the timestamp (ms since epoch) it will expire
37
+ * default fields are:
38
38
* - path = '/'
39
- * - expires = TOKEN_AGE (default 14) days
39
+ * - expires = now + 14 days
40
40
* @param {object } cookie
41
41
* - has the cookie keys: name, value, path, expires, max-age, domain
42
42
* - adds secure=true, samesite=Lax for KBase use.
@@ -50,7 +50,7 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
50
50
const name = encodeURIComponent ( cookie . name ) ;
51
51
const value = encodeURIComponent ( cookie . value || '' ) ;
52
52
const props = {
53
- expires : TOKEN_AGE , // gets translated to GMT string
53
+ expires : Date . now ( ) + DEFAULT_TOKEN_LIFE , // gets translated to GMT string
54
54
path : '/' ,
55
55
samesite : 'Lax' ,
56
56
} ;
@@ -66,14 +66,8 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
66
66
if ( cookie . domain ) {
67
67
props . domain = cookie . domain ;
68
68
}
69
- props [ 'max-age' ] = 86400 * props . expires ;
70
- if ( props . expires === 0 ) {
71
- props . expires = new Date ( 0 ) . toUTCString ( ) ;
72
- } else {
73
- props . expires = new Date (
74
- new Date ( ) . getTime ( ) + 86400000 * props . expires
75
- ) . toUTCString ( ) ;
76
- }
69
+ props [ 'max-age' ] = parseInt ( ( props . expires - Date . now ( ) ) / 1000 ) ;
70
+ props . expires = new Date ( props . expires ) . toUTCString ( ) ;
77
71
78
72
const fields = Object . keys ( props ) . map ( ( key ) => {
79
73
return `${ key } =${ props [ key ] } ` ;
@@ -85,7 +79,7 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
85
79
86
80
const propStr = fields . join ( ';' ) ;
87
81
88
- const newCookie = `${ name } =${ value } ; ${ propStr } ` ;
82
+ const newCookie = `${ name } =${ value } ;${ propStr } ` ;
89
83
document . cookie = newCookie ;
90
84
}
91
85
@@ -161,22 +155,34 @@ define(['bluebird', 'jquery', 'narrativeConfig'], (Promise, $, Config) => {
161
155
return getCookie ( cookieConfig . auth . name ) ;
162
156
}
163
157
164
- /* Sets the given auth token into the browser's cookie.
165
- * Does nothing if the token is null.
158
+ /**
159
+ * Returns a Promise that ets the given auth token into the
160
+ * browser's cookie, as configured. The cookie has the
161
+ * same lifespan as the token.
162
+ * If the token is null or expired, this does nothing.
163
+ * If there's an error in looking up the token, this throws
164
+ * an error.
165
+ * @param {string } token
166
+ * @returns
166
167
*/
167
- function setAuthToken ( token ) {
168
+ async function setAuthToken ( token ) {
169
+ const tokenInfo = await getTokenInfo ( token ) ;
170
+ // if it's expired, don't set (actually should've thrown
171
+ // here, and get caught by the caller, but check anyway)
172
+ if ( tokenInfo . expires - Date . now ( ) <= 0 ) {
173
+ return ;
174
+ }
168
175
const deployEnv = Config . get ( 'environment' ) ;
169
176
170
177
function setToken ( config ) {
171
178
// Honor cookie host whitelist if present.
172
- if ( config . enableIn ) {
173
- if ( config . enableIn . indexOf ( deployEnv ) === - 1 ) {
174
- return ;
175
- }
179
+ if ( config . enableIn && ! config . enableIn . includes ( deployEnv ) ) {
180
+ return ;
176
181
}
177
182
const cookieField = {
178
183
name : config . name ,
179
184
value : token ,
185
+ expires : tokenInfo . expires ,
180
186
} ;
181
187
if ( config . domain ) {
182
188
cookieField . domain = config . domain ;
0 commit comments