fix: wrap raw base64 public key with PEM headers for Node crypto

Ozonetel returns raw base64 public key without PEM headers. Node's
crypto.publicEncrypt requires PEM format.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 21:09:00 +05:30
parent 7b178f9dc7
commit d97d73dd1a

View File

@@ -54,10 +54,14 @@ export class OzonetelAdminAuthService implements OnModuleInit {
return this.login(); return this.login();
} }
private rsaEncrypt(publicKeyPem: string, plaintext: string): string { private rsaEncrypt(publicKeyRaw: string, plaintext: string): string {
// Ozonetel returns raw base64 without PEM headers — wrap it
const pem = publicKeyRaw.includes('-----BEGIN')
? publicKeyRaw
: `-----BEGIN PUBLIC KEY-----\n${publicKeyRaw}\n-----END PUBLIC KEY-----`;
const buffer = Buffer.from(plaintext, 'utf8'); const buffer = Buffer.from(plaintext, 'utf8');
const encrypted = publicEncrypt( const encrypted = publicEncrypt(
{ key: publicKeyPem, padding: cryptoConstants.RSA_PKCS1_PADDING }, { key: pem, padding: cryptoConstants.RSA_PKCS1_PADDING },
buffer, buffer,
); );
return encrypted.toString('base64'); return encrypted.toString('base64');