node-laravel-encryptor
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -0,1 +1,8 @@ | ||
# v2.0.3 | ||
* `JsonSerializer.serialize` now stringify an object | ||
a la ExpressJS cookie json serializer style, | ||
adding string `j:` to stringify object. | ||
* `JsonSerializer.unserialize` now checks before parsing if string | ||
starts with `j:` | ||
# v2.0.2 | ||
@@ -2,0 +9,0 @@ * Custom Serializer can be injected into Encryptor |
@@ -5,13 +5,28 @@ "use strict"; | ||
serialize(data) { | ||
return JSON.stringify(data); | ||
if (typeof data === 'object') | ||
return 'j:' + JSON.stringify(data); | ||
return String(data); | ||
} | ||
unSerialize(data) { | ||
unSerialize(str) { | ||
if (typeof str !== 'string') | ||
return undefined; | ||
if (JsonSerializer.isJson(str)) { | ||
return JsonSerializer.parseJson(str); | ||
} | ||
else { | ||
return str; | ||
} | ||
} | ||
static parseJson(str) { | ||
try { | ||
return JSON.parse(data); | ||
return JSON.parse(str.slice(2)); | ||
} | ||
catch (e) { | ||
return data; | ||
catch (err) { | ||
return undefined; | ||
} | ||
} | ||
static isJson(str) { | ||
return str.substr(0, 2) === 'j:'; | ||
} | ||
} | ||
exports.JsonSerializer = JsonSerializer; |
@@ -8,3 +8,3 @@ { | ||
"module": "dist/index.js", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "node version Laravel Illuminate/Encryption/Encrypter.php", | ||
@@ -11,0 +11,0 @@ "main": "dist/index.js", |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/AdSegura/node-laravel-encryptor.svg?branch=master)](https://travis-ci.org/AdSegura/node-laravel-encryptor) | ||
# node-laravel-encryptor | ||
@@ -2,0 +4,0 @@ |
@@ -11,3 +11,6 @@ import {Serialize_Interface} from "../contracts/Serialize_Interface"; | ||
serialize(data: any): string { | ||
return JSON.stringify(data) | ||
if(typeof data === 'object') | ||
return 'j:' + JSON.stringify(data); | ||
return String(data); | ||
} | ||
@@ -19,11 +22,35 @@ | ||
* | ||
* @param data | ||
* @param str | ||
*/ | ||
unSerialize(data: any):any { | ||
unSerialize(str: string):any { | ||
if (typeof str !== 'string') return undefined; | ||
if(JsonSerializer.isJson(str)){ | ||
return JsonSerializer.parseJson(str); | ||
} else { | ||
return str; | ||
} | ||
} | ||
/** | ||
* Parse JSON | ||
* @param str | ||
*/ | ||
static parseJson(str: string): any{ | ||
try { | ||
return JSON.parse(data) | ||
} catch (e) { | ||
return data; | ||
return JSON.parse(str.slice(2)) | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
/** | ||
* Is Json, a la expressJs, | ||
* if str is 'j:{"foo": "bar"}' if JSON | ||
* | ||
* @param str | ||
*/ | ||
static isJson(str: string): any { | ||
return str.substr(0, 2) === 'j:' | ||
} | ||
} |
@@ -17,3 +17,3 @@ const express = require('express'); | ||
constructor(private options: any){ | ||
constructor(private options: any) { | ||
@@ -31,4 +31,4 @@ this.express = new express(); | ||
if(options.async) { | ||
if(! options.artillery) | ||
if (options.async) { | ||
if (!options.artillery) | ||
this.cookieMiddleware = this.cookieAsync(options.cookie, this.server_id); | ||
@@ -39,3 +39,3 @@ else | ||
} else { | ||
if(! options.artillery) | ||
if (!options.artillery) | ||
this.cookieMiddleware = this.cookieSync(options.cookie, this.server_id); | ||
@@ -49,3 +49,3 @@ else | ||
cookieSync(cookieName: string, data: any){ | ||
cookieSync(cookieName: string, data: any) { | ||
return (req, res, next) => { | ||
@@ -55,3 +55,3 @@ try { | ||
return this.response(cookieName, res, next)(enc) | ||
} catch(e){ | ||
} catch (e) { | ||
return this.errorAndNext(next)(e); | ||
@@ -62,3 +62,3 @@ } | ||
cookieAsync(cookieName: string, data: any){ | ||
cookieAsync(cookieName: string, data: any) { | ||
return (req, res, next) => { | ||
@@ -72,3 +72,3 @@ this.cipher | ||
stupidMiddlewareSync(){ | ||
stupidMiddlewareSync() { | ||
return (req, res, next) => { | ||
@@ -78,3 +78,3 @@ try { | ||
next(); | ||
} catch(e){ | ||
} catch (e) { | ||
return this.errorAndNext(next)(e); | ||
@@ -85,3 +85,3 @@ } | ||
stupidMiddlewareAsync(){ | ||
stupidMiddlewareAsync() { | ||
return (req, res, next) => { | ||
@@ -123,28 +123,3 @@ this.cipher | ||
serverId(){ | ||
return this.server_id; | ||
} | ||
/** | ||
* default cookie params | ||
* | ||
* @param opt | ||
*/ | ||
private cookie_params(opt?: any) { | ||
const base = { | ||
domain: 'localhost', | ||
httpOnly: true, | ||
path: '/', | ||
secure: true, | ||
signed: false, | ||
sameSite: 'Lax', | ||
//maxAge: (new Date(Date.now() + 60 * 60 * 1000)).getMilliseconds(), | ||
expires: new Date(Date.now() + 24 * 60 * 60 * 1000), | ||
}; | ||
return Object.assign({}, base, opt); | ||
}; | ||
/** | ||
* Log Error and Next | ||
@@ -160,10 +135,12 @@ * @param next | ||
private decipherCookieMiddleware(){ | ||
private decipherCookieMiddleware() { | ||
return (req, res, next) => { | ||
if(req.cookies['superdope']){ | ||
if (req.cookies['superdope']) { | ||
console.log('decipherCookieMiddleware') | ||
//console.log(req.headers) | ||
const foo = cookie.parse(req.headers.cookie, {decode: (data) => { | ||
return this.cipher.decrypt(decodeURIComponent(data), false) | ||
}}) | ||
const foo = cookie.parse(req.headers.cookie, { | ||
decode: (data) => { | ||
return this.cipher.decrypt(decodeURIComponent(data), false) | ||
} | ||
}); | ||
@@ -176,26 +153,35 @@ console.log(foo) | ||
listen(port: number, cb?: any): any{ | ||
this.express.use(cookieParser(null, {decode: (data) => { | ||
return this.cipher.decrypt(decodeURIComponent(data), false) | ||
}})); | ||
this.express.use(this.cookieMiddleware); | ||
//this.express.use(this.decipherCookieMiddleware()); | ||
listen(port: number, cb?: any): any { | ||
this.express.use(this.logErrors); | ||
this.express.use(this.clientErrorHandler); | ||
this.express.use(this.errorHandler); | ||
//crypto cookie | ||
this.express.use(cookieParser(null, { | ||
decode: (data) => { | ||
return this.cipher.decrypt(decodeURIComponent(data), false); | ||
}, | ||
this.api(); | ||
encode: (data) => { | ||
return encodeURIComponent(this.cipher.encryptSync(data)); | ||
} | ||
})); | ||
this.httpServer = http.createServer(this.express); | ||
this.server = this.httpServer.listen(port, cb); | ||
this.express.use(this.cookieMiddleware); | ||
//this.express.use(this.decipherCookieMiddleware()); | ||
if(!cb) return this.server | ||
this.express.use(this.logErrors); | ||
this.express.use(this.clientErrorHandler); | ||
this.express.use(this.errorHandler); | ||
this.api(); | ||
this.httpServer = http.createServer(this.express); | ||
this.server = this.httpServer.listen(port, cb); | ||
if (!cb) return this.server | ||
} | ||
address(){ | ||
address() { | ||
return false; | ||
} | ||
close(cb?: any){ | ||
close(cb?: any) { | ||
this.server.close(cb); | ||
@@ -205,3 +191,3 @@ } | ||
api(){ | ||
api() { | ||
this.express.get( | ||
@@ -232,2 +218,3 @@ '/', | ||
} | ||
/** | ||
@@ -243,3 +230,3 @@ * Outputs a simple message to show that the server is running. | ||
//res.json(res.getHeaders()['set-cookie']) | ||
if(! req.query.id) return res.send('error'); | ||
if (!req.query.id) return res.send('error'); | ||
res.json({id: req.query.id, encrypted: res.enc}); | ||
@@ -252,7 +239,7 @@ } | ||
getReadCookie(req: any, res: any, next: any) { | ||
getReadCookie(req: any, res: any, next: any) { | ||
res.send(req.cookies['cryptocookie']) | ||
} | ||
logErrors(err, req, res, next){ | ||
logErrors(err, req, res, next) { | ||
console.error(err.stack); | ||
@@ -262,5 +249,5 @@ next(err) | ||
clientErrorHandler(err, req, res, next){ | ||
clientErrorHandler(err, req, res, next) { | ||
if (req.xhr) { | ||
res.status(500).send({ error: 'Something failed!' }) | ||
res.status(500).send({error: 'Something failed!'}) | ||
} else { | ||
@@ -271,8 +258,33 @@ next(err) | ||
errorHandler (err, req, res, next) { | ||
if(process.env.NODE_ENV !== 'development') | ||
return res.status(500).send({ error: 'Something failed!' }); | ||
errorHandler(err, req, res, next) { | ||
if (process.env.NODE_ENV !== 'development') | ||
return res.status(500).send({error: 'Something failed!'}); | ||
res.status(500).send({error: err.stack}) | ||
} | ||
serverId() { | ||
return this.server_id; | ||
} | ||
/** | ||
* default cookie params | ||
* | ||
* @param opt | ||
*/ | ||
private cookie_params(opt?: any) { | ||
const base = { | ||
domain: 'localhost', | ||
httpOnly: true, | ||
path: '/', | ||
secure: true, | ||
signed: false, | ||
sameSite: 'Lax', | ||
//maxAge: (new Date(Date.now() + 60 * 60 * 1000)).getMilliseconds(), | ||
expires: new Date(Date.now() + 24 * 60 * 60 * 1000), | ||
}; | ||
return Object.assign({}, base, opt); | ||
}; | ||
} |
@@ -61,3 +61,2 @@ const {it} = require("mocha"); | ||
it('should cipher and decipher Sync Mode', done => { | ||
const encryptor = new Encryptor({key, serialize_mode}); | ||
@@ -64,0 +63,0 @@ let enc = encryptor.encryptSync(text); |
@@ -5,3 +5,3 @@ const {Cookie} = require("cookiejar"); | ||
const uuid = require('uuid/v1'); | ||
const {describe, it} = require("mocha"); | ||
const {it} = require("mocha"); | ||
const {expect} = require("chai"); | ||
@@ -8,0 +8,0 @@ const chai = require("chai"); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
85715
39
1897
403
2