node-laravel-encryptor
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -5,2 +5,4 @@ "use strict"; | ||
const EncryptorError_1 = require("./lib/EncryptorError"); | ||
const phpSerializer_1 = require("./serializers/phpSerializer"); | ||
const jsonSerializer_1 = require("./serializers/jsonSerializer"); | ||
let crypto; | ||
@@ -14,3 +16,3 @@ try { | ||
class Base_encryptor { | ||
constructor(options) { | ||
constructor(options, driver) { | ||
this.key_length = 64; | ||
@@ -21,13 +23,7 @@ this.valid_key_lengths = [32, 64]; | ||
this.options = Object.assign({}, { serialize_mode: this.default_serialize_mode }, options); | ||
this.secret = Buffer.from(this.options.key, 'base64'); | ||
this.serialize_driver = new Serializer_1.Serializer(this.options); | ||
this.setSerializerDriver(driver); | ||
this.setAlgorithm(); | ||
this.secret = Base_encryptor.prepareAppKey(this.options.key); | ||
this.random_bytes = this.options.random_bytes ? this.options.random_bytes : this.random_bytes; | ||
} | ||
setAlgorithm() { | ||
if (this.options.key_length && this.valid_key_lengths.indexOf(this.options.key_length) < 0) | ||
Base_encryptor.throwError('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.'); | ||
this.algorithm = this.options.key_length ? | ||
`aes-${this.options.key_length * 4}-cbc` : `aes-${this.key_length * 4}-cbc`; | ||
} | ||
encryptIt(data) { | ||
@@ -64,2 +60,53 @@ return this | ||
} | ||
static prepareAppKey(key) { | ||
if (!key) | ||
Base_encryptor.throwError('no app key given'); | ||
return Buffer.from(key, 'base64'); | ||
} | ||
setSerializerDriver(driver) { | ||
if (driver) { | ||
if (!Base_encryptor.validateSerializerDriver(driver)) | ||
Base_encryptor.throwError('validateSerializerDriver'); | ||
this.serialize_driver = new Serializer_1.Serializer(new driver); | ||
this.options.serialize_mode = 'custom'; | ||
} | ||
else { | ||
this.serialize_driver = new Serializer_1.Serializer(this.pickSerializeDriver()); | ||
} | ||
} | ||
static validateSerializerDriver(driver) { | ||
try { | ||
const custom_driver = new driver; | ||
return Base_encryptor | ||
.validateSerializerImplementsSerializerInterface(custom_driver); | ||
} | ||
catch (e) { | ||
Base_encryptor.throwError('validateSerializerDriver'); | ||
} | ||
} | ||
static validateSerializerImplementsSerializerInterface(driver) { | ||
return (typeof driver['serialize'] === 'function') | ||
&& (typeof driver['unSerialize'] === 'function'); | ||
} | ||
pickSerializeDriver() { | ||
if (!this.options.serialize_mode) | ||
this.options.serialize_mode = 'php'; | ||
switch (this.options.serialize_mode) { | ||
case 'json': { | ||
return new jsonSerializer_1.JsonSerializer; | ||
} | ||
case 'php': { | ||
return new phpSerializer_1.PhpSerializer; | ||
} | ||
default: { | ||
throw new EncryptorError_1.EncryptorError(`Serializer Encryptor Class unknown option ${this.options.serialize_mode} serialize_mode`); | ||
} | ||
} | ||
} | ||
setAlgorithm() { | ||
if (this.options.key_length && this.valid_key_lengths.indexOf(this.options.key_length) < 0) | ||
Base_encryptor.throwError('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.'); | ||
this.algorithm = this.options.key_length ? | ||
`aes-${this.options.key_length * 4}-cbc` : `aes-${this.key_length * 4}-cbc`; | ||
} | ||
prepareDataToCipher(data, force_serialize) { | ||
@@ -66,0 +113,0 @@ if (force_serialize === true && this.serialize_driver.getDriverName() === 'PhpSerializer') { |
@@ -6,4 +6,4 @@ "use strict"; | ||
class Encryptor extends base_encryptor_1.Base_encryptor { | ||
constructor(options) { | ||
super(options); | ||
constructor(options, driver) { | ||
super(options, driver); | ||
} | ||
@@ -10,0 +10,0 @@ encrypt(data, force_serialize) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const EncryptorError_1 = require("./EncryptorError"); | ||
const jsonSerializer_1 = require("../serializers/jsonSerializer"); | ||
const phpSerializer_1 = require("../serializers/phpSerializer"); | ||
class Serializer { | ||
constructor(options) { | ||
this.options = options; | ||
if (!this.options.serialize_mode) | ||
this.options.serialize_mode = 'php'; | ||
switch (this.options.serialize_mode) { | ||
case 'json': { | ||
this.driver = new jsonSerializer_1.JsonSerializer; | ||
break; | ||
} | ||
case 'php': { | ||
this.driver = new phpSerializer_1.PhpSerializer; | ||
break; | ||
} | ||
default: { | ||
throw new EncryptorError_1.EncryptorError(`Serializer Encryptor Class unknown option ${this.options.serialize_mode} serialize_mode`); | ||
} | ||
} | ||
constructor(driver) { | ||
this.driver = driver; | ||
} | ||
@@ -25,0 +7,0 @@ serialize(data) { |
{ | ||
"name": "node-laravel-encryptor", | ||
"engines": { | ||
"node": ">=8.16.1", | ||
"npm": ">=6.4.1" | ||
}, | ||
"module": "dist/index.js", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "node version Laravel Illuminate/Encryption/Encrypter.php", | ||
@@ -6,0 +10,0 @@ "main": "dist/index.js", |
224
README.md
@@ -14,4 +14,10 @@ # node-laravel-encryptor | ||
the data before ciphering it. | ||
# Install | ||
## Prerequisites | ||
* NodeJs `>=v8.16.1 (npm v6.4.1)` | ||
## Laravel Compatibility | ||
* Laravel `>=5.4.30` | ||
## Install | ||
```sh | ||
@@ -21,4 +27,4 @@ $> npm i node-laravel-encryptor | ||
# Use | ||
## Async mode | ||
## Use | ||
### Async mode | ||
```js | ||
@@ -36,3 +42,3 @@ const {Encryptor} = require('node-laravel-encryptor'); | ||
## Sync mode | ||
### Sync mode | ||
```js | ||
@@ -46,6 +52,9 @@ const enc = encryptor.encryptSync({foo: 'bar'}); | ||
### Serialize `<php>`|`<json>` | ||
## Serialize `<php>`|`<json>`|`<custom>` | ||
Encryptor let you chose between `php-serialize` npm package or `JSON` node native implementation to serialize the data. | ||
Encryptor let you chose between `php-serialize` npm package or `JSON` node native implementation to serialize the data out of the box. | ||
If you need to use other serialize library, like `mspack` or any other custom lib, Encryptor let you inject, at the constructor or at runtime with | ||
`setSerializerDriver(your_lib)`, your custom Serializer Class. | ||
> If you use this module with the intend to be able **to read and write ciphered data to/from Laravel** you should instance Encryptor class without any `serialize_mode` option, just the defaults. | ||
@@ -81,31 +90,71 @@ | ||
# Encryptor Class | ||
#### Write your custom Serializer | ||
#### Prerequisites | ||
* adhere to Serializer Interface contract | ||
Your custom Serializer must implement this two methods: | ||
```ts | ||
export interface Serialize_Interface { | ||
serialize(data: any): string; | ||
unSerialize(data: string): any; | ||
} | ||
``` | ||
## Encryptor Class | ||
#### Constructor | ||
* arguments: | ||
* options `<object>` {key, key_length} | ||
* key: `<string>` APP_KEY without `base64:` | ||
* key_length: `<number>` [optional] [default] `<64>` values 32|64 for aes-[128]-cbc aes-[256]-cbc | ||
* serialize_mode: `<string>` [optional] [default] `<php>` values `<php>`|`<json>` | ||
* options: `<object>` {key, key_length} | ||
* key: `<string>` APP_KEY without `base64:` | ||
* key_length: `<number>` [optional] [default] `<64>` values 32|64 for aes-[128]-cbc aes-[256]-cbc | ||
* serialize_mode: `<string>` [optional] [default] `<php>` values `<php>`|`<json>` | ||
* serialize driver: `class to serialize` [optional] | ||
* throw EncryptorError | ||
if no `key_length` is given default is 64. | ||
### Methods | ||
#### encrypt(data, force_serialize) | ||
> Will encrypt data with MAC signature, and return a Promise with encrypted base64 string. | ||
## Methods | ||
### encrypt(data, force_serialize) | ||
With `force_serialize` (only apply with `serialize_mode:'php'`) you can force Encryptor to serialize data | ||
before cipher even if data is not an object. | ||
`force_serialize`, will not take any effect if Encryptor is using other serializer driver than `php-serialize` module. | ||
* arguments: | ||
* data: `<string>|<object>|<number>` | ||
* data: `<string>`|`<object>`|`<number>` | ||
* force_serialize: `<boolean>` [optional] | ||
* return `<string>` base64 | ||
* return Promise `<string>` base64 json encoded object `{iv, value, mac}` | ||
* throw EncryptorError | ||
### decrypt(data) | ||
#### decrypt(data) | ||
> Will decrypt data with MAC signature verification, and return original data. | ||
* arguments: | ||
* data: `<string>|<object>|<number>` | ||
* return `<string>|<object>` | ||
* data: `<string>`|`<object>`|`<number>` | ||
* return `<string>`|`<object>` | ||
* throw EncryptorError | ||
#### encryptSync(data, force_serialize) | ||
> Will encrypt data with MAC signature, and return encrypted base64 string. | ||
With `force_serialize` (only apply with `serialize_mode:'php'`) you can force Encryptor to serialize data | ||
before cipher even if data is not an object. | ||
`force_serialize`, will not take any effect if Encryptor is using other serializer driver than `php-serialize` module. | ||
* arguments: | ||
* data: `<string>`|`<object>`|`<number>` | ||
* force_serialize: `<boolean>` [optional] | ||
* return `<string>` base64 json encoded object `{iv, value, mac}` | ||
* throw EncryptorError | ||
Encrypt and Decrypt methods will serialize or unserialize data if needed. | ||
### Static generateRandomKey() | ||
#### setSerializerDriver(custom_Serializer_lib) | ||
> Will inject custom serializer driver to Encryptor Class | ||
* arguments: | ||
* custom_Serializer_lib: `object class serialize module` | ||
* return `<void>` | ||
* throw EncryptorError | ||
#### Static generateRandomKey() | ||
> Will generate valid App_key a la Laravel | ||
* arguments: | ||
* length: `<number>` [optional], default 32 | ||
@@ -115,3 +164,4 @@ * return `<string>` base64 | ||
### Static static_decipher(key, data) | ||
#### Static static_decipher(key, data) | ||
> will decipher data | ||
* arguments: | ||
@@ -123,3 +173,4 @@ * key: `<string>` base64 encoded key | ||
### Static static_cipher(key, data, [cb]) | ||
#### Static static_cipher(key, data, [cb]) | ||
> will cipher data | ||
* arguments: | ||
@@ -132,10 +183,7 @@ * key: `<string>` base64 encoded key | ||
# Tests | ||
## Tests | ||
```sh | ||
$> npm run test | ||
``` | ||
To be able to run PHP test you must have installed: | ||
To be able to run `PHP test` you should install: | ||
* PHP >= 7.1.3 | ||
* PHP `>= 7.1.3` | ||
* OpenSSL PHP Extension | ||
@@ -151,46 +199,50 @@ * Mbstring PHP Extension | ||
Testing node Laravel Encryptor | ||
Test Encryptor Cipher/Decipher serialize_mode: Native JSON | ||
✓ should cipher and decipher text | ||
✓ should cipher and decipher object | ||
✓ should cipher and decipher with no key_length defined | ||
✓ should cipher and decipher a number | ||
✓ should cipher and decipher Sync Mode | ||
Test Encryptor Cipher/Decipher serialize_mode: PHP Serialize | ||
✓ should cipher and decipher text | ||
✓ should cipher and decipher object | ||
✓ should set serialized_mode to php-serialized if no serialize_mode given | ||
✓ should force serialize data input when serializer driver is php-serialized and data is not an object | ||
✓ should cipher and decipher with no key_length defined | ||
✓ should cipher and decipher a number | ||
✓ should cipher and decipher Sync Mode | ||
Test Encryptor static methods | ||
✓ should generate a valid App key | ||
✓ should Cipher/deCipher correctly using static Encryptor methods | ||
✓ should Cipher correctly using static Encryptor method with callback function | ||
Test Encryptor Class Errors | ||
✓ should throw EncryptorError Error Type | ||
✓ should throw 'encrypt no data given' EncryptorError when data to encrypt is null | ||
✓ should throw 'decrypt no data given' EncryptorError when data to decrypt is null | ||
✓ should throw 'not valid Key' EncryptorError when cipher | ||
✓ should throw 'not valid algorithm' EncryptorError when cipher | ||
✓ should throw 'not valid Json' EncryptorError when decipher | ||
✓ should throw 'invalid MAC signature' EncryptorError when decipher | ||
✓ should throw 'invalid Payload' EncryptorError when decipher | ||
✓ should throw 'invalid iv length' EncryptorError when decipher | ||
✓ should throw unknown Serialize EncryptorError Error when options.serialize_mode != json/php | ||
Test Encryptor compatibility with Laravel Illuminate/Encryption/Encrypter | ||
✓ should decipher data at Laravel correctly with serialize_mode php (64ms) | ||
✓ should decipher from Laravel correctly with serialize_mode php (57ms) | ||
✓ should decipher data, Sync Mode, at Laravel correctly with serialize_mode php (56ms) | ||
Test integration with Express cookie | ||
✓ should create one request to Express aSync Mode, receive cookie and decipher (39ms) | ||
✓ should create one request to Express Sync Mode, receive cookie and decipher | ||
30 passing (296ms) | ||
Testing node Laravel Encryptor | ||
Test Encryptor Cipher/Decipher serialize_mode: Native JSON | ||
✓ should cipher and decipher text | ||
✓ should cipher and decipher object | ||
✓ should cipher and decipher with no key_length defined | ||
✓ should cipher and decipher a number | ||
✓ should cipher and decipher Sync Mode | ||
Test Encryptor Cipher/Decipher serialize_mode: PHP Serialize | ||
✓ should cipher and decipher text | ||
✓ should cipher and decipher object | ||
✓ should cipher and decipher with no key_length defined | ||
✓ should cipher and decipher a number | ||
✓ should cipher and decipher Sync Mode | ||
Test Encryptor static methods | ||
✓ should generate a valid App key | ||
✓ should Cipher/deCipher correctly using static Encryptor methods | ||
✓ should Cipher correctly using static Encryptor method with callback function | ||
Test Encryptor Class Errors | ||
✓ should throw 'EncryptorError' Error Type | ||
✓ should throw 'encrypt no data given' EncryptorError when data to encrypt is null | ||
✓ should throw 'decrypt no data given' EncryptorError when data to decrypt is null | ||
✓ should throw 'not valid Key' EncryptorError when key not valid | ||
✓ should throw 'no app key given' EncryptorError when null options | ||
✓ should throw 'not valid algorithm' EncryptorError when algorithm not valid | ||
✓ should throw 'not valid Json' EncryptorError when decipher not valid payload | ||
✓ should throw 'invalid MAC signature' EncryptorError when deciphering tampered signature | ||
✓ should throw 'invalid Payload' EncryptorError when decipher cannot validate payload | ||
✓ should throw 'invalid iv length' EncryptorError when deciphering with invalid IV | ||
✓ should throw 'validateSerializerDriver' EncryptorError when custom serializer driver not implements Serializer interface | ||
✓ should throw 'Serializer Encryptor Class unknown option' EncryptorError when options.serialize_mode != json/php | ||
Test Encryptor Class Serialize driver injection | ||
✓ should set serialized_mode to php-serialized if no serialize_mode given | ||
✓ should force serialize data input when serializer driver is php-serialized and data is not an object | ||
✓ should inject custom serializer driver in constructor | ||
✓ should inject custom serializer driver at runtime | ||
✓ should use injected serializer driver to serialize/deserialize data | ||
Test Encryptor compatibility with Laravel Illuminate/Encryption/Encrypter | ||
✓ should decipher data at Laravel correctly with serialize_mode php (60ms) | ||
✓ should decipher from Laravel correctly with serialize_mode php (56ms) | ||
✓ should decipher data, Sync Mode, at Laravel correctly with serialize_mode php (58ms) | ||
Test integration with Express cookie | ||
✓ should create one request to Express aSync Mode, receive cookie and decipher (39ms) | ||
✓ should create one request to Express Sync Mode, receive cookie and decipher | ||
35 passing (296ms) | ||
``` | ||
# Artillery test | ||
### Artillery test | ||
@@ -204,5 +256,5 @@ In order to run Artillery integration test and stress test with aSync|Sync mode we have | ||
``` | ||
## Run Artillery expect test | ||
#### Run Artillery expect test | ||
#### start server running in async mode | ||
##### start server running in async mode | ||
```bash | ||
@@ -235,3 +287,3 @@ $> npm run artillery_server_async | ||
#### start server running in sync mode | ||
##### start server running in sync mode | ||
```bash | ||
@@ -264,5 +316,5 @@ $> npm run artillery_server_sync | ||
## Run Artillery stress test | ||
### Run Artillery stress test | ||
#### start server running in async mode | ||
##### start server running in async mode | ||
```bash | ||
@@ -272,3 +324,3 @@ $> npm run artillery_server_async | ||
#### start server running in sync mode | ||
##### start server running in sync mode | ||
```bash | ||
@@ -278,3 +330,3 @@ $> npm run artillery_server_sync | ||
#### run test | ||
##### run test | ||
```bash | ||
@@ -284,3 +336,3 @@ $> npm run artillery | ||
#### Async Mode | ||
##### Async Mode | ||
```sh | ||
@@ -305,3 +357,3 @@ All virtual users finished | ||
#### Sync Mode | ||
##### Sync Mode | ||
```sh | ||
@@ -326,3 +378,3 @@ All virtual users finished | ||
### [Dont block the event loop](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/) | ||
#### [Dont block the event loop](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/) | ||
>Blocking the Event Loop: Node core modules | ||
@@ -345,3 +397,3 @@ > | ||
## Laravel Encrypter format: | ||
#### Laravel Encrypter format: | ||
@@ -355,13 +407,13 @@ Laravel only allows `AES-128-CBC` `AES-256-CBC`. | ||
"value": "encrypted data", | ||
"mac": "Hash HMAC" | ||
"mac": "Hash HMAC signature" | ||
} | ||
``` | ||
## Dependencies | ||
### Dependencies | ||
* [php-serialize](https://github.com/steelbrain/php-serialize#readme) | ||
## Contributing | ||
### Contributing | ||
Pull requests are welcome! | ||
## License | ||
### License | ||
[MIT](https://choosealicense.com/licenses/mit/) | ||
import {Serializer} from "./lib/Serializer"; | ||
import {EncryptorError} from "./lib/EncryptorError"; | ||
import {PhpSerializer} from "./serializers/phpSerializer"; | ||
import {JsonSerializer} from "./serializers/jsonSerializer"; | ||
import cryptTypes from "crypto"; | ||
import {PhpSerializer} from "./serializers/phpSerializer"; | ||
let crypto; | ||
@@ -35,2 +37,3 @@ //Determining if crypto support is unavailable | ||
private static readonly app_key_length = 32; | ||
/** serialize driver */ | ||
@@ -47,3 +50,3 @@ private serialize_driver: Serializer; | ||
random_bytes?: number, | ||
serialize_mode?: 'json'|'php' | ||
serialize_mode?: 'json'|'php'|'custom' | ||
}; | ||
@@ -58,13 +61,11 @@ | ||
* @param options {key: string, key_length?: number } | ||
* @param driver | ||
*/ | ||
constructor(options) { | ||
constructor(options, driver?: Serializer) { | ||
this.options = Object.assign({}, {serialize_mode: this.default_serialize_mode}, options); | ||
this.secret = Buffer.from(this.options.key, 'base64'); | ||
this.serialize_driver = new Serializer(this.options); | ||
this.setSerializerDriver(driver); | ||
this.setAlgorithm(); | ||
this.secret = Base_encryptor.prepareAppKey(this.options.key); | ||
this.random_bytes = this.options.random_bytes ? this.options.random_bytes : this.random_bytes; | ||
@@ -74,19 +75,2 @@ } | ||
/** | ||
* setAlgorithm | ||
* will populate this.algorithm with valid one aes-[128]-cbc aes-[256]-cbc | ||
* from options.key_length or this.key_length | ||
* | ||
* if there is an error will push it to errors (and return as reject at public methods) | ||
*/ | ||
protected setAlgorithm() { | ||
if (this.options.key_length && this.valid_key_lengths.indexOf(this.options.key_length) < 0) | ||
Base_encryptor.throwError( | ||
'The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.' | ||
); | ||
this.algorithm = this.options.key_length ? | ||
`aes-${this.options.key_length * 4}-cbc` : `aes-${this.key_length * 4}-cbc`; | ||
} | ||
/** | ||
* encryptIt | ||
@@ -124,7 +108,6 @@ * | ||
protected decryptIt(encrypted: string): any{ | ||
let payload; | ||
try { | ||
payload = JSON.parse(encrypted); | ||
payload = JSON.parse(encrypted); | ||
} catch (e) { | ||
@@ -151,2 +134,93 @@ Base_encryptor.throwError('Encryptor decryptIt cannot parse json') | ||
/** | ||
* prepareAppKey | ||
* | ||
* @param key | ||
*/ | ||
static prepareAppKey(key: string): Buffer{ | ||
if(! key) | ||
Base_encryptor.throwError('no app key given'); | ||
return Buffer.from(key, 'base64'); | ||
} | ||
/** | ||
* set serializer driver | ||
* | ||
* @param driver | ||
*/ | ||
setSerializerDriver(driver?: any){ | ||
if(driver) { | ||
if(! Base_encryptor.validateSerializerDriver(driver)) | ||
Base_encryptor.throwError('validateSerializerDriver'); | ||
this.serialize_driver = new Serializer(new driver); | ||
this.options.serialize_mode = 'custom'; | ||
}else{ | ||
this.serialize_driver = new Serializer(this.pickSerializeDriver()); | ||
} | ||
} | ||
/** | ||
* validateSerializerDriver | ||
* | ||
* @param driver | ||
*/ | ||
static validateSerializerDriver(driver: any){ | ||
try { | ||
const custom_driver = new driver; | ||
return Base_encryptor | ||
.validateSerializerImplementsSerializerInterface(custom_driver) | ||
}catch (e) { | ||
Base_encryptor.throwError('validateSerializerDriver') | ||
} | ||
} | ||
/** | ||
* validateSerializerDriver | ||
* | ||
* @param driver | ||
*/ | ||
static validateSerializerImplementsSerializerInterface(driver: any){ | ||
return (typeof driver['serialize'] === 'function') | ||
&& (typeof driver['unSerialize'] === 'function') | ||
} | ||
/** | ||
* pickSerializeDriver | ||
*/ | ||
pickSerializeDriver(){ | ||
if(! this.options.serialize_mode) | ||
this.options.serialize_mode ='php'; | ||
switch (this.options.serialize_mode) { | ||
case 'json': { | ||
return new JsonSerializer; | ||
} | ||
case 'php': { | ||
return new PhpSerializer; | ||
} | ||
default: { | ||
throw new EncryptorError( | ||
`Serializer Encryptor Class unknown option ${this.options.serialize_mode} serialize_mode` | ||
) | ||
} | ||
} | ||
} | ||
/** | ||
* setAlgorithm | ||
* will populate this.algorithm with valid one aes-[128]-cbc aes-[256]-cbc | ||
* from options.key_length or this.key_length | ||
* | ||
* if there is an error will push it to errors (and return as reject at public methods) | ||
*/ | ||
protected setAlgorithm() { | ||
if (this.options.key_length && this.valid_key_lengths.indexOf(this.options.key_length) < 0) | ||
Base_encryptor.throwError( | ||
'The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.' | ||
); | ||
this.algorithm = this.options.key_length ? | ||
`aes-${this.options.key_length * 4}-cbc` : `aes-${this.key_length * 4}-cbc`; | ||
} | ||
/** | ||
* Prepare Data | ||
@@ -153,0 +227,0 @@ * will receive data from this.encrypt(data) |
export interface Serialize_Interface { | ||
serialize(data: any): any; | ||
unSerialize(data: any): any; | ||
serialize(data: any): string; | ||
unSerialize(data: string): any; | ||
} |
import {Base_encryptor} from "./base_encryptor"; | ||
import {EncryptorError} from "./lib/EncryptorError"; | ||
import {Serializer} from "./lib/Serializer"; | ||
@@ -34,5 +35,6 @@ // Cipher steps: | ||
* @param options {key: string, key_length?: number, random_bytes?: number = 8 } | ||
* @param driver | ||
*/ | ||
constructor(options) { | ||
super(options); | ||
constructor(options, driver?: Serializer) { | ||
super(options, driver); | ||
} | ||
@@ -39,0 +41,0 @@ |
@@ -1,35 +0,7 @@ | ||
import {EncryptorError} from "./EncryptorError"; | ||
import {Serialize_Interface} from "../contracts/Serialize_Interface"; | ||
import {JsonSerializer} from "../serializers/jsonSerializer"; | ||
import {PhpSerializer} from "../serializers/phpSerializer"; | ||
export class Serializer implements Serialize_Interface{ | ||
export class Serializer{ | ||
/** Driver */ | ||
private driver: Serialize_Interface; | ||
constructor(private driver: Serialize_Interface) {} | ||
constructor(protected options) { | ||
//make default option to php-serialize | ||
if(! this.options.serialize_mode) | ||
this.options.serialize_mode ='php'; | ||
switch (this.options.serialize_mode) { | ||
case 'json': { | ||
this.driver = new JsonSerializer; | ||
break; | ||
} | ||
case 'php': { | ||
this.driver = new PhpSerializer; | ||
break; | ||
} | ||
default: { | ||
throw new EncryptorError( | ||
`Serializer Encryptor Class unknown option ${this.options.serialize_mode} serialize_mode` | ||
) | ||
} | ||
} | ||
} | ||
/** | ||
@@ -57,2 +29,5 @@ * Serialize | ||
/** | ||
* get driver class name | ||
*/ | ||
getDriverName(){ | ||
@@ -59,0 +34,0 @@ return this.driver.constructor.name |
@@ -27,3 +27,2 @@ import {Serialize_Interface} from "../contracts/Serialize_Interface"; | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
import {describe} from 'mocha'; | ||
import enc_test from './suites/encryptor.test'; | ||
@@ -6,4 +8,5 @@ import static_methods_test from './suites/encryptor.static.test'; | ||
import integrator from './suites/express.test'; | ||
import {describe} from 'mocha'; | ||
import driver from './suites/serializer.test'; | ||
describe('Testing node Laravel Encryptor', function() { | ||
@@ -14,4 +17,5 @@ describe('Test Encryptor Cipher/Decipher serialize_mode: Native JSON', enc_test.bind(this, 'json')); | ||
describe('Test Encryptor Class Errors', error_test.bind(this)); | ||
describe('Test Encryptor Class Serialize driver injection', driver.bind(this)); | ||
describe('Test Encryptor compatibility with Laravel Illuminate/Encryption/Encrypter', laravel.bind(this)); | ||
describe('Test integration with Express cookie', integrator.bind(this)); | ||
}); |
@@ -1,3 +0,1 @@ | ||
import {PhpSerializer} from "../../src/serializers/phpSerializer"; | ||
const {it} = require("mocha"); | ||
@@ -39,29 +37,2 @@ const {expect} = require("chai"); | ||
if ('php' === serialize_mode){ | ||
it('should set serialized_mode to php-serialized if no serialize_mode given', done => { | ||
const encryptor = new Encryptor({key}); | ||
expect(encryptor.serialize_driver.driver.constructor.name === 'PhpSerializer') | ||
.to.be.equal(true); | ||
encryptor | ||
.encrypt(one_object) | ||
.then(res => { | ||
expect(decipher(res)).to.have.property('foo').equals('bar'); | ||
done(); | ||
}) | ||
}); | ||
it('should force serialize data input when serializer driver is php-serialized and data is not an object', done => { | ||
let encryptor = new Encryptor({key, serialize_mode}); | ||
encryptor | ||
.encrypt('baz', true) | ||
.then(encrypted => { | ||
const decrypted = encryptor.decrypt(encrypted); | ||
expect(encryptor.getRawDecrypted() === 's:3:"baz";').to.be.equal(true); | ||
expect(decrypted).equals('baz'); | ||
done(); | ||
}) | ||
}); | ||
} | ||
it('should cipher and decipher with no key_length defined', done => { | ||
@@ -68,0 +39,0 @@ const encryptor = new Encryptor({key, serialize_mode}); |
@@ -10,3 +10,3 @@ const {it} = require("mocha"); | ||
it('should throw EncryptorError Error Type', done => { | ||
it('should throw \'EncryptorError\' Error Type', done => { | ||
const encryptor = new Encryptor({key}); | ||
@@ -47,3 +47,3 @@ try { | ||
it("should throw 'not valid Key' EncryptorError when cipher", done => { | ||
it("should throw 'not valid Key' EncryptorError when key not valid", done => { | ||
const encryptor = new Encryptor({key: 'foobarbaz'}); | ||
@@ -61,4 +61,14 @@ | ||
it("should throw 'not valid algorithm' EncryptorError when cipher", done => { | ||
it("should throw 'no app key given' EncryptorError when null options", done => { | ||
try { | ||
new Encryptor(); | ||
}catch (e) { | ||
expect(e.name).equal('EncryptorError'); | ||
expect(e.message).equal('no app key given'); | ||
done() | ||
} | ||
}); | ||
it("should throw 'not valid algorithm' EncryptorError when algorithm not valid", done => { | ||
try { | ||
new Encryptor({ | ||
@@ -76,3 +86,3 @@ key, | ||
it("should throw 'not valid Json' EncryptorError when decipher", done => { | ||
it("should throw 'not valid Json' EncryptorError when decipher not valid payload", done => { | ||
const encryptor = new Encryptor({ | ||
@@ -92,3 +102,3 @@ key, | ||
it("should throw 'invalid MAC signature' EncryptorError when decipher", done => { | ||
it("should throw 'invalid MAC signature' EncryptorError when deciphering tampered signature", done => { | ||
const encryptor = new Encryptor({key}); | ||
@@ -116,3 +126,3 @@ | ||
it("should throw 'invalid Payload' EncryptorError when decipher", done => { | ||
it("should throw 'invalid Payload' EncryptorError when decipher cannot validate payload", done => { | ||
const encryptor = new Encryptor({key}); | ||
@@ -138,3 +148,3 @@ | ||
it("should throw 'invalid iv length' EncryptorError when decipher", done => { | ||
it("should throw 'invalid iv length' EncryptorError when deciphering with invalid IV", done => { | ||
const encryptor = new Encryptor({key}); | ||
@@ -162,11 +172,26 @@ | ||
it('should throw unknown Serialize EncryptorError Error when options.serialize_mode != json/php ', done => { | ||
try { | ||
new Encryptor({key, serialize_mode: 'foo'}); | ||
} catch (e) { | ||
expect(e.name).equal('EncryptorError'); | ||
expect(e.message).equal('Serializer Encryptor Class unknown option foo serialize_mode'); | ||
done(); | ||
} | ||
it('should throw \'validateSerializerDriver\' EncryptorError when custom serializer driver not implements Serializer interface', | ||
done => { | ||
const badSerializerDriver = | ||
module.exports = | ||
class badSerializerDriver{serialize(){}}; | ||
try { | ||
new Encryptor({key}, badSerializerDriver); | ||
}catch (e) { | ||
expect(e.name).equal('EncryptorError'); | ||
expect(e.message).equal('validateSerializerDriver'); | ||
done() | ||
} | ||
}); | ||
it('should throw \'Serializer Encryptor Class unknown option\' EncryptorError when options.serialize_mode != json/php ', | ||
done => { | ||
try { | ||
new Encryptor({key, serialize_mode: 'foo'}); | ||
} catch (e) { | ||
expect(e.name).equal('EncryptorError'); | ||
expect(e.message).equal('Serializer Encryptor Class unknown option foo serialize_mode'); | ||
done(); | ||
} | ||
}); | ||
} |
@@ -16,4 +16,3 @@ { | ||
"files": [ | ||
"src/index.ts", | ||
"src/call.ts" | ||
"src/index.ts" | ||
], | ||
@@ -20,0 +19,0 @@ "exclude": [ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
84087
38
1851
401
0