From 8c5509ceeaa8fc1d1bf5e05a0d78fdde555dd6e5 Mon Sep 17 00:00:00 2001 From: Guiqiang Zhang Date: Tue, 25 Jul 2017 09:16:48 +0800 Subject: [PATCH 1/2] check v3 token expiration --- initializers/v3client.js | 3 ++- package.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/initializers/v3client.js b/initializers/v3client.js index df8312a37..b1e620288 100644 --- a/initializers/v3client.js +++ b/initializers/v3client.js @@ -12,6 +12,7 @@ var request = require('request'); var _ = require('underscore'); var async = require('async'); +var tcAccounts = require('tc-accounts'); /** * The URL of the V3 API @@ -63,7 +64,7 @@ function getToken(connection, callback) { return; } // Cached token - if (!_.isUndefined(tokens[connection.authToken])) { + if (!_.isUndefined(tokens[connection.authToken]) && !tcAccounts.isTokenExpired(tokens[connection.authToken])) { callback(null, tokens[connection.authToken]); return; } diff --git a/package.json b/package.json index c3e6dfc2a..84605d1dd 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ "validator": "~3.5.0", "wkhtmltoimage": ">= 0.1.3", "xml2js": "0.2.x", - "xtend": "2.1.x" + "xtend": "2.1.x", + "tc-accounts": "https://github.com/appirio-tech/accounts-app#dev" }, "devDependencies": { "supertest": "0.8.x", From c50710c02b82c8147756007b04fd7095879270b8 Mon Sep 17 00:00:00 2001 From: Guiqiang Zhang Date: Tue, 25 Jul 2017 10:43:23 +0800 Subject: [PATCH 2/2] update check token expiration logic --- initializers/v3client.js | 66 ++++++++++++++++++++++++++++++++++++++-- package.json | 2 +- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/initializers/v3client.js b/initializers/v3client.js index b1e620288..21db566dc 100644 --- a/initializers/v3client.js +++ b/initializers/v3client.js @@ -12,7 +12,7 @@ var request = require('request'); var _ = require('underscore'); var async = require('async'); -var tcAccounts = require('tc-accounts'); +var atob = require('atob'); /** * The URL of the V3 API @@ -64,7 +64,7 @@ function getToken(connection, callback) { return; } // Cached token - if (!_.isUndefined(tokens[connection.authToken]) && !tcAccounts.isTokenExpired(tokens[connection.authToken])) { + if (!_.isUndefined(tokens[connection.authToken]) && !isTokenExpired(tokens[connection.authToken])) { callback(null, tokens[connection.authToken]); return; } @@ -87,6 +87,68 @@ function getToken(connection, callback) { }); } +function urlBase64Decode(str) { + var output = str.replace(/-/g, '+').replace(/_/g, '/'); + + switch (output.length % 4) { + case 0: + break; + + case 2: + output += '=='; + break; + + case 3: + output += '='; + break; + + default: + throw 'Illegal base64url string!' + } + return decodeURIComponent(escape(atob(output)));//polyfill https://github.com/davidchambers/Base64.js +} + +function decodeToken(token) { + var parts = token.split('.'); + + if (parts.length !== 3) { + throw new Error('The token is invalid') + } + + var decoded = urlBase64Decode(parts[1]); + + if (!decoded) { + throw new Error('Cannot decode the token') + } + + return JSON.parse(decoded) +} + +function getTokenExpirationDate(token) { + var decoded = decodeToken(token); + + if(typeof decoded.exp === 'undefined') { + return null + } + + var d = new Date(0);// The 0 here is the key, which sets the date to the epoch + d.setUTCSeconds(decoded.exp); + + return d +} + +function isTokenExpired(token) { + var d = getTokenExpirationDate(token); + + if (d === null) { + return false + } + + // Token expired? + return !(d.valueOf() > (new Date().valueOf())) +} + + /** * Get IDs of users in the specified group * diff --git a/package.json b/package.json index 84605d1dd..d5470d6df 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "wkhtmltoimage": ">= 0.1.3", "xml2js": "0.2.x", "xtend": "2.1.x", - "tc-accounts": "https://github.com/appirio-tech/accounts-app#dev" + "atob": "2.0.3" }, "devDependencies": { "supertest": "0.8.x", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy