Skip to content

Commit

Permalink
replaced config.get with State.getInstance().config
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-chripko committed Apr 13, 2023
1 parent 0c97b13 commit 5aa1fa0
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 179 deletions.
78 changes: 38 additions & 40 deletions config/default.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
import { ExtractJwt } from 'passport-jwt'
import { IPassportJwtWrapperConfig, IPassportConfig } from '../src/types/config'

export default {
passportJwtWrapper: <Partial<IPassportJwtWrapperConfig>>{
checkAccessToken: false,
i18next: {
preload: ['en', 'sk'],
fallbackLng: 'en',
ns: ['error', 'translation'],
defaultNS: 'translation',
detection: {
order: ['header']
},
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json',
jsonIndent: 2
},
nsSeparator: ':',
keySeparator: false
export default <IPassportJwtWrapperConfig>{
checkAccessToken: false,
i18next: {
preload: ['en', 'sk'],
fallbackLng: 'en',
ns: ['error', 'translation'],
defaultNS: 'translation',
detection: {
order: ['header']
},
passport: <IPassportConfig>{
local: {
usernameField: 'email',
passwordField: 'password',
session: false,
passReqToCallback: true
},
jwt: {
secretOrKey: process.env.JWT_SECRET,
api: {
exp: '15m',
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), ExtractJwt.fromUrlQueryParameter('t')]),
refresh: {
exp: '4h'
}
},
passwordReset: {
exp: '4h',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
},
invitation: {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
exp: '30d'
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json',
jsonIndent: 2
},
nsSeparator: ':',
keySeparator: false
},
passport: <IPassportConfig>{
local: {
usernameField: 'email',
passwordField: 'password',
session: false,
passReqToCallback: true
},
jwt: {
secretOrKey: process.env.JWT_SECRET,
api: {
exp: '15m',
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), ExtractJwt.fromUrlQueryParameter('t')]),
refresh: {
exp: '4h'
}
},
passwordReset: {
exp: '4h',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
},
invitation: {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
exp: '30d'
}
}
}
Expand Down
50 changes: 0 additions & 50 deletions config/test.ts

This file was deleted.

12 changes: 6 additions & 6 deletions locales/en/error.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Access token is not valid": "Access token is not valid",
"Incorrect email or password": "Incorrect email or password",
"Invitation token is not valid": "Invitation token is not valid",
"Password reset token is invalid": "Password reset token is invalid",
"Refresh token is not valid": "Refresh token is not valid",
"User was not found": "User was not found"
"Access token is not valid": "Access token is not valid",
"Incorrect email or password": "Incorrect email or password",
"Invitation token is not valid": "Invitation token is not valid",
"Password reset token is invalid": "Password reset token is invalid",
"Refresh token is not valid": "Refresh token is not valid",
"User was not found": "User was not found"
}
12 changes: 6 additions & 6 deletions locales/sk/error.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Access token is not valid": "Autorizačný token nie je platný",
"Incorrect email or password": "Nesprávne meno alebo heslo",
"Invitation token is not valid": "Token pre pozvanie používateľa nie je platný",
"Password reset token is invalid": "Token pre zmenu hesla je neplatný",
"Refresh token is not valid": "Token pre obnovenie nie je platný",
"User was not found": "Používateľ neexistuje"
"Access token is not valid": "Autorizačný token nie je platný",
"Incorrect email or password": "Nesprávne meno alebo heslo",
"Invitation token is not valid": "Token pre pozvanie používateľa nie je platný",
"Password reset token is invalid": "Token pre zmenu hesla je neplatný",
"Refresh token is not valid": "Token pre obnovenie nie je platný",
"User was not found": "Používateľ neexistuje"
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"test": "NODE_ENV=test JWT_SECRET=somerandomsecret ts-mocha --config \"./.mocharc.js\"",
"test:debug": "NODE_ENV=test JWT_SECRET=somerandomsecret ts-mocha --config \"./.mocharc.js\" --inspect",
"test:coverage": "NODE_ENV=test JWT_SECRET=somerandomsecret nyc --reporter=lcov --temp-dir=\"./temp/.nyc_output\" ts-mocha --config \"./.mocharc.js\" --timeout=100000",
"test": "JWT_SECRET=somerandomsecret ts-mocha --config \"./.mocharc.js\"",
"test:debug": "JWT_SECRET=somerandomsecret ts-mocha --config \"./.mocharc.js\" --inspect",
"test:coverage": "JWT_SECRET=somerandomsecret nyc --reporter=lcov --temp-dir=\"./temp/.nyc_output\" ts-mocha --config \"./.mocharc.js\" --timeout=100000",
"lint": "eslint src tests --color --ext .js --ext .ts",
"lint:fix": "eslint src tests --color --ext .js --ext .ts --fix",
"lint:watch": "npm run lint -- --watch --ext .js --ext .ts",
"translate:scan": "NODE_ENV=test i18next-scanner 'src/**/*.ts'",
"translate:scan": "i18next-scanner 'src/**/*.ts'",
"prebuild": "rm -rf dist",
"build": "tsc"
},
Expand All @@ -27,6 +27,7 @@
"@types/bcrypt": "^5.0.0",
"@types/chai": "^4.3.4",
"@types/config": "^3.3.0",
"@types/deep-extend": "^0.6.0",
"@types/express": "^4.17.15",
"@types/i18next": "^13.0.0",
"@types/joi": "^17.2.3",
Expand Down Expand Up @@ -54,7 +55,6 @@
"nyc": "^15.1.0",
"passport": "0.6.0",
"prettier": "^2.8.3",
"rewiremock": "^3.14.5",
"supertest": "^6.3.3",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
Expand All @@ -63,6 +63,7 @@
"dependencies": {
"bcrypt": "^5.1.0",
"config": "3.3.7",
"deep-extend": "^0.6.0",
"express": "^4.18.2",
"i18next": "^22.4.9",
"joi": "^17.7.0",
Expand Down
6 changes: 2 additions & 4 deletions src/apiAuth/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Strategy as JwtStrategy, VerifiedCallback } from 'passport-jwt'
import config from 'config'
import { Request } from 'express'

import { IPassportConfig, IPassportJwtWrapperConfig } from '../types/config'
import { IJwtPayload } from '../types/interfaces'
import { State } from '../State'
import { JWT_AUDIENCE } from '../utils/enums'
Expand All @@ -16,7 +14,7 @@ import { customTFunction } from '../utils/translations'
* @param done
*/
export async function strategyVerifyFunction(req: Request, payload: IJwtPayload, done: VerifiedCallback) {
const libConfig: IPassportJwtWrapperConfig = config.get('passportJwtWrapper')
const libConfig = State.getInstance().config

try {
const user = await State.getInstance().userRepository.getUserById(payload.uid)
Expand Down Expand Up @@ -44,7 +42,7 @@ export async function strategyVerifyFunction(req: Request, payload: IJwtPayload,
* passport-jwt strategy for securing endpoints with access JWTs
*/
export function strategy() {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new JwtStrategy(
{
Expand Down
2 changes: 1 addition & 1 deletion src/invitation/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function getToken(userID: ID): Promise<string> {
uid: userID
}

const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport
const expiresIn = passportConfig.jwt.invitation.exp
const tokenOptions = {
audience: JWT_AUDIENCE.INVITATION,
Expand Down
2 changes: 1 addition & 1 deletion src/invitation/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const strategyVerifyFunction = async (req: Request, payload: IJwtPayload,
* User invitation strategy, needed for the guard to function.
*/
export function strategy() {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new JwtStrategy(
{
Expand Down
2 changes: 1 addition & 1 deletion src/login/getTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function getTokens(userID: ID, familyID?: ID, payload?: Record<stri
const rid = await state.refreshTokenRepository.createTokenID()
const fid = familyID ?? rid

const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

const [accessToken, refreshToken] = await Promise.all([
createJwt(
Expand Down
2 changes: 1 addition & 1 deletion src/login/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function strategyVerifyFunction(
* passport-local Strategy
*/
export function strategy() {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new Strategy(passportConfig.local, strategyVerifyFunction)
}
2 changes: 1 addition & 1 deletion src/passwordReset/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default async function getToken(email: string): Promise<[string, IUser<ID
const state = State.getInstance()
let user = await state.userRepository.getUserByEmail(email)

const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

let passportSecret = passportConfig.jwt.secretOrKey

Expand Down
4 changes: 2 additions & 2 deletions src/passwordReset/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function secretOrKeyProvider(req: Request, rawJwtToken: string, don
return done(null)
}

const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

const userSecret = `${passportConfig.jwt.secretOrKey}${user.hash}`
return done(null, userSecret)
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function strategyVerifyFunction(req: Request, payload: IJwtPayload,
* Password reset strategy
*/
export function strategy() {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new JwtStrategy(
{
Expand Down
5 changes: 3 additions & 2 deletions src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { JWT_AUDIENCE } from './enums'
import { ErrorBuilder } from './ErrorBuilder'
import { customTFunction } from './translations'
import { Flow } from './Flow'
import { State } from '../State'

/**
* Creates JWT token
Expand All @@ -18,7 +19,7 @@ import { Flow } from './Flow'
* @returns {Promise<string>} JWT token
*/
export function createJwt(payload: any, options: SignOptions, secret?: string): Promise<string> {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new Promise((resolve, reject) => {
sign(payload, secret || passportConfig.jwt.secretOrKey, options, (err, token) => {
Expand Down Expand Up @@ -48,7 +49,7 @@ export const createHash = async (password: string): Promise<string> => {
* @param tFunction
*/
export function verifyRefreshJWT(token: string, tFunction?: TFunction): Promise<IRefreshJwtPayload> {
const passportConfig: IPassportConfig = config.get('passportJwtWrapper.passport')
const passportConfig = State.getInstance().config.passport

return new Promise((resolve, reject) => {
jsonwebtoken.verify(
Expand Down
Loading

0 comments on commit 5aa1fa0

Please sign in to comment.