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

View File

@ -22,6 +22,7 @@ tap.test('should create a new jwt', async () => {
tap.test('should verify a jwt', async () => { tap.test('should verify a jwt', async () => {
const data = await smartjwtInstance.verifyJWTAndGetData(testJwt); const data = await smartjwtInstance.verifyJWTAndGetData(testJwt);
console.log(data); console.log(data);
console.log(smartjwtInstance.publicKey.toPemString());
}); });
tap.test('should not verify a wrong jwt', async () => { 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); 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(); tap.start();

View File

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