Skip to content

Commit

Permalink
Merge pull request #59 from GoodRequest/no-config
Browse files Browse the repository at this point in the history
No config
  • Loading branch information
juraj-chripko authored Apr 20, 2023
2 parents cd4b21f + 49ee55a commit 3a151f6
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 207 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ coverage
temp
.npmrc
codecov
/.npmrc
i18next-scanner.config.js

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.

3 changes: 2 additions & 1 deletion i18next-scanner.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = require('config')
const defaultConfigs = require('./config/default')

const i18nextConfig = config.get('passportJwtWrapper.i18next')
const i18nextConfig = config.has('i18next') ? config.get('i18next') : defaultConfigs.i18next

module.exports = {
input: [
Expand Down
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"
}
20 changes: 18 additions & 2 deletions package-lock.json

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

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "@goodrequest/passport-jwt-wrapper",
"version": "1.5.1-0",
"version": "1.6.0",
"description": "",
"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
7 changes: 6 additions & 1 deletion src/State.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { PassportStatic } from 'passport'
import { ID, IUserRepository, IRefreshTokenRepository, IInvitationTokenRepository, IPasswordResetTokenRepository } from './types/interfaces'
import { IPassportJwtWrapperConfig } from './types/config'

/**
* Internal class for keeping state,
Expand All @@ -18,30 +19,34 @@ export class State<T extends ID, U extends ID> {
}

static initialize<T extends ID, U extends ID>(
config: IPassportJwtWrapperConfig,
passportStatic: PassportStatic,
userRepository: IUserRepository<U>,
refreshTokenRepository: IRefreshTokenRepository<T, U>,
invitationTokenRepository?: IInvitationTokenRepository<U>,
passwordResetTokenRepository?: IPasswordResetTokenRepository<U>
): State<T, U> {
this.instance = new State<T, U>(passportStatic, userRepository, refreshTokenRepository, invitationTokenRepository, passwordResetTokenRepository)
this.instance = new State<T, U>(config, passportStatic, userRepository, refreshTokenRepository, invitationTokenRepository, passwordResetTokenRepository)

return this.instance
}

readonly passport: PassportStatic
readonly userRepository: IUserRepository<U>
readonly config: IPassportJwtWrapperConfig
readonly refreshTokenRepository: IRefreshTokenRepository<T, U>
readonly invitationTokenRepository?: IInvitationTokenRepository<U>
readonly passwordResetTokenRepository?: IPasswordResetTokenRepository<U>

private constructor(
config: IPassportJwtWrapperConfig,
passportStatic: PassportStatic,
userRepository: IUserRepository<U>,
refreshTokenRepository: IRefreshTokenRepository<T, U>,
invitationTokenRepository?: IInvitationTokenRepository<U>,
passwordResetTokenRepository?: IPasswordResetTokenRepository<U>
) {
this.config = config
this.passport = passportStatic
this.userRepository = userRepository
this.refreshTokenRepository = refreshTokenRepository
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
Loading

0 comments on commit 3a151f6

Please sign in to comment.