@amplication/plugin-auth-jwt
Advanced tools
Comparing version 1.4.3 to 1.4.4-beta.pr-289.0d3954c
@@ -1,10 +0,13 @@ | ||
import { JWT_SECRET_KEY } from "../../constants"; | ||
import { JWT_SECRET_KEY_PROVIDER_NAME } from "../../constants"; | ||
import { SecretsManagerService } from "../../providers/secrets/secretsManager.service"; | ||
import { EnumSecretsNameKey } from "../../providers/secrets/secretsNameKey.enum"; | ||
export const jwtSecretFactory = { | ||
provide: JWT_SECRET_KEY, | ||
provide: JWT_SECRET_KEY_PROVIDER_NAME, | ||
useFactory: async ( | ||
secretsService: SecretsManagerService | ||
): Promise<string> => { | ||
const secret = await secretsService.getSecret<string>(JWT_SECRET_KEY); | ||
const secret = await secretsService.getSecret<string>( | ||
EnumSecretsNameKey.JwtSecretKey | ||
); | ||
if (secret) { | ||
@@ -11,0 +14,0 @@ return secret; |
@@ -1,2 +0,2 @@ | ||
export const JWT_SECRET_KEY = "JWT_SECRET_KEY"; | ||
export const JWT_SECRET_KEY_PROVIDER_NAME = "JWT_SECRET_KEY"; | ||
export const JWT_EXPIRATION = "JWT_EXPIRATION"; |
@@ -5,7 +5,6 @@ import { forwardRef, Module } from "@nestjs/common"; | ||
import { PassportModule } from "@nestjs/passport"; | ||
import { JWT_EXPIRATION, JWT_SECRET_KEY } from "../constants"; | ||
import { JWT_EXPIRATION } from "../constants"; | ||
import { SecretsManagerModule } from "../providers/secrets/secretsManager.module"; | ||
import { SecretsManagerService } from "../providers/secrets/secretsManager.service"; | ||
// @ts-ignore | ||
// eslint-disable-next-line | ||
import { EnumSecretsNameKey } from "../providers/secrets/secretsNameKey.enum"; | ||
import { AuthController } from "./auth.controller"; | ||
@@ -32,3 +31,5 @@ import { AuthResolver } from "./auth.resolver"; | ||
) => { | ||
const secret = await secretsService.getSecret<string>(JWT_SECRET_KEY); | ||
const secret = await secretsService.getSecret<string>( | ||
EnumSecretsNameKey.JwtSecretKey | ||
); | ||
const expiresIn = configService.get(JWT_EXPIRATION); | ||
@@ -35,0 +36,0 @@ if (!secret) { |
@@ -9,3 +9,3 @@ import { UnauthorizedException } from "@nestjs/common"; | ||
const userService = mock<ENTITY_SERVICE>(); | ||
const jwtStrategy = new JwtStrategyBase(userService, "Secrete"); | ||
const jwtStrategy = new JwtStrategyBase("Secrete", userService); | ||
beforeEach(() => { | ||
@@ -12,0 +12,0 @@ userService.findOne.mockClear(); |
import { Inject, Injectable } from "@nestjs/common"; | ||
import { JWT_SECRET_KEY } from "../../constants"; | ||
import { JWT_SECRET_KEY_PROVIDER_NAME } from "../../constants"; | ||
import { JwtStrategyBase } from "./base/jwt.strategy.base"; | ||
@@ -9,5 +9,5 @@ | ||
export class JwtStrategy extends JwtStrategyBase { | ||
constructor(@Inject(JWT_SECRET_KEY) secretOrKey: string) { | ||
constructor(@Inject(JWT_SECRET_KEY_PROVIDER_NAME) secretOrKey: string) { | ||
super(secretOrKey, ENTITY_SERVICE); | ||
} | ||
} |
{ | ||
"name": "@amplication/plugin-auth-jwt", | ||
"version": "1.4.3", | ||
"version": "1.4.4-beta.pr-289.0d3954c", | ||
"description": "set jwt as provider for Amplication build", | ||
"main": "dist/index.js", | ||
"nx": {}, | ||
"scripts": { | ||
@@ -15,3 +16,3 @@ "dev": "webpack --watch", | ||
"dependencies": { | ||
"@amplication/code-gen-types": "^2.0.17", | ||
"@amplication/code-gen-types": "^2.0.19", | ||
"@amplication/code-gen-utils": "^0.0.7", | ||
@@ -21,16 +22,16 @@ "lodash": "^4.17.21" | ||
"devDependencies": { | ||
"@babel/parser": "^7.23.0", | ||
"@babel/types": "^7.23.0", | ||
"@types/jest": "^29.5.6", | ||
"@types/lodash": "^4.14.200", | ||
"@babel/parser": "^7.23.3", | ||
"@babel/types": "^7.23.3", | ||
"@types/jest": "^29.5.8", | ||
"@types/lodash": "^4.14.201", | ||
"@types/normalize-path": "^3.0.2", | ||
"@typescript-eslint/eslint-plugin": "^6.9.0", | ||
"@typescript-eslint/parser": "^6.9.0", | ||
"@typescript-eslint/eslint-plugin": "^6.11.0", | ||
"@typescript-eslint/parser": "^6.11.0", | ||
"copy-webpack-plugin": "^11.0.0", | ||
"eslint": "^8.52.0", | ||
"eslint": "^8.53.0", | ||
"jest-mock-extended": "^3.0.5", | ||
"prettier": "^3.0.3", | ||
"prettier": "^3.1.0", | ||
"rimraf": "^5.0.5", | ||
"ts-jest": "^29.1.1", | ||
"ts-loader": "^9.5.0", | ||
"ts-loader": "^9.5.1", | ||
"typescript": "^5.2.2", | ||
@@ -37,0 +38,0 @@ "webpack": "^5.89.0", |
# @amplication/plugin-auth-jwt | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@amplication/plugin-auth-jwt)](https://www.npmjs.com/package/@amplication/plugin-auth-jwt) | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@amplication/plugin-auth-jwt)](https://www.npmjs.com/package/@amplication/plugin-auth-jwt) | ||
@@ -9,3 +9,3 @@ Enable JWT authentication on a service | ||
This plugin adds the required code to use Passport JWT strategy on the generated NestJS application | ||
This plugin adds the required code to use Passport JWT strategy on the generated NestJS application | ||
@@ -18,2 +18,2 @@ ## Configuration | ||
This plugin is integrated with the default behavior of the service by changing the `defaultAuth.guard.ts` file in the `auth` folder, so all guards on REST API controllers and GraphQL API resolvers will use this authentication by default. | ||
This plugin is integrated with the default behavior of the service by changing the `defaultAuth.guard.ts` file in the `auth` folder, so all guards on REST API controllers and GraphQL API resolvers will use this authentication by default. |
@@ -0,1 +1,2 @@ | ||
import { CreateServerDockerComposeParams } from "@amplication/code-gen-types"; | ||
import { join } from "path"; | ||
@@ -9,1 +10,17 @@ | ||
"Authentication entity does not exist. Have you configured the authentication entity in the Resource Settings?"; | ||
export const AUTH_ENTITY_FIELD_USERNAME = "username"; | ||
export const AUTH_ENTITY_FIELD_PASSWORD = "password"; | ||
export const updateDockerComposeProperties: CreateServerDockerComposeParams["updateProperties"] = | ||
[ | ||
{ | ||
services: { | ||
server: { | ||
environment: { | ||
JWT_SECRET_KEY: "${JWT_SECRET_KEY}", | ||
JWT_EXPIRATION: "${JWT_EXPIRATION}", | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; |
@@ -7,2 +7,5 @@ import { | ||
CreateServerAuthParams, | ||
CreateServerParams, | ||
CreateServerDockerComposeParams, | ||
CreateServerSecretsManagerParams, | ||
DsgContext, | ||
@@ -21,6 +24,22 @@ EntityField, | ||
} from "./core"; | ||
import { addIdentifierToConstructorSuperCall, addImports, addInjectableDependency, awaitExpression, getClassDeclarationById, importNames, interpolate, logicalExpression, memberExpression } from "./util/ast"; | ||
import { | ||
addIdentifierToConstructorSuperCall, | ||
addImports, | ||
addInjectableDependency, | ||
awaitExpression, | ||
getClassDeclarationById, | ||
importNames, | ||
interpolate, | ||
logicalExpression, | ||
memberExpression, | ||
} from "./util/ast"; | ||
import { builders, namedTypes } from "ast-types"; | ||
import { relativeImportPath } from "./util/module"; | ||
import { isPasswordField } from "./util/field"; | ||
import { | ||
AUTH_ENTITY_FIELD_PASSWORD, | ||
AUTH_ENTITY_FIELD_USERNAME, | ||
updateDockerComposeProperties, | ||
} from "./constants"; | ||
import { getPluginSettings } from "./util/getPluginSettings"; | ||
@@ -40,2 +59,5 @@ const ARGS_ID = builders.identifier("args"); | ||
return { | ||
CreateServer: { | ||
before: this.beforeCreateServer, | ||
}, | ||
CreateAdminUI: { | ||
@@ -54,5 +76,38 @@ before: this.beforeCreateAdminModules, | ||
}, | ||
CreateServerDockerCompose: { | ||
before: this.beforeCreateDockerComposeFile, | ||
}, | ||
CreateServerSecretsManager: { | ||
before: this.beforeCreateSecretsManager, | ||
}, | ||
}; | ||
} | ||
beforeCreateServer(context: DsgContext, eventParams: CreateServerParams) { | ||
const authEntity = context.entities?.find( | ||
(x) => x.name === context.resourceInfo?.settings.authEntityName | ||
); | ||
if (!authEntity) { | ||
throw new Error(`Authentication entity does not exist`); | ||
} | ||
const requiredFields = [ | ||
AUTH_ENTITY_FIELD_USERNAME, | ||
AUTH_ENTITY_FIELD_PASSWORD, | ||
]; | ||
requiredFields.forEach((requiredField) => { | ||
const field = authEntity.fields.find( | ||
(field) => field.name === requiredField | ||
); | ||
if (!field) { | ||
throw new Error( | ||
`Authentication entity must have a field named ${requiredField}` | ||
); | ||
} | ||
}); | ||
return eventParams; | ||
} | ||
beforeCreateAdminModules( | ||
@@ -271,4 +326,24 @@ context: DsgContext, | ||
} | ||
beforeCreateDockerComposeFile( | ||
dsgContext: DsgContext, | ||
eventParams: CreateServerDockerComposeParams | ||
): CreateServerDockerComposeParams { | ||
eventParams.updateProperties.push(...updateDockerComposeProperties); | ||
return eventParams; | ||
} | ||
beforeCreateSecretsManager( | ||
dsgContext: DsgContext, | ||
eventParams: CreateServerSecretsManagerParams | ||
): CreateServerSecretsManagerParams { | ||
const settings = getPluginSettings(dsgContext.pluginInstallations); | ||
eventParams.secretsNameKey.push({ | ||
name: "JwtSecretKey", // Used in jwt strategy as Enum key | ||
key: settings.JwtSecretKeyReference, | ||
}); | ||
return eventParams; | ||
} | ||
} | ||
export default JwtAuthPlugin; |
@@ -1,10 +0,13 @@ | ||
import { JWT_SECRET_KEY } from "../../constants"; | ||
import { JWT_SECRET_KEY_PROVIDER_NAME } from "../../constants"; | ||
import { SecretsManagerService } from "../../providers/secrets/secretsManager.service"; | ||
import { EnumSecretsNameKey } from "../../providers/secrets/secretsNameKey.enum"; | ||
export const jwtSecretFactory = { | ||
provide: JWT_SECRET_KEY, | ||
provide: JWT_SECRET_KEY_PROVIDER_NAME, | ||
useFactory: async ( | ||
secretsService: SecretsManagerService | ||
): Promise<string> => { | ||
const secret = await secretsService.getSecret<string>(JWT_SECRET_KEY); | ||
const secret = await secretsService.getSecret<string>( | ||
EnumSecretsNameKey.JwtSecretKey | ||
); | ||
if (secret) { | ||
@@ -11,0 +14,0 @@ return secret; |
@@ -1,2 +0,2 @@ | ||
export const JWT_SECRET_KEY = "JWT_SECRET_KEY"; | ||
export const JWT_SECRET_KEY_PROVIDER_NAME = "JWT_SECRET_KEY"; | ||
export const JWT_EXPIRATION = "JWT_EXPIRATION"; |
@@ -5,7 +5,6 @@ import { forwardRef, Module } from "@nestjs/common"; | ||
import { PassportModule } from "@nestjs/passport"; | ||
import { JWT_EXPIRATION, JWT_SECRET_KEY } from "../constants"; | ||
import { JWT_EXPIRATION } from "../constants"; | ||
import { SecretsManagerModule } from "../providers/secrets/secretsManager.module"; | ||
import { SecretsManagerService } from "../providers/secrets/secretsManager.service"; | ||
// @ts-ignore | ||
// eslint-disable-next-line | ||
import { EnumSecretsNameKey } from "../providers/secrets/secretsNameKey.enum"; | ||
import { AuthController } from "./auth.controller"; | ||
@@ -32,3 +31,5 @@ import { AuthResolver } from "./auth.resolver"; | ||
) => { | ||
const secret = await secretsService.getSecret<string>(JWT_SECRET_KEY); | ||
const secret = await secretsService.getSecret<string>( | ||
EnumSecretsNameKey.JwtSecretKey | ||
); | ||
const expiresIn = configService.get(JWT_EXPIRATION); | ||
@@ -35,0 +36,0 @@ if (!secret) { |
@@ -9,3 +9,3 @@ import { UnauthorizedException } from "@nestjs/common"; | ||
const userService = mock<ENTITY_SERVICE>(); | ||
const jwtStrategy = new JwtStrategyBase(userService, "Secrete"); | ||
const jwtStrategy = new JwtStrategyBase("Secrete", userService); | ||
beforeEach(() => { | ||
@@ -12,0 +12,0 @@ userService.findOne.mockClear(); |
import { Inject, Injectable } from "@nestjs/common"; | ||
import { JWT_SECRET_KEY } from "../../constants"; | ||
import { JWT_SECRET_KEY_PROVIDER_NAME } from "../../constants"; | ||
import { JwtStrategyBase } from "./base/jwt.strategy.base"; | ||
@@ -9,5 +9,5 @@ | ||
export class JwtStrategy extends JwtStrategyBase { | ||
constructor(@Inject(JWT_SECRET_KEY) secretOrKey: string) { | ||
constructor(@Inject(JWT_SECRET_KEY_PROVIDER_NAME) secretOrKey: string) { | ||
super(secretOrKey, ENTITY_SERVICE); | ||
} | ||
} |
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": [ | ||
"src/index.ts" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
], | ||
} | ||
"include": ["src/index.ts"], | ||
"exclude": ["node_modules"] | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3162303
46
39350
2