feat(smartjwt): Add nested JWT functionality

This commit is contained in:
Philipp Kunz 2024-09-05 16:46:01 +02:00
parent 59ae8ae466
commit 8097ca059f
6 changed files with 31 additions and 13 deletions

View File

@ -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

View File

@ -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"

3
pnpm-lock.yaml generated
View File

@ -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

View File

@ -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.'
}

View File

@ -87,7 +87,7 @@ export class SmartJwt<T extends object = any> {
public isObjectWithJwt<T extends object>(
public isNestedJwt<T extends object>(
object: unknown,
): object is IObjectWithJwt<T> {
return (
@ -98,17 +98,22 @@ export class SmartJwt<T extends object = any> {
);
}
public jwtObjectGuard = new plugins.smartguard.Guard(async (dataArg: IObjectWithJwt<any>) => {
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<T extends object>(object: IObjectWithJwt<T>) {
const jwtData = await this.verifyJWTAndGetData(object.jwt);
(jwtData as any).jwt = object.jwt;
return plugins.smartjson.deepEqualObjects(object, jwtData);
}
public async createNestedJwt<T extends object>(payloadArg: T): Promise<IObjectWithJwt<T>> {
const jwt = await this.createJWT(payloadArg as any);
return {
...payloadArg,
jwt,
}
}
}
return true;
public nestedJwtGuard = new plugins.smartguard.Guard(async (dataArg: IObjectWithJwt<any>) => {
return this.verifyNestedJwt(dataArg);
}, {
name: 'jwtObjectGuard',
failedHint: 'is not a valid jwt object',

View File

@ -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';