feat(core): Enhanced JWT handling and key management

This commit is contained in:
2024-08-26 21:49:48 +02:00
parent 26655ff92e
commit b247630e0f
6 changed files with 5089 additions and 3988 deletions

View File

@@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartjwt',
version: '2.0.4',
description: 'a package for handling jwt'
version: '2.1.0',
description: 'A JavaScript package for creating and verifying JWTs with strong typing support.'
}

View File

@@ -1,5 +1,8 @@
import * as plugins from './smartjwt.plugins.js';
// Define the IWithJwt interface with a constraint that T must be an object
export type IObjectWithJwt<T extends object> = T & { jwt: string };
/**
* A class to create and validate JWTs and their keys
*/
@@ -81,4 +84,33 @@ export class SmartJwt<T extends object = any> {
public async init() {
await this.createNewKeyPair();
}
public isObjectWithJwt<T extends object>(
object: unknown,
): object is IObjectWithJwt<T> {
return (
typeof object === 'object' &&
object !== null &&
'jwt' in object &&
typeof (object as IObjectWithJwt<T>).jwt === 'string'
);
}
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;
}
}
}
return true;
}, {
name: 'jwtObjectGuard',
failedHint: 'is not a valid jwt object',
})
}

View File

@@ -1,7 +1,8 @@
// @pushrocks scope
import * as smartcrypto from '@push.rocks/smartcrypto';
import * as smartguard from '@push.rocks/smartguard';
export { smartcrypto };
export { smartcrypto, smartguard };
// thirdparty scope
import jsonwebtoken from 'jsonwebtoken';