travist / jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
http://www.travistidwell.com/jsencrypt
Other
6.68k stars 2.02k forks source link

nest.js jsencrypt does not work with error `window is not defined` #303

Open ingyeoking13 opened 11 months ago

ingyeoking13 commented 11 months ago
export class ClientDecryptAuthGuard implements CanActivate {
    constructor() {}
    async decrypt(req) {
        const JSEncrypt = (await import('jsencrypt')).default;
        const rsa = new JSEncrypt();
        rsa.setPrivateKey(keys.rsaPrivKey);
        const token = rsa.decrypt(req.token);
        const bodyOriginJson = CryptoJS.AES.decrypt(
            req.body['data'],
            token.toString(),
        ).toString(CryptoJS.enc.Utf8);
        return [bodyOriginJson, token];
    }

    async canActivate(context: ExecutionContext): Promise<boolean> {
        const req: Request = context.switchToHttp().getRequest();
        [req.body, req['token']] = await this.decrypt(req);
        const data = req.body;
        return true;
    }
}

ReferenceError: window is not defined at Object. (/Users/gaelim/Project/violet-project/violet-backend/node_modules/jsencrypt/bin/jsencrypt.js:18:4) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Function.Module._load (node:internal/modules/cjs/loader:958:12) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at /Users/gaelim/Project/violet-project/violet-backend/src/guards/clientDecryptAuth.guard.ts:15:28 at processTicksAndRejections (node:internal/process/task_queues:95:5) at ClientDecryptAuthGuard.decrypt (/Users/gaelim/Project/violet-project/violet-backend/src/guards/clientDecryptAuth.guard.ts:15:28)

using jsencrypt with nest js guard.