@betsys-nestjs/jwe
v7.0.0
Published
This library is responsible for JWE encryption and decryption.
Downloads
18
Maintainers
Keywords
Readme
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 toforFeature
:
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)
// ...
}
}