use ports package

This commit is contained in:
admin 2025-08-23 23:25:25 +02:00
parent 497bee7160
commit 956cfaf061
7 changed files with 122 additions and 100 deletions

51
package-lock.json generated
View File

@ -1,15 +1,15 @@
{ {
"name": "@apihub24/jwt-generator", "name": "@apihub24/jwt-generator",
"version": "1.0.2", "version": "1.0.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@apihub24/jwt-generator", "name": "@apihub24/jwt-generator",
"version": "1.0.2", "version": "1.0.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apihub24/token-authentication": "^1.0.4", "@apihub24/authentication": "^1.0.1",
"@nestjs/common": "^11.1.6", "@nestjs/common": "^11.1.6",
"@nestjs/config": "^4.0.2", "@nestjs/config": "^4.0.2",
"jsonwebtoken": "^9.0.2" "jsonwebtoken": "^9.0.2"
@ -37,25 +37,12 @@
"node": ">=6.0.0" "node": ">=6.0.0"
} }
}, },
"node_modules/@apihub24/repository": { "node_modules/@apihub24/authentication": {
"version": "1.0.3", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.3.tgz", "resolved": "https://registry.npmjs.org/@apihub24/authentication/-/authentication-1.0.1.tgz",
"integrity": "sha512-m2twcVPrdnKAcnNQFabGzQ/18/kQUEtuqAuSzVBTEc3mxBKBQ5ex1+Cx4JP/sZ1HqdS4GisFXDa8zfrnpdcLaA==", "integrity": "sha512-nWw75ofQKHxE0dI7PzvNBQNcQrX/HSrzuAJTYNu42BoCROba1NUz8QAodTn5+3dIeQEzw127gtSb6D7yW0B8Jg==",
"license": "MIT" "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": { "node_modules/@babel/code-frame": {
"version": "7.27.1", "version": "7.27.1",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz",
@ -1137,8 +1124,10 @@
"version": "11.1.6", "version": "11.1.6",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.6.tgz", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.6.tgz",
"integrity": "sha512-siWX7UDgErisW18VTeJA+x+/tpNZrJewjTBsRPF3JVxuWRuAB1kRoiJcxHgln8Lb5UY9NdvklITR84DUEXD0Cg==", "integrity": "sha512-siWX7UDgErisW18VTeJA+x+/tpNZrJewjTBsRPF3JVxuWRuAB1kRoiJcxHgln8Lb5UY9NdvklITR84DUEXD0Cg==",
"dev": true,
"hasInstallScript": true, "hasInstallScript": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@nuxt/opencollective": "0.4.1", "@nuxt/opencollective": "0.4.1",
"fast-safe-stringify": "2.1.1", "fast-safe-stringify": "2.1.1",
@ -1206,7 +1195,9 @@
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/@nuxt/opencollective/-/opencollective-0.4.1.tgz", "resolved": "https://registry.npmjs.org/@nuxt/opencollective/-/opencollective-0.4.1.tgz",
"integrity": "sha512-GXD3wy50qYbxCJ652bDrDzgMr3NFEkIS374+IgFQKkCvk9yiYcLvX2XDYr7UyQxf4wK0e+yqDYRubZ0DtOxnmQ==", "integrity": "sha512-GXD3wy50qYbxCJ652bDrDzgMr3NFEkIS374+IgFQKkCvk9yiYcLvX2XDYr7UyQxf4wK0e+yqDYRubZ0DtOxnmQ==",
"dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"consola": "^3.2.3" "consola": "^3.2.3"
}, },
@ -1408,7 +1399,9 @@
"version": "13.15.2", "version": "13.15.2",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.15.2.tgz", "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.15.2.tgz",
"integrity": "sha512-y7pa/oEJJ4iGYBxOpfAKn5b9+xuihvzDVnC/OSvlVnGxVg0pOqmjiMafiJ1KVNQEaPZf9HsEp5icEwGg8uIe5Q==", "integrity": "sha512-y7pa/oEJJ4iGYBxOpfAKn5b9+xuihvzDVnC/OSvlVnGxVg0pOqmjiMafiJ1KVNQEaPZf9HsEp5icEwGg8uIe5Q==",
"license": "MIT" "license": "MIT",
"optional": true,
"peer": true
}, },
"node_modules/@types/yargs": { "node_modules/@types/yargs": {
"version": "17.0.33", "version": "17.0.33",
@ -2065,6 +2058,8 @@
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.2.tgz", "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.2.tgz",
"integrity": "sha512-3kMVRF2io8N8pY1IFIXlho9r8IPUUIfHe2hYVtiebvAzU2XeQFXTv+XI4WX+TnXmtwXMDcjngcpkiPM0O9PvLw==", "integrity": "sha512-3kMVRF2io8N8pY1IFIXlho9r8IPUUIfHe2hYVtiebvAzU2XeQFXTv+XI4WX+TnXmtwXMDcjngcpkiPM0O9PvLw==",
"license": "MIT", "license": "MIT",
"optional": true,
"peer": true,
"dependencies": { "dependencies": {
"@types/validator": "^13.11.8", "@types/validator": "^13.11.8",
"libphonenumber-js": "^1.11.1", "libphonenumber-js": "^1.11.1",
@ -2198,7 +2193,9 @@
"version": "3.4.2", "version": "3.4.2",
"resolved": "https://registry.npmjs.org/consola/-/consola-3.4.2.tgz", "resolved": "https://registry.npmjs.org/consola/-/consola-3.4.2.tgz",
"integrity": "sha512-5IKcdX0nnYavi6G7TtOhwkYzyjfJlatbjMjuLSfE2kYT5pMDOilZ4OvMhi637CcDICTmz3wARPoyhqyX1Y+XvA==", "integrity": "sha512-5IKcdX0nnYavi6G7TtOhwkYzyjfJlatbjMjuLSfE2kYT5pMDOilZ4OvMhi637CcDICTmz3wARPoyhqyX1Y+XvA==",
"dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"engines": { "engines": {
"node": "^14.18.0 || >=16.10.0" "node": "^14.18.0 || >=16.10.0"
} }
@ -2461,7 +2458,9 @@
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz",
"integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==",
"license": "MIT" "dev": true,
"license": "MIT",
"peer": true
}, },
"node_modules/fb-watchman": { "node_modules/fb-watchman": {
"version": "2.0.2", "version": "2.0.2",
@ -3609,7 +3608,9 @@
"version": "1.12.13", "version": "1.12.13",
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.12.13.tgz", "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.12.13.tgz",
"integrity": "sha512-QZXnR/OGiDcBjF4hGk0wwVrPcZvbSSyzlvkjXv5LFfktj7O2VZDrt4Xs8SgR/vOFco+qk1i8J43ikMXZoTrtPw==", "integrity": "sha512-QZXnR/OGiDcBjF4hGk0wwVrPcZvbSSyzlvkjXv5LFfktj7O2VZDrt4Xs8SgR/vOFco+qk1i8J43ikMXZoTrtPw==",
"license": "MIT" "license": "MIT",
"optional": true,
"peer": true
}, },
"node_modules/lines-and-columns": { "node_modules/lines-and-columns": {
"version": "1.2.4", "version": "1.2.4",
@ -4053,7 +4054,9 @@
"version": "8.2.0", "version": "8.2.0",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz",
"integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==", "integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==",
"dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"engines": { "engines": {
"node": ">=16" "node": ">=16"
} }
@ -4983,6 +4986,8 @@
"resolved": "https://registry.npmjs.org/validator/-/validator-13.15.15.tgz", "resolved": "https://registry.npmjs.org/validator/-/validator-13.15.15.tgz",
"integrity": "sha512-BgWVbCI72aIQy937xbawcs+hrVaN/CZ2UwutgaJ36hGqRrLNM+f5LUT/YPRbo8IV/ASeFzXszezV+y2+rq3l8A==", "integrity": "sha512-BgWVbCI72aIQy937xbawcs+hrVaN/CZ2UwutgaJ36hGqRrLNM+f5LUT/YPRbo8IV/ASeFzXszezV+y2+rq3l8A==",
"license": "MIT", "license": "MIT",
"optional": true,
"peer": true,
"engines": { "engines": {
"node": ">= 0.10" "node": ">= 0.10"
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@apihub24/jwt-generator", "name": "@apihub24/jwt-generator",
"version": "1.0.2", "version": "1.0.4",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
@ -15,7 +15,7 @@
"@nestjs/common": "^11.1.6", "@nestjs/common": "^11.1.6",
"@nestjs/config": "^4.0.2", "@nestjs/config": "^4.0.2",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"@apihub24/token-authentication": "^1.0.4" "@apihub24/authentication": "^1.0.1"
}, },
"devDependencies": { "devDependencies": {
"rimraf": "^6.0.1", "rimraf": "^6.0.1",

View File

@ -1,34 +1,37 @@
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { import { JwtGeneratorModule } from "./";
APIHUB24_TOKEN_SERVICE_INJECTION_KEY, import { SessionModuleMock } from "../test/session.service.mock";
JwtGeneratorModule, import { ConfigService } from "@nestjs/config";
JwtService, import { APIHUB24_TOKEN_SERVICE } from "@apihub24/authentication";
} from "./";
import { SessionServiceMock } from "../test/session.service.mock";
describe("JwtGeneratorModule Tests", () => { describe("JwtGeneratorModule Tests", () => {
let module: TestingModule | null = null; let module: TestingModule | null = null;
beforeAll(async () => { beforeAll(async () => {
module = await Test.createTestingModule({ module = await Test.createTestingModule({
imports: [ imports: [SessionModuleMock.forRoot(), JwtGeneratorModule.forRoot()],
JwtGeneratorModule.forRoot([ })
{ .overrideProvider(ConfigService)
provide: "@apihub24/session_service", .useValue({
useClass: SessionServiceMock, get: jest.fn((key: string) => {
}, if (key === "JWT_SECRET") {
]), return "thisisnotsigrid";
], }
}).compile(); if (key === "JWT_ISSUER") {
}); return "test";
}
it("should export injection Key @apihub24/token_service", () => { if (key === "JWT_AUDIENCE") {
expect(APIHUB24_TOKEN_SERVICE_INJECTION_KEY).toBeDefined(); return "test";
}
return null;
}),
})
.compile();
}); });
it("should get JwtService by injection Key", () => { it("should get JwtService by injection Key", () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get("@apihub24/token_service"); const service = module?.get(APIHUB24_TOKEN_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
}); });
}); });

View File

@ -1,18 +1,17 @@
import { DynamicModule, Module, Provider } from "@nestjs/common"; import { DynamicModule, Global, Module } from "@nestjs/common";
import { JwtService } from "./jwt.service"; import { JwtService } from "./jwt.service";
import { ConfigModule } from "@nestjs/config"; import { ConfigModule } from "@nestjs/config";
import { APIHUB24_TOKEN_SERVICE } from "@apihub24/authentication";
export const APIHUB24_TOKEN_SERVICE_INJECTION_KEY = "@apihub24/token_service"; @Global()
@Module({}) @Module({})
export class JwtGeneratorModule { export class JwtGeneratorModule {
static forRoot(requiredProviders: Provider[]): DynamicModule { static forRoot(): DynamicModule {
const providers = [ const providers = [
{ {
provide: APIHUB24_TOKEN_SERVICE_INJECTION_KEY, provide: APIHUB24_TOKEN_SERVICE,
useClass: JwtService, useClass: JwtService,
}, },
...requiredProviders,
]; ];
return { return {
module: JwtGeneratorModule, module: JwtGeneratorModule,

View File

@ -1,27 +1,41 @@
import { ISession } from "@apihub24/token-authentication"; import {
import { APIHUB24_TOKEN_SERVICE_INJECTION_KEY, JwtGeneratorModule } from "./"; APIHUB24_SESSION_SERVICE,
APIHUB24_TOKEN_SERVICE,
ISession,
} from "@apihub24/authentication";
import { JwtGeneratorModule } from "./";
import { Test, TestingModule } from "@nestjs/testing"; 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", () => { describe("JwtService Tests", () => {
let module: TestingModule | null = null; let module: TestingModule | null = null;
beforeAll(async () => { beforeAll(async () => {
module = await Test.createTestingModule({ module = await Test.createTestingModule({
imports: [ imports: [SessionModuleMock.forRoot(), JwtGeneratorModule.forRoot()],
JwtGeneratorModule.forRoot([ })
{ .overrideProvider(ConfigService)
provide: "@apihub24/session_service", .useValue({
useClass: SessionServiceMock, get: jest.fn((key: string) => {
}, if (key === "JWT_SECRET") {
]), return "thisisnotsigrid";
], }
}).compile(); if (key === "JWT_ISSUER") {
return "test";
}
if (key === "JWT_AUDIENCE") {
return "test";
}
return null;
}),
})
.compile();
}); });
it("should generate JWT", async () => { it("should generate JWT", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
const session: ISession = { const session: ISession = {
@ -44,7 +58,7 @@ describe("JwtService Tests", () => {
it("should verify valid JWT", async () => { it("should verify valid JWT", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
const session: ISession = { const session: ISession = {
@ -68,15 +82,15 @@ describe("JwtService Tests", () => {
it("should not verify invalid JWT", async () => { it("should not verify invalid JWT", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
expect(await service?.validate("")).toBeFalsy(); expect(await service?.validate("")).toBeFalsy();
}); });
it("should get Account from JWT", async () => { it("should get Account from JWT", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
const sessionService = module?.get("@apihub24/session_service"); const sessionService = module?.get(APIHUB24_SESSION_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
const session: ISession = { const session: ISession = {
@ -104,8 +118,8 @@ describe("JwtService Tests", () => {
it("should not get Account from invalid JWT", async () => { it("should not get Account from invalid JWT", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
const sessionService = module?.get("@apihub24/session_service"); const sessionService = module?.get(APIHUB24_SESSION_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
expect(await service?.getAccount("")).toBeNull(); expect(await service?.getAccount("")).toBeNull();
@ -113,8 +127,8 @@ describe("JwtService Tests", () => {
it("should not get Account if no Session available", async () => { it("should not get Account if no Session available", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
const sessionService = module?.get("@apihub24/session_service"); const sessionService = module?.get(APIHUB24_SESSION_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
const session: ISession = { const session: ISession = {
@ -139,8 +153,8 @@ describe("JwtService Tests", () => {
it("should not get Account if no Account in Session available", async () => { it("should not get Account if no Account in Session available", async () => {
expect(module).toBeDefined(); expect(module).toBeDefined();
const service = module?.get(APIHUB24_TOKEN_SERVICE_INJECTION_KEY); const service = module?.get(APIHUB24_TOKEN_SERVICE);
const sessionService = module?.get("@apihub24/session_service"); const sessionService = module?.get(APIHUB24_SESSION_SERVICE);
expect(service).toBeDefined(); expect(service).toBeDefined();
const session: ISession = { const session: ISession = {
@ -156,6 +170,7 @@ describe("JwtService Tests", () => {
}, },
metaData: {}, metaData: {},
}; };
console.info(sessionService);
jest.spyOn(sessionService, "getById").mockReturnValue({ jest.spyOn(sessionService, "getById").mockReturnValue({
id: "1", id: "1",
account: null, account: null,

View File

@ -1,22 +1,22 @@
import * as tokenAuthentication from "@apihub24/token-authentication"; import * as authentication from "@apihub24/authentication";
import { Inject, Injectable } from "@nestjs/common"; import { Inject, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import jwt, { JwtPayload } from "jsonwebtoken"; import jwt, { JwtPayload } from "jsonwebtoken";
@Injectable() @Injectable()
export class JwtService implements tokenAuthentication.ITokenService { export class JwtService implements authentication.ITokenService {
constructor( constructor(
@Inject(ConfigService) @Inject(ConfigService)
private readonly configService: ConfigService, private readonly configService: ConfigService,
@Inject("@apihub24/session_service") @Inject(authentication.APIHUB24_SESSION_SERVICE)
private readonly sessionService: tokenAuthentication.ISessionService private readonly sessionService: authentication.ISessionService
) {} ) {}
generate( generate(
session: tokenAuthentication.ISession, session: authentication.ISession,
subject: string, subject: string,
expires?: string, expires?: string,
algorithm?: tokenAuthentication.Algorithm algorithm?: authentication.Algorithm
): Promise<string> { ): Promise<string> {
return Promise.resolve( return Promise.resolve(
jwt.sign( jwt.sign(
@ -46,9 +46,7 @@ export class JwtService implements tokenAuthentication.ITokenService {
} }
} }
async getAccount( async getAccount(token: string): Promise<authentication.IAccount | null> {
token: string
): Promise<tokenAuthentication.IAccount | null> {
try { try {
const payload = jwt.verify(token, this.getSecretKey()) as JwtPayload; const payload = jwt.verify(token, this.getSecretKey()) as JwtPayload;
if (typeof payload?.sessionId !== "string") { if (typeof payload?.sessionId !== "string") {

View File

@ -1,20 +1,22 @@
import { import { APIHUB24_SESSION_SERVICE } from "@apihub24/authentication";
IAccount, import { DynamicModule, Global, Module } from "@nestjs/common";
ISession,
ISessionService,
} from "@apihub24/token-authentication";
export class SessionServiceMock implements ISessionService { @Global()
create(account: IAccount): Promise<ISession> { @Module({})
throw new Error("Method not implemented."); export class SessionModuleMock {
} static forRoot(): DynamicModule {
getBy(filter: (account: IAccount) => boolean): Promise<ISession[]> { const providers = [
throw new Error("Method not implemented."); {
} provide: APIHUB24_SESSION_SERVICE,
getById(sessionId: string): Promise<ISession | null> { useValue: {
throw new Error("Method not implemented."); getById: () => {},
} },
remove(sessionId: string): Promise<void> { },
throw new Error("Method not implemented."); ];
return {
module: SessionModuleMock,
providers: [...providers],
exports: [...providers],
};
} }
} }