fix(core): update

This commit is contained in:
Philipp Kunz 2021-02-09 10:26:42 +00:00
parent 566a07a6b5
commit 397b6f13a7
4 changed files with 10143 additions and 667 deletions

10767
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,8 @@
"version": "1.0.10",
"private": false,
"description": "a package for handling jwt",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
@ -13,16 +13,16 @@
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.8",
"tslint": "^5.20.0",
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.52",
"@pushrocks/tapbundle": "^3.2.10",
"@types/node": "^14.14.25",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {
"@pushrocks/smartcrypto": "^1.0.7",
"@types/jsonwebtoken": "^8.3.4",
"@pushrocks/smartcrypto": "^1.0.9",
"@types/jsonwebtoken": "^8.5.0",
"jsonwebtoken": "^8.5.1"
},
"files": [

View File

@ -22,6 +22,7 @@ tap.test('should create a new jwt', async () => {
tap.test('should verify a jwt', async () => {
const data = await smartjwtInstance.verifyJWTAndGetData(testJwt);
console.log(data);
console.log(smartjwtInstance.publicKey.toPemString());
});
tap.test('should not verify a wrong jwt', async () => {
@ -38,4 +39,11 @@ tap.test('should not verify a wrong jwt', async () => {
expect(error).to.be.instanceOf(Error);
});
tap.test('should verify a jwt on another instance', async () => {
const secondSmartJwtInstance = new smartjwt.SmartJwt();
secondSmartJwtInstance.setPublicPemKeyForVerification(smartjwtInstance.publicKey.toPemString());
const result = secondSmartJwtInstance.verifyJWTAndGetData(testJwt);
console.log(result);
})
tap.start();

View File

@ -8,7 +8,7 @@ export interface ISmartJWTJSONKeypair {
/**
* A class to create and validate JWTs and their keys
*/
export class SmartJwt {
export class SmartJwt<T extends object = any> {
public smartcryptoInstance = new plugins.smartcrypto.Smartcrypto();
public publicKey: plugins.smartcrypto.PublicKey;
public privateKey: plugins.smartcrypto.PrivateKey;
@ -18,7 +18,7 @@ export class SmartJwt {
/**
* creates a JWT
*/
public async createJWT(payloadArg: any) {
public async createJWT(payloadArg: T) {
return plugins.jsonwebtoken.sign(payloadArg, this.privateKey.toPemString(), {
algorithm: 'RS256'
});
@ -27,10 +27,11 @@ export class SmartJwt {
/**
* checks a JWT
*/
public async verifyJWTAndGetData(jwtArg: string) {
return plugins.jsonwebtoken.verify(jwtArg, this.publicKey.toPemString(), {
public async verifyJWTAndGetData(jwtArg: string): Promise<T> {
const result = plugins.jsonwebtoken.verify(jwtArg, this.publicKey.toPemString(), {
algorithms: ['RS256']
});
return result as any;
}
/**
@ -74,6 +75,14 @@ export class SmartJwt {
this.setPublicKey(keypair.publicKey);
}
/**
* when you just want to validate something
* @param publicPemKey
*/
public setPublicPemKeyForVerification(publicPemKey: string) {
this.publicKey = plugins.smartcrypto.PublicKey.fromPemString(publicPemKey);
}
public async init() {
await this.createNewKeyPair();
}