feat(core): Enhanced JWT handling and key management

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

106
changelog.md Normal file
View File

@ -0,0 +1,106 @@
# Changelog
## 2024-08-26 - 2.1.0 - feat(core)
Enhanced JWT handling and key management
- Introduced the `IObjectWithJwt` type for improved type constraints.
- Added support for smartguard to create object guards with JWT validation.
- Upgraded dependencies to latest versions.
## 2024-05-29 - 2.0.4 - Maintenance
Minor updates and configuration changes.
- Updated project description.
- Modified tsconfig settings.
- Updated npmextra.json with githost.
## 2024-02-13 - 2.0.3 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2023-07-11 - 2.0.2 - Organizational Update
Switched to the new organizational scheme.
- Adjusted project structure to align with new organizational policies.
## 2022-12-22 - 2.0.1 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2022-10-24 - 2.0.0 - Major Update
Significant updates including breaking changes.
- **Breaking Change:** Switched project to ECMAScript modules.
## 2021-09-22 - 1.0.14 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2021-02-20 - 1.0.13 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2021-02-09 - 1.0.12 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2021-02-09 - 1.0.11 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2020-03-28 - 1.0.10 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.9 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.8 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.7 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.6 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.5 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-10-01 - 1.0.4 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-02-23 - 1.0.3 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-02-23 - 1.0.2 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.
## 2019-02-23 - 1.0.1 - Bug Fixes
Critical fixes in core functionalities.
- Fixed core issues.

View File

@ -12,16 +12,17 @@
"build": "(tsbuild)"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.72",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.86",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.11.17"
"@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsrun": "^1.2.49",
"@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.0.24",
"@types/node": "^22.5.0"
},
"dependencies": {
"@push.rocks/smartcrypto": "^2.0.4",
"@tsclass/tsclass": "^4.0.51",
"@types/jsonwebtoken": "^9.0.5",
"@push.rocks/smartguard": "^3.1.0",
"@tsclass/tsclass": "^4.1.2",
"@types/jsonwebtoken": "^9.0.6",
"jsonwebtoken": "^9.0.2"
},
"files": [

8915
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

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