feat(smartjwt): Add nested JWT functionality
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user