From 8097ca059f9e6d47b203b21e67912de707959ef4 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 5 Sep 2024 16:46:01 +0200 Subject: [PATCH] feat(smartjwt): Add nested JWT functionality --- changelog.md | 8 ++++++++ package.json | 1 + pnpm-lock.yaml | 3 +++ ts/00_commitinfo_data.ts | 2 +- ts/smartjwt.classes.smartjwt.ts | 27 ++++++++++++++++----------- ts/smartjwt.plugins.ts | 3 ++- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/changelog.md b/changelog.md index 4987c84..a2bc018 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,13 @@ # Changelog +## 2024-09-05 - 2.2.0 - feat(smartjwt) +Add nested JWT functionality + +- Introduce `isNestedJwt` method to check if an object contains a nested JWT +- Implement `verifyNestedJwt` method to validate nested JWTs +- Add `createNestedJwt` method to create nested JWTs with given payload +- Include `nestedJwtGuard` for guarding nested JWT objects + ## 2024-08-26 - 2.1.0 - feat(core) Enhanced JWT handling and key management diff --git a/package.json b/package.json index 2037b89..a590736 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "dependencies": { "@push.rocks/smartcrypto": "^2.0.4", "@push.rocks/smartguard": "^3.1.0", + "@push.rocks/smartjson": "^5.0.20", "@tsclass/tsclass": "^4.1.2", "@types/jsonwebtoken": "^9.0.6", "jsonwebtoken": "^9.0.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2ebb12e..c6abd27 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@push.rocks/smartguard': specifier: ^3.1.0 version: 3.1.0 + '@push.rocks/smartjson': + specifier: ^5.0.20 + version: 5.0.20 '@tsclass/tsclass': specifier: ^4.1.2 version: 4.1.2 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 5d33e9f..d7c01e2 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartjwt', - version: '2.1.0', + version: '2.2.0', description: 'A JavaScript package for creating and verifying JWTs with strong typing support.' } diff --git a/ts/smartjwt.classes.smartjwt.ts b/ts/smartjwt.classes.smartjwt.ts index f5eb289..6ba13dc 100644 --- a/ts/smartjwt.classes.smartjwt.ts +++ b/ts/smartjwt.classes.smartjwt.ts @@ -87,7 +87,7 @@ export class SmartJwt { - public isObjectWithJwt( + public isNestedJwt( object: unknown, ): object is IObjectWithJwt { return ( @@ -98,17 +98,22 @@ export class SmartJwt { ); } - public jwtObjectGuard = new plugins.smartguard.Guard(async (dataArg: IObjectWithJwt) => { - const jwtData = this.verifyJWTAndGetData(dataArg.jwt); - // check all other properties wether they match with jwtData - for (const key in dataArg) { - if (key !== 'jwt') { - if (jwtData[key] !== dataArg[key]) { - return false; - } - } + public async verifyNestedJwt(object: IObjectWithJwt) { + const jwtData = await this.verifyJWTAndGetData(object.jwt); + (jwtData as any).jwt = object.jwt; + return plugins.smartjson.deepEqualObjects(object, jwtData); + } + + public async createNestedJwt(payloadArg: T): Promise> { + const jwt = await this.createJWT(payloadArg as any); + return { + ...payloadArg, + jwt, } - return true; + } + + public nestedJwtGuard = new plugins.smartguard.Guard(async (dataArg: IObjectWithJwt) => { + return this.verifyNestedJwt(dataArg); }, { name: 'jwtObjectGuard', failedHint: 'is not a valid jwt object', diff --git a/ts/smartjwt.plugins.ts b/ts/smartjwt.plugins.ts index ace59e8..26f6d6a 100644 --- a/ts/smartjwt.plugins.ts +++ b/ts/smartjwt.plugins.ts @@ -1,8 +1,9 @@ // @pushrocks scope import * as smartcrypto from '@push.rocks/smartcrypto'; import * as smartguard from '@push.rocks/smartguard'; +import * as smartjson from '@push.rocks/smartjson'; -export { smartcrypto, smartguard }; +export { smartcrypto, smartguard, smartjson }; // thirdparty scope import jsonwebtoken from 'jsonwebtoken';