feat(smartjwt): Add nested JWT functionality

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

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