7.0.0 • Published 1 year ago

@betsys-nestjs/jwe v7.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

JWE library

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

Dependencies

PackageVersion
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)

        // ...
    }
}
7.0.0

1 year ago

6.1.0

1 year ago

5.1.0

2 years ago

6.0.0

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago