Skip to content

Commit

Permalink
fix(xuinode): fixed dynamic import issue when compiling with webpack (#…
Browse files Browse the repository at this point in the history
…75)

Also fixed issue with cached authenticate method of wrapper
  • Loading branch information
paddybasi committed Jun 12, 2020
1 parent d071d27 commit 215f80e
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"test": "jest --env node",
"test:coverage": "jest --env node --coverage",
"build": "tsc",
"build": "tsc --declarationMap --sourceMap",
"lint": "eslint \"*/**/*.{js,ts}\" --quiet --fix",
"commit": "git cz",
"ci:lint": "eslint \"*/**/*.{js,ts}\" --quiet",
Expand Down
2 changes: 1 addition & 1 deletion src/auth/oauth2/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as oauth2 } from './oauth2.class'
export { oauth2 } from './oauth2.class'
export { OAuth2Metadata } from './OAuth2Metadata.interface'
export { XUIOAuth2Strategy } from './XUIOAuth2Strategy.class'
2 changes: 1 addition & 1 deletion src/auth/oauth2/models/oauth2.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import oauth2 from './oauth2.class'
import { oauth2 } from './oauth2.class'

test('OIDC Auth', () => {
expect(oauth2).toBeDefined()
Expand Down
2 changes: 1 addition & 1 deletion src/auth/oauth2/models/oauth2.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ export class OAuth2 extends Strategy {
}
}

export default new OAuth2()
export const oauth2 = new OAuth2()
2 changes: 1 addition & 1 deletion src/auth/oidc/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as oidc } from './openid.class'
export { oidc } from './openid.class'
export { OpenIDMetadata } from './OpenIDMetadata.interface'
2 changes: 1 addition & 1 deletion src/auth/oidc/models/openid.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import oidc, { OpenID } from './openid.class'
import { oidc, OpenID } from './openid.class'
import passport from 'passport'
import { Request, Response, NextFunction, Router } from 'express'
import { AUTH } from '../../auth.constants'
Expand Down
2 changes: 1 addition & 1 deletion src/auth/oidc/models/openid.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,4 @@ export class OpenID extends AuthStrategy {
}
}

export default new OpenID()
export const oidc = new OpenID()
2 changes: 1 addition & 1 deletion src/auth/s2s/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { DecodedJWT } from './decodedJwt.interface'
export { default as s2s } from './s2s.class'
export { s2s } from './s2s.class'
export { S2S } from './s2s.constants'
export { S2SConfig } from './s2sConfig.interface'
export { S2SToken } from './s2sToken.interface'
2 changes: 1 addition & 1 deletion src/auth/s2s/s2s.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ export class S2SAuth extends EventEmitter {
}
}

export default new S2SAuth()
export const s2s = new S2SAuth()
2 changes: 1 addition & 1 deletion src/common/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as xuiNode } from './xuiNode.class'
export { xuiNode } from './xuiNode.class'
export { XuiNodeOptions } from './xuiNodeOptions.interface'
4 changes: 2 additions & 2 deletions src/common/models/xuiNode.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import xuiNode, { XuiNode } from './xuiNode.class'
import { xuiNode, XuiNode } from './xuiNode.class'
import { Router } from 'express'
import { XuiNodeOptions } from './xuiNodeOptions.interface'

Expand All @@ -25,5 +25,5 @@ test('applyMiddlewareLayer() should call importMiddleware with the baseDir and m

xuiNode.applyMiddleware(middleware, xuiNodeOptions)

expect(spyOnImportMiddleware).toHaveBeenCalledWith(expect.any(String), middleware)
expect(spyOnImportMiddleware).toHaveBeenCalledWith(middleware)
})
34 changes: 23 additions & 11 deletions src/common/models/xuiNode.class.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { EventEmitter } from 'events'
import { NextFunction, Request, RequestHandler, Response, Router } from 'express'
import { XuiNodeOptions } from './xuiNodeOptions.interface'
import * as path from 'path'
import { hasKey } from '../util'
import { AUTH } from '../../auth/auth.constants'
import { XuiNodeMiddlewareInterface } from './xuiNodeMiddleware.interface' // NOTE: do not shorten this path, tests fail
import { AUTH } from '../../auth/auth.constants' // NOTE: do not shorten this path, tests fail
import { XuiNodeMiddlewareInterface } from './xuiNodeMiddleware.interface'

export class XuiNode extends EventEmitter {
protected readonly router: Router
protected readonly middlewares: Array<string>
public authenticateMiddleware: any
public constructor(
router: Router = Router({ mergeParams: true }),
// deliberately done it this way as we need session first
Expand All @@ -19,6 +19,11 @@ export class XuiNode extends EventEmitter {
this.middlewares = middlewares
}

public authenticate = (req: Request, res: Response, next: NextFunction): void => {
const authMiddleware = this.authenticateMiddleware ? this.authenticateMiddleware : this.authenticateDefault
authMiddleware(req, res, next)
}

/**
* the proxied authenticate method which is publicly exposed
* what constitutes a user being unauthenticated?
Expand All @@ -27,7 +32,8 @@ export class XuiNode extends EventEmitter {
* @param res
* @param next
*/
public authenticate = (req: Request, res: Response, next: NextFunction): void => {
public authenticateDefault = (req: Request, res: Response, next: NextFunction): void => {
console.log('xuiNode AUTHENTICATE CALLED!!!!!')
if (req.isUnauthenticated()) {
console.log('unauthenticated, redirecting')
return res.redirect(AUTH.ROUTE.LOGIN)
Expand All @@ -43,18 +49,24 @@ export class XuiNode extends EventEmitter {
/**
* Import a middleware layer.
*
* @param {string} baseDir - ie. /Users/username/projects/rpx-xui-node-lib/src/
* @param {string} middleware - ie. 's2s'
* @return {Promise<any>}
*/
public importMiddleware = async (baseDir: string, middleware: string) =>
await import(path.join(baseDir, middleware))
public importMiddleware = async (middleware: string) => {
switch (middleware) {
case 'auth':
return await import('../../auth')
case 'session':
return await import('../../session')
default:
throw new Error('unknown middleware')
}
}

public applyMiddleware = async (middleware: string, options: XuiNodeOptions): Promise<void> => {
if (hasKey(options, middleware)) {
const baseDir = path.join(__dirname, '../../')
const middlewareLayerOptions = options[middleware]
const middlewareLayer = await this.importMiddleware(baseDir, middleware)
const middlewareLayer = await this.importMiddleware(middleware)
this.applyMiddlewareLayer(middlewareLayer, middlewareLayerOptions)
}
}
Expand All @@ -65,7 +77,7 @@ export class XuiNode extends EventEmitter {
const middleware = middlewareLayer[key]
this.proxyEvents(middleware)
if (hasKey(middleware, 'authenticate')) {
this.authenticate = middleware.authenticate
this.authenticateMiddleware = middleware.authenticate
}
this.router.use(middleware.configure(value))
}
Expand All @@ -86,4 +98,4 @@ export class XuiNode extends EventEmitter {
}
}

export default new XuiNode()
export const xuiNode = new XuiNode()
4 changes: 2 additions & 2 deletions src/session/models/fileSessionStore.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createMock } from 'ts-auto-mock'

import fileSessionStore from './fileSessionStore.class'
import { fileStore } from './fileSessionStore.class'
import { FileSessionMetadata } from './sessionMetadata.interface'

describe('getStore()', () => {
it('should return', () => {
const fileSessionMetadata = createMock<FileSessionMetadata>()
expect(fileSessionStore.getStore(fileSessionMetadata)).toBeDefined()
expect(fileStore.getStore(fileSessionMetadata)).toBeDefined()
})
})
2 changes: 1 addition & 1 deletion src/session/models/fileSessionStore.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export class FileSessionStore extends SessionStore {
}
}

export default new FileSessionStore()
export const fileStore = new FileSessionStore()
4 changes: 2 additions & 2 deletions src/session/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { SessionMetadata } from './sessionMetadata.interface'
export { default as redisStore } from './redisSessionStore.class'
export { default as fileStore } from './fileSessionStore.class'
export { redisStore } from './redisSessionStore.class'
export { fileStore } from './fileSessionStore.class'
2 changes: 1 addition & 1 deletion src/session/models/redisSessionStore.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ export class RedisSessionStore extends SessionStore {
}
}

export default new RedisSessionStore()
export const redisStore = new RedisSessionStore()

0 comments on commit 215f80e

Please sign in to comment.