From f5056ce9de726b369f41d57a5fcba38053fdcca9 Mon Sep 17 00:00:00 2001 From: doktordirk Date: Tue, 31 May 2016 08:25:45 +0200 Subject: [PATCH] feat(authentication): use jwt-decode to decode token --- config.js | 5 +++++ package.json | 9 ++++++--- src/authentication.js | 17 ++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/config.js b/config.js index d7c1331..43b9435 100644 --- a/config.js +++ b/config.js @@ -19,6 +19,7 @@ System.config({ "aurelia-router": "npm:aurelia-router@1.0.0-beta.1.2.3", "extend": "npm:extend@3.0.0", "fetch": "github:github/fetch@0.11.1", + "jwt-decode": "npm:jwt-decode@2.0.1", "npm:aurelia-api@3.0.0-rc2": { "aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-beta.1.2.3", "aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0-beta.1.2.5", @@ -52,6 +53,10 @@ System.config({ "aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.1", "aurelia-path": "npm:aurelia-path@1.0.0-beta.1.2.2", "aurelia-route-recognizer": "npm:aurelia-route-recognizer@1.0.0-beta.1.2.1" + }, + "npm:jwt-decode@2.0.1": { + "fs": "github:jspm/nodelibs-fs@0.1.2", + "systemjs-json": "github:systemjs/plugin-json@0.1.2" } } }); diff --git a/package.json b/package.json index fb33220..3c1e896 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "aurelia-pal": "^1.0.0-beta.1.2.2", "aurelia-path": "^1.0.0-beta.1.2.2", "aurelia-router": "^1.0.0-beta.1.2.3", - "extend": "^3.0.0" + "extend": "^3.0.0", + "jwt-decode": "^2.0.1" }, "peerDependencies": { "aurelia-api": "^3.0.0-rc2", @@ -49,7 +50,8 @@ "aurelia-pal": "^1.0.0-beta.1.2.2", "aurelia-path": "^1.0.0-beta.1.2.2", "aurelia-router": "^1.0.0-beta.1.2.3", - "extend": "^3.0.0" + "extend": "^3.0.0", + "jwt-decode": "^2.0.1" }, "devDependencies": { "aurelia-pal-browser": "^1.0.0-beta.2.0.1", @@ -66,7 +68,8 @@ "aurelia-pal": "^1.0.0-beta.1.2.2", "aurelia-path": "^1.0.0-beta.1.2.2", "aurelia-router": "^1.0.0-beta.1.2.3", - "extend": "^3.0.0" + "extend": "^3.0.0", + "jwt-decode": "^2.0.1" }, "devDependencies": { "aurelia-tools": "^0.1.20", diff --git a/src/authentication.js b/src/authentication.js index 33375b9..82d022d 100644 --- a/src/authentication.js +++ b/src/authentication.js @@ -1,6 +1,7 @@ import {PLATFORM} from 'aurelia-pal'; import {inject} from 'aurelia-dependency-injection'; import {deprecated} from 'aurelia-metadata'; +import jwtDecode from 'jwt-decode'; import * as LogManager from 'aurelia-logging'; import {BaseConfig} from './baseConfig'; @@ -146,19 +147,13 @@ export class Authentication { } } - let payload = null; + this.payload = null; - if (this.accessToken && this.accessToken.split('.').length === 3) { - try { - const base64 = this.accessToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'); - payload = JSON.parse(decodeURIComponent(escape(atob(base64)))); - } catch (e) { - payload = null; - } - } + try { + this.payload = this.accessToken ? jwtDecode(this.accessToken) : null; + } catch (_) {} - this.payload = payload; - this.exp = payload ? parseInt(payload.exp, 10) : NaN; + this.exp = this.payload ? parseInt(this.payload.exp, 10) : NaN; this.hasDataStored = true;