Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Create GetFullPathToken #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/jwt-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import {
SessionSecretToken,
SessionCookieNameToken,
SessionCookieExpiresToken,
GetFullPathToken,
} from './tokens.js';
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}`;

type JWTConfig = {
secret: string,
cookieName: string,
Expand All @@ -37,11 +35,13 @@ class JWTSession {
cookie: string | void;
token: ?Object | string;
config: JWTConfig;
getFullPath: string => string;

constructor(ctx: Context, config: JWTConfig) {
constructor(ctx: Context, config: JWTConfig, getFullPath: string => string) {
this.config = config;
this.cookie = ctx.cookies.get(this.config.cookieName);
this.token = null;
this.getFullPath = getFullPath
}
async loadToken() {
if (this.token == null) {
Expand All @@ -57,14 +57,14 @@ class JWTSession {
this.token,
"Cannot access token before loaded, please use this plugin before any of it's dependencies"
);
return get(this.token, getFullPath(keyPath));
return get(this.token, this.getFullPath(keyPath));
}
set(keyPath: string, val: mixed): 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);
return set(this.token, this.getFullPath(keyPath), val);
}
}

Expand All @@ -76,12 +76,13 @@ const p: FusionPlugin<SessionDeps, SessionService> =
secret: SessionSecretToken,
cookieName: SessionCookieNameToken,
expires: SessionCookieExpiresToken.optional,
getFullPath: GetFullPathToken.optional,
},
provides: deps => {
const {secret, cookieName, expires = 86400} = deps;
const {secret, cookieName, expires = 86400, getFullPath = (ctx: Context) => keyPath => `data.${keyPath}`} = deps;
const service: SessionService = {
from: memoize((ctx: Context) => {
return new JWTSession(ctx, {secret, cookieName, expires});
return new JWTSession(ctx, {secret, cookieName, expires, getFullPath(ctx)});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this is specified as part of config here, but specified as another constructor parameter in JWTSession. I'm assuming that since config is fairly session specific, that this should be another parameter here instead of in config.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinGrandon Agree, so in this case we will sepaprate geFullPath out of config object. Thanks.

}),
};
return service;
Expand Down
5 changes: 4 additions & 1 deletion src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @flow
*/

import type {Token} from 'fusion-core';
import type {Context, Token} from 'fusion-core';
import {createToken} from 'fusion-core';

export const SessionSecretToken: Token<string> = createToken('SessionSecret');
Expand All @@ -16,3 +16,6 @@ export const SessionCookieNameToken: Token<string> = createToken(
export const SessionCookieExpiresToken: Token<number> = createToken(
'SessionCookieExpires'
);
export const GetFullPathToken: Token<(ctx: Context) => string => string)> = createToken(
'GetFullPathToken'
);