From 9acf7495e39d32141fcc17b8dfa6e92be231899b Mon Sep 17 00:00:00 2001 From: Truong Hoang Dung Date: Thu, 13 Sep 2018 15:17:26 +0700 Subject: [PATCH 1/3] Provide dataPath per get/set call. This is nessessary for user to set custom namespace/claims in JWT token. --- src/jwt-server.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/jwt-server.js b/src/jwt-server.js index 3d7c279..5a99638 100644 --- a/src/jwt-server.js +++ b/src/jwt-server.js @@ -25,7 +25,7 @@ import { import type {SessionDeps, SessionService} from './types.js'; // Scope path to `data.` here since `jsonwebtoken` has some special top-level keys that we do not want to expose (ex: `exp`) -const getFullPath = keyPath => `data.${keyPath}`; +const getFullPath = (keyPath, dataPath) => `${dataPath}.${keyPath}`; type JWTConfig = { secret: string, @@ -52,19 +52,29 @@ class JWTSession { } return this.token; } - get(keyPath: string): mixed { + get(keyPath: string, dataPath: string = ''): mixed { assert( this.token, "Cannot access token before loaded, please use this plugin before any of it's dependencies" ); - return get(this.token, getFullPath(keyPath)); + if (datPath === '') { + return get(this.token, keyPath); + } else { + return get(this.token, getFullPath(keyPath)); + } + } - set(keyPath: string, val: mixed): boolean { + set(keyPath: string, val: mixed, dataPath: string = ''): boolean { assert( this.token, "Cannot access token before loaded, please use this plugin before any of it's dependencies" ); - return set(this.token, getFullPath(keyPath), val); + if (datPath === '') { + return set(this.token, keyPath, val); + } else { + return set(this.token, getFullPath(keyPath, dataPath), val); + } + } } From b47003e04179b05a1a708f4a514184a15ce4fc73 Mon Sep 17 00:00:00 2001 From: Truong Hoang Dung Date: Thu, 13 Sep 2018 15:20:33 +0700 Subject: [PATCH 2/3] Fix syntax error --- src/jwt-server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/jwt-server.js b/src/jwt-server.js index 5a99638..5c1111e 100644 --- a/src/jwt-server.js +++ b/src/jwt-server.js @@ -57,10 +57,10 @@ class JWTSession { this.token, "Cannot access token before loaded, please use this plugin before any of it's dependencies" ); - if (datPath === '') { + if (dataPath === '') { return get(this.token, keyPath); } else { - return get(this.token, getFullPath(keyPath)); + return get(this.token, getFullPath(keyPath, dataPath)); } } @@ -69,7 +69,7 @@ class JWTSession { this.token, "Cannot access token before loaded, please use this plugin before any of it's dependencies" ); - if (datPath === '') { + if (dataPath === '') { return set(this.token, keyPath, val); } else { return set(this.token, getFullPath(keyPath, dataPath), val); From 132f188c7d0f493611997b95dfcbd2b47a3c6c4e Mon Sep 17 00:00:00 2001 From: Truong Hoang Dung Date: Thu, 13 Sep 2018 20:43:06 +0700 Subject: [PATCH 3/3] Fix types --- src/types.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.js b/src/types.js index 904b1c2..f53c232 100644 --- a/src/types.js +++ b/src/types.js @@ -19,8 +19,8 @@ export type SessionService = { ctx: Context ): { loadToken(): Promise, - get(keyPath: string): mixed, - set(keyPath: string, val: mixed): boolean, + get(keyPath: string, dataPath: string.optional): mixed, + set(keyPath: string, val: mixed, dataPath: string.optional): boolean, cookie: string | void, }, };