-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDPA-2689] Store authentication token more securely. (#406)
* [SDPA-2689] Authenticated token fixes * Token to be store in cookies * Token removed from Vuex Store (as this will output to HTML) * Authenticated state (true / false) to be stored in Vuex. * [SDPA-2689] Split preview and authenticate functions into separate libs. * [SDPA-2689] Move vuex store management into authenticate lib. * [SDPA-2689] Extract and set cookie name to authenticatedContent. * [SDPA-2689] Add authenticatedContent module enabled checks. * [SDPA-2689] Add authenticated preview tests. * [SDPA-2689] Fixed test. Added preview role to created used. * [SDPA-2689] Lint fixes. * [SDPA-2689] isModuleEnabled to return false if no config available.
- Loading branch information
Showing
19 changed files
with
328 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/ripple-nuxt-tide/modules/authenticated-content/lib/authenticate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import cookieparser from 'cookieparser' | ||
import Cookie from 'js-cookie' | ||
|
||
const authCookieName = 'authenticatedContent' | ||
let serverToken = null | ||
|
||
/** | ||
* Decode a JWT token and test exipration date. | ||
* @param {String} token JWT token | ||
* @return {Boolean} is expired | ||
*/ | ||
function isTokenExpired (token) { | ||
if (token) { | ||
const jwtDecode = require('jwt-decode') | ||
const { exp } = jwtDecode(token) | ||
// Token expiry timestamp is in a shorter format, match them for comparison | ||
const now = parseInt(Date.now().toString().slice(0, exp.toString().length)) | ||
return exp < now | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
/** | ||
* Client / Server use. | ||
* Get auth token. | ||
* @return {String} auth token | ||
*/ | ||
function getToken () { | ||
if (process.client) { | ||
return Cookie.get(authCookieName) | ||
} else { | ||
return serverToken | ||
} | ||
} | ||
|
||
/** | ||
* Client / Server use. | ||
* Clear auth token. | ||
* @param {Object} store vuex store object | ||
*/ | ||
function clearToken (store) { | ||
if (process.client) { | ||
Cookie.remove(authCookieName) | ||
} else { | ||
serverToken = null | ||
} | ||
store.dispatch('tideAuthenticatedContent/setAuthenticated', false) | ||
} | ||
|
||
/** | ||
* Client use only. | ||
* Store auth token in cookies. | ||
* @param {String} token JWT token | ||
* @param {Object} store vuex store object | ||
*/ | ||
function clientSetToken (token, store) { | ||
Cookie.set(authCookieName, token) | ||
store.dispatch('tideAuthenticatedContent/setAuthenticated', true) | ||
} | ||
|
||
/** | ||
* Server use only. | ||
* Store request header auth token to memory for page rendering. | ||
* @param {Object} cookies Request header cookies | ||
* @param {Object} store vuex store object | ||
*/ | ||
function serverSetToken (cookies, store) { | ||
let isAuth = false | ||
if (cookies) { | ||
const parsed = cookieparser.parse(cookies) | ||
if (parsed[authCookieName]) { | ||
if (!isTokenExpired(parsed[authCookieName])) { | ||
serverToken = parsed[authCookieName] | ||
isAuth = true | ||
} | ||
} | ||
} | ||
store.dispatch('tideAuthenticatedContent/setAuthenticated', isAuth) | ||
} | ||
|
||
/** | ||
* Check if current user is authenticated. | ||
* @param {Object} store vuex store object | ||
* @return {Boolean} is user authenticated | ||
*/ | ||
function isAuthenticated (store) { | ||
return store.state.tideAuthenticatedContent.isAuthenticated | ||
} | ||
|
||
export { isTokenExpired } | ||
export { getToken } | ||
export { clearToken } | ||
export { clientSetToken } | ||
export { serverSetToken } | ||
export { isAuthenticated } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.