diff --git a/package-lock.json b/package-lock.json index b228218..48f04f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@apihub24/jwt-generator", - "version": "1.0.2", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@apihub24/jwt-generator", - "version": "1.0.2", + "version": "1.0.4", "license": "MIT", "dependencies": { - "@apihub24/token-authentication": "^1.0.4", + "@apihub24/authentication": "^1.0.1", "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "jsonwebtoken": "^9.0.2" @@ -37,25 +37,12 @@ "node": ">=6.0.0" } }, - "node_modules/@apihub24/repository": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.3.tgz", - "integrity": "sha512-m2twcVPrdnKAcnNQFabGzQ/18/kQUEtuqAuSzVBTEc3mxBKBQ5ex1+Cx4JP/sZ1HqdS4GisFXDa8zfrnpdcLaA==", + "node_modules/@apihub24/authentication": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@apihub24/authentication/-/authentication-1.0.1.tgz", + "integrity": "sha512-nWw75ofQKHxE0dI7PzvNBQNcQrX/HSrzuAJTYNu42BoCROba1NUz8QAodTn5+3dIeQEzw127gtSb6D7yW0B8Jg==", "license": "MIT" }, - "node_modules/@apihub24/token-authentication": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@apihub24/token-authentication/-/token-authentication-1.0.4.tgz", - "integrity": "sha512-4v572LBbk9mDAthrYcI7wbm45gVki2GvZJUCKZPG7fYjR33M67+JtaVaLHy5jQ47QtOo1FF7cdF69wEq6nnn1g==", - "license": "MIT", - "dependencies": { - "@apihub24/repository": "^1.0.3", - "@nestjs/common": "^11.1.6", - "@nestjs/config": "^4.0.2", - "@nestjs/core": "^11.1.6", - "class-validator": "^0.14.2" - } - }, "node_modules/@babel/code-frame": { "version": "7.27.1", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz", @@ -1137,8 +1124,10 @@ "version": "11.1.6", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.6.tgz", "integrity": "sha512-siWX7UDgErisW18VTeJA+x+/tpNZrJewjTBsRPF3JVxuWRuAB1kRoiJcxHgln8Lb5UY9NdvklITR84DUEXD0Cg==", + "dev": true, "hasInstallScript": true, "license": "MIT", + "peer": true, "dependencies": { "@nuxt/opencollective": "0.4.1", "fast-safe-stringify": "2.1.1", @@ -1206,7 +1195,9 @@ "version": "0.4.1", "resolved": "https://registry.npmjs.org/@nuxt/opencollective/-/opencollective-0.4.1.tgz", "integrity": "sha512-GXD3wy50qYbxCJ652bDrDzgMr3NFEkIS374+IgFQKkCvk9yiYcLvX2XDYr7UyQxf4wK0e+yqDYRubZ0DtOxnmQ==", + "dev": true, "license": "MIT", + "peer": true, "dependencies": { "consola": "^3.2.3" }, @@ -1408,7 +1399,9 @@ "version": "13.15.2", "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.15.2.tgz", "integrity": "sha512-y7pa/oEJJ4iGYBxOpfAKn5b9+xuihvzDVnC/OSvlVnGxVg0pOqmjiMafiJ1KVNQEaPZf9HsEp5icEwGg8uIe5Q==", - "license": "MIT" + "license": "MIT", + "optional": true, + "peer": true }, "node_modules/@types/yargs": { "version": "17.0.33", @@ -2065,6 +2058,8 @@ "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.2.tgz", "integrity": "sha512-3kMVRF2io8N8pY1IFIXlho9r8IPUUIfHe2hYVtiebvAzU2XeQFXTv+XI4WX+TnXmtwXMDcjngcpkiPM0O9PvLw==", "license": "MIT", + "optional": true, + "peer": true, "dependencies": { "@types/validator": "^13.11.8", "libphonenumber-js": "^1.11.1", @@ -2198,7 +2193,9 @@ "version": "3.4.2", "resolved": "https://registry.npmjs.org/consola/-/consola-3.4.2.tgz", "integrity": "sha512-5IKcdX0nnYavi6G7TtOhwkYzyjfJlatbjMjuLSfE2kYT5pMDOilZ4OvMhi637CcDICTmz3wARPoyhqyX1Y+XvA==", + "dev": true, "license": "MIT", + "peer": true, "engines": { "node": "^14.18.0 || >=16.10.0" } @@ -2461,7 +2458,9 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", - "license": "MIT" + "dev": true, + "license": "MIT", + "peer": true }, "node_modules/fb-watchman": { "version": "2.0.2", @@ -3609,7 +3608,9 @@ "version": "1.12.13", "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.12.13.tgz", "integrity": "sha512-QZXnR/OGiDcBjF4hGk0wwVrPcZvbSSyzlvkjXv5LFfktj7O2VZDrt4Xs8SgR/vOFco+qk1i8J43ikMXZoTrtPw==", - "license": "MIT" + "license": "MIT", + "optional": true, + "peer": true }, "node_modules/lines-and-columns": { "version": "1.2.4", @@ -4053,7 +4054,9 @@ "version": "8.2.0", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz", "integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==", + "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=16" } @@ -4983,6 +4986,8 @@ "resolved": "https://registry.npmjs.org/validator/-/validator-13.15.15.tgz", "integrity": "sha512-BgWVbCI72aIQy937xbawcs+hrVaN/CZ2UwutgaJ36hGqRrLNM+f5LUT/YPRbo8IV/ASeFzXszezV+y2+rq3l8A==", "license": "MIT", + "optional": true, + "peer": true, "engines": { "node": ">= 0.10" } diff --git a/package.json b/package.json index 051694d..847ac08 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@apihub24/jwt-generator", - "version": "1.0.2", + "version": "1.0.4", "description": "", "main": "dist/index.js", "types": "./dist/index.d.ts", @@ -15,7 +15,7 @@ "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "jsonwebtoken": "^9.0.2", - "@apihub24/token-authentication": "^1.0.4" + "@apihub24/authentication": "^1.0.1" }, "devDependencies": { "rimraf": "^6.0.1", diff --git a/src/jwt.generator.module.spec.ts b/src/jwt.generator.module.spec.ts index c3cef82..1ac3210 100644 --- a/src/jwt.generator.module.spec.ts +++ b/src/jwt.generator.module.spec.ts @@ -1,34 +1,37 @@ import { Test, TestingModule } from "@nestjs/testing"; -import { - APIHUB24_TOKEN_SERVICE_INJECTION_KEY, - JwtGeneratorModule, - JwtService, -} from "./"; -import { SessionServiceMock } from "../test/session.service.mock"; +import { JwtGeneratorModule } from "./"; +import { SessionModuleMock } from "../test/session.service.mock"; +import { ConfigService } from "@nestjs/config"; +import { APIHUB24_TOKEN_SERVICE } from "@apihub24/authentication"; describe("JwtGeneratorModule Tests", () => { let module: TestingModule | null = null; beforeAll(async () => { module = await Test.createTestingModule({ - imports: [ - JwtGeneratorModule.forRoot([ - { - provide: "@apihub24/session_service", - useClass: SessionServiceMock, - }, - ]), - ], - }).compile(); - }); - - it("should export injection Key @apihub24/token_service", () => { - expect(APIHUB24_TOKEN_SERVICE_INJECTION_KEY).toBeDefined(); + imports: [SessionModuleMock.forRoot(), JwtGeneratorModule.forRoot()], + }) + .overrideProvider(ConfigService) + .useValue({ + get: jest.fn((key: string) => { + if (key === "JWT_SECRET") { + return "thisisnotsigrid"; + } + if (key === "JWT_ISSUER") { + return "test"; + } + if (key === "JWT_AUDIENCE") { + return "test"; + } + return null; + }), + }) + .compile(); }); it("should get JwtService by injection Key", () => { expect(module).toBeDefined(); - const service = module?.get("@apihub24/token_service"); + const service = module?.get(APIHUB24_TOKEN_SERVICE); expect(service).toBeDefined(); }); }); diff --git a/src/jwt.generator.module.ts b/src/jwt.generator.module.ts index 3e83ad8..822e0ee 100644 --- a/src/jwt.generator.module.ts +++ b/src/jwt.generator.module.ts @@ -1,18 +1,17 @@ -import { DynamicModule, Module, Provider } from "@nestjs/common"; +import { DynamicModule, Global, Module } from "@nestjs/common"; import { JwtService } from "./jwt.service"; import { ConfigModule } from "@nestjs/config"; +import { APIHUB24_TOKEN_SERVICE } from "@apihub24/authentication"; -export const APIHUB24_TOKEN_SERVICE_INJECTION_KEY = "@apihub24/token_service"; - +@Global() @Module({}) export class JwtGeneratorModule { - static forRoot(requiredProviders: Provider[]): DynamicModule { + static forRoot(): DynamicModule { const providers = [ { - provide: APIHUB24_TOKEN_SERVICE_INJECTION_KEY, + provide: APIHUB24_TOKEN_SERVICE, useClass: JwtService, }, - ...requiredProviders, ]; return { module: JwtGeneratorModule, diff --git a/src/jwt.service.spec.ts b/src/jwt.service.spec.ts index c2d9735..36b0b72 100644 --- a/src/jwt.service.spec.ts +++ b/src/jwt.service.spec.ts @@ -1,27 +1,41 @@ -import { ISession } from "@apihub24/token-authentication"; -import { APIHUB24_TOKEN_SERVICE_INJECTION_KEY, JwtGeneratorModule } from "./"; +import { + APIHUB24_SESSION_SERVICE, + APIHUB24_TOKEN_SERVICE, + ISession, +} from "@apihub24/authentication"; +import { JwtGeneratorModule } from "./"; import { Test, TestingModule } from "@nestjs/testing"; -import { SessionServiceMock } from "../test/session.service.mock"; +import { SessionModuleMock } from "../test/session.service.mock"; +import { ConfigService } from "@nestjs/config"; describe("JwtService Tests", () => { let module: TestingModule | null = null; beforeAll(async () => { module = await Test.createTestingModule({ - imports: [ - JwtGeneratorModule.forRoot([ - { - provide: "@apihub24/session_service", - useClass: SessionServiceMock, - }, - ]), - ], - }).compile(); + imports: [SessionModuleMock.forRoot(), JwtGeneratorModule.forRoot()], + }) + .overrideProvider(ConfigService) + .useValue({ + get: jest.fn((key: string) => { + if (key === "JWT_SECRET") { + return "thisisnotsigrid"; + } + if (key === "JWT_ISSUER") { + return "test"; + } + if (key === "JWT_AUDIENCE") { + return "test"; + } + return null; + }), + }) + .compile(); }); it("should generate JWT", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); + const service = module?.get(APIHUB24_TOKEN_SERVICE); expect(service).toBeDefined(); const session: ISession = { @@ -44,7 +58,7 @@ describe("JwtService Tests", () => { it("should verify valid JWT", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); + const service = module?.get(APIHUB24_TOKEN_SERVICE); expect(service).toBeDefined(); const session: ISession = { @@ -68,15 +82,15 @@ describe("JwtService Tests", () => { it("should not verify invalid JWT", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); + const service = module?.get(APIHUB24_TOKEN_SERVICE); expect(service).toBeDefined(); expect(await service?.validate("")).toBeFalsy(); }); it("should get Account from JWT", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); - const sessionService = module?.get("@apihub24/session_service"); + const service = module?.get(APIHUB24_TOKEN_SERVICE); + const sessionService = module?.get(APIHUB24_SESSION_SERVICE); expect(service).toBeDefined(); const session: ISession = { @@ -104,8 +118,8 @@ describe("JwtService Tests", () => { it("should not get Account from invalid JWT", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); - const sessionService = module?.get("@apihub24/session_service"); + const service = module?.get(APIHUB24_TOKEN_SERVICE); + const sessionService = module?.get(APIHUB24_SESSION_SERVICE); expect(service).toBeDefined(); expect(await service?.getAccount("")).toBeNull(); @@ -113,8 +127,8 @@ describe("JwtService Tests", () => { it("should not get Account if no Session available", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); - const sessionService = module?.get("@apihub24/session_service"); + const service = module?.get(APIHUB24_TOKEN_SERVICE); + const sessionService = module?.get(APIHUB24_SESSION_SERVICE); expect(service).toBeDefined(); const session: ISession = { @@ -139,8 +153,8 @@ describe("JwtService Tests", () => { it("should not get Account if no Account in Session available", async () => { expect(module).toBeDefined(); - const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); - const sessionService = module?.get("@apihub24/session_service"); + const service = module?.get(APIHUB24_TOKEN_SERVICE); + const sessionService = module?.get(APIHUB24_SESSION_SERVICE); expect(service).toBeDefined(); const session: ISession = { @@ -156,6 +170,7 @@ describe("JwtService Tests", () => { }, metaData: {}, }; + console.info(sessionService); jest.spyOn(sessionService, "getById").mockReturnValue({ id: "1", account: null, diff --git a/src/jwt.service.ts b/src/jwt.service.ts index 7207b08..a5e69c8 100644 --- a/src/jwt.service.ts +++ b/src/jwt.service.ts @@ -1,22 +1,22 @@ -import * as tokenAuthentication from "@apihub24/token-authentication"; +import * as authentication from "@apihub24/authentication"; import { Inject, Injectable } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import jwt, { JwtPayload } from "jsonwebtoken"; @Injectable() -export class JwtService implements tokenAuthentication.ITokenService { +export class JwtService implements authentication.ITokenService { constructor( @Inject(ConfigService) private readonly configService: ConfigService, - @Inject("@apihub24/session_service") - private readonly sessionService: tokenAuthentication.ISessionService + @Inject(authentication.APIHUB24_SESSION_SERVICE) + private readonly sessionService: authentication.ISessionService ) {} generate( - session: tokenAuthentication.ISession, + session: authentication.ISession, subject: string, expires?: string, - algorithm?: tokenAuthentication.Algorithm + algorithm?: authentication.Algorithm ): Promise { return Promise.resolve( jwt.sign( @@ -46,9 +46,7 @@ export class JwtService implements tokenAuthentication.ITokenService { } } - async getAccount( - token: string - ): Promise { + async getAccount(token: string): Promise { try { const payload = jwt.verify(token, this.getSecretKey()) as JwtPayload; if (typeof payload?.sessionId !== "string") { diff --git a/test/session.service.mock.ts b/test/session.service.mock.ts index e651985..8d3df02 100644 --- a/test/session.service.mock.ts +++ b/test/session.service.mock.ts @@ -1,20 +1,22 @@ -import { - IAccount, - ISession, - ISessionService, -} from "@apihub24/token-authentication"; +import { APIHUB24_SESSION_SERVICE } from "@apihub24/authentication"; +import { DynamicModule, Global, Module } from "@nestjs/common"; -export class SessionServiceMock implements ISessionService { - create(account: IAccount): Promise { - throw new Error("Method not implemented."); - } - getBy(filter: (account: IAccount) => boolean): Promise { - throw new Error("Method not implemented."); - } - getById(sessionId: string): Promise { - throw new Error("Method not implemented."); - } - remove(sessionId: string): Promise { - throw new Error("Method not implemented."); +@Global() +@Module({}) +export class SessionModuleMock { + static forRoot(): DynamicModule { + const providers = [ + { + provide: APIHUB24_SESSION_SERVICE, + useValue: { + getById: () => {}, + }, + }, + ]; + return { + module: SessionModuleMock, + providers: [...providers], + exports: [...providers], + }; } }