@betsys-nestjs/jwe
TypeScript icon, indicating that this package has built-in type declarations

7.0.0 • Public • Published

JWE library

This library is responsible for JWE encryption and decryption & Verification of JWS Sign.

Dependencies

Package Version
jose ^1.28.0
@nestjs/common ^10.0.0
reflect-metadata ^0.1.13
rxjs ^7.8.0

Usage

  • To start using this library simply import JweModule to your module.
@Module({
    imports: [
        JweModule.forFeature(jweConfig),
    ]
})
export class AppModule {
    // ...
}
  • Pass jweConfig argument to forFeature:
const jweConfig: JweConfig = {
    algorithm: 'A256KW',
};
  • Then inject JweService wherever you want to encrypt or decrypt something.
import {JweService} from '@betsys-nestjs/jwe';

class AppService {
    constructor(@InjectEncryptionLib() private encryptionService: JweService) {}

    encryptToken(plainText: string): string {
        const encryptJwk = JweService.makeEncryptionJwk(
            <EncryptionJwk>{ k: 'qZz2A36voILRDRayWUXIxHahHaD_VvvIDXtVZ_JWzNA', kty: 'oct' },
        );

        return this.encryptionService.encrypt(plainText, jwk)
    }

    decryptToken(cipherText): string {
        // ...

        // If you want to decrypt only (used for legacy token)
        const encryptJwk = JweService.makeEncryptionJwk(
            <EncryptionJwk>{ k: 'qZz2A36voILRDRayWUXIxHahHaD_VvvIDXtVZ_JWzNA', kty: 'oct' },
        );

        this.encryptionService.decrypt(token, encryptJwk)

        // If you want to decrypt and verify signature (used for V2 token)
        const signJwk = JweService.makeSignatureJwk(<SignatureJwk>{
            alg: 'RS256',
            e: 'AQAB',
            kty: 'RSA',
            n: V2_SIGN_KEY, // SIGN KEY - Download from login api on app init
            use: 'sig',
            kid: 'qX7-bj6Jq8u2alS8L5AsKJMdJyazH-Xc2qNzs83oT2M',
        });

        this.encryptionService.decrypt(token, encryptJwk, signJwk)

        // ...
    }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @betsys-nestjs/jwe

Weekly Downloads

4

Version

7.0.0

License

MIT

Unpacked Size

8.11 kB

Total Files

13

Last publish

Collaborators

  • betsys-development
  • pawelnowak1
  • andrejsoucek
  • jammie88
  • jiraspe2
  • jakubschneller
  • javor454
  • krizacekcz
  • flyrell