@neoskop/ethereal-secrets-middleware
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -23,2 +23,2 @@ import { RedisStoreOptions } from 'connect-redis'; | ||
} | ||
export declare function etherealSecrets(config?: EtherealSecretsConfig): (req: Request, res: Response, next: NextFunction) => void; | ||
export declare function etherealSecrets(config: EtherealSecretsConfig): (req: Request, res: Response, next: NextFunction) => void; |
@@ -9,3 +9,3 @@ "use strict"; | ||
const deepmerge = require("deepmerge"); | ||
const IORedis = require("ioredis"); | ||
const ioredis = require("ioredis"); | ||
const Validator = require("validator"); | ||
@@ -91,4 +91,4 @@ const parseUuid = (req, res, uuidConsumer) => { | ||
} | ||
function etherealSecrets(config = {}) { | ||
config = deepmerge({ | ||
function etherealSecrets(config) { | ||
const mergedConfig = deepmerge({ | ||
local: { | ||
@@ -107,11 +107,18 @@ ttl: 4 * 60 * 60, | ||
}, | ||
}, config); | ||
const redisConfig = Object.assign({ | ||
host: 'redis', | ||
}, config.redis, { ttl: config.local.ttl }); | ||
const redisClient = new IORedis(redisConfig); | ||
}, { | ||
local: config.local || {}, | ||
remote: config.remote || {}, | ||
trustProxy: config.trustProxy, | ||
}); | ||
const redisConfig = Object.assign({}, config.redis, { | ||
ttl: mergedConfig.local.ttl, | ||
}); | ||
const redisClient = redisConfig.client || new ioredis(redisConfig); | ||
const sessionConfig = { | ||
store: new (RedisStore(session))({ client: redisClient }), | ||
name: config.local.cookie.name, | ||
secret: config.local.cookie.secret, | ||
store: new (RedisStore(session))({ | ||
client: redisClient, | ||
ttl: mergedConfig.local.ttl, | ||
}), | ||
name: mergedConfig.local.cookie.name, | ||
secret: mergedConfig.local.cookie.secret, | ||
resave: false, | ||
@@ -121,11 +128,11 @@ saveUninitialized: false, | ||
httpOnly: true, | ||
maxAge: config.local.ttl * 1000, | ||
maxAge: mergedConfig.local.ttl * 1000, | ||
path: '/', | ||
secure: false, | ||
sameSite: 'strict', | ||
}, config.local.cookie), | ||
}, mergedConfig.local.cookie), | ||
}; | ||
const sessionHandler = session(sessionConfig); | ||
return (req, res, next) => { | ||
if (config.remote.enabled) { | ||
if (mergedConfig.remote.enabled) { | ||
switch (req.method) { | ||
@@ -137,8 +144,8 @@ case 'GET': | ||
else { | ||
return parseUuid(req, res, uuid => readRemotelyEncrypted(redisClient, uuid, res)); | ||
return parseUuid(req, res, (uuid) => readRemotelyEncrypted(redisClient, uuid, res)); | ||
} | ||
case 'POST': | ||
return createRemotelyEncrypted(req, res, redisClient, config.remote); | ||
return createRemotelyEncrypted(req, res, redisClient, mergedConfig.remote); | ||
case 'DELETE': | ||
return parseUuid(req, res, uuid => removeRemotelyEncrypted(redisClient, uuid, res)); | ||
return parseUuid(req, res, (uuid) => removeRemotelyEncrypted(redisClient, uuid, res)); | ||
} | ||
@@ -145,0 +152,0 @@ } |
@@ -12,3 +12,3 @@ "use strict"; | ||
const UuidStatic = require("uuid"); | ||
before(done => { | ||
before((done) => { | ||
this.redis = new IORedis({ host: 'redis' }); | ||
@@ -19,3 +19,3 @@ chai.use(chaiHttp); | ||
}); | ||
beforeEach(done => { | ||
beforeEach((done) => { | ||
this.app = express(); | ||
@@ -32,2 +32,3 @@ this.clock = sinon.useFakeTimers(); | ||
remote: remoteOptions, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -44,2 +45,5 @@ } | ||
}, | ||
redis: { | ||
host: 'redis', | ||
}, | ||
})); | ||
@@ -62,2 +66,3 @@ this.app.put('/secrets', (req, res) => { | ||
}, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -71,6 +76,3 @@ try { | ||
chai.expect(res).to.have.status(200).and.be.json; | ||
chai | ||
.expect(res.body) | ||
.to.be.an('object') | ||
.with.key('key'); | ||
chai.expect(res.body).to.be.an('object').with.key('key'); | ||
} | ||
@@ -86,2 +88,3 @@ catch (err) { | ||
}, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -112,2 +115,3 @@ let agent = chai.request.agent(this.app); | ||
}, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -139,2 +143,3 @@ try { | ||
}, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -163,2 +168,3 @@ try { | ||
}, | ||
redis: { host: 'redis' }, | ||
})); | ||
@@ -184,6 +190,3 @@ try { | ||
try { | ||
const res = await chai | ||
.request(this.app) | ||
.post('/secrets') | ||
.send({ | ||
const res = await chai.request(this.app).post('/secrets').send({ | ||
data: 'foo', | ||
@@ -193,6 +196,3 @@ }); | ||
chai.expect(res).to.have.status(201), | ||
chai | ||
.expect(res.body) | ||
.to.be.an('object') | ||
.with.keys('key', 'expiryDate'), | ||
chai.expect(res.body).to.be.an('object').with.keys('key', 'expiryDate'), | ||
chai.expect(res.body['key']).to.be.a.uuid(), | ||
@@ -214,3 +214,3 @@ ]); | ||
}) | ||
.then(res => { | ||
.then((res) => { | ||
let timeInEliteFuture = new Date(); | ||
@@ -229,3 +229,3 @@ timeInEliteFuture.setSeconds(timeInEliteFuture.getSeconds() + 1337); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -240,6 +240,6 @@ }); | ||
.set('Accept', 'application/json') | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(400); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(400); | ||
@@ -257,3 +257,3 @@ }); | ||
.set('Accept', 'application/json') | ||
.then(res => { | ||
.then((res) => { | ||
let key = res.body['key']; | ||
@@ -263,3 +263,3 @@ return chai.request | ||
.get('/secrets/' + key) | ||
.then(res => { | ||
.then((res) => { | ||
return Promise.all([ | ||
@@ -274,7 +274,7 @@ chai.expect(res).to.have.status(200), | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
}); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -289,6 +289,6 @@ }); | ||
.set('Accept', 'application/json') | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(400); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(400); | ||
@@ -302,6 +302,6 @@ }); | ||
.del('/secrets/foobar') | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(400); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(400); | ||
@@ -316,6 +316,6 @@ }); | ||
.set('Accept', 'application/json') | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(404); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(404); | ||
@@ -339,10 +339,10 @@ }); | ||
.get('/secrets/' + key) | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(404); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(404); | ||
}); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -368,3 +368,3 @@ }); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -391,3 +391,3 @@ }); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -411,3 +411,3 @@ }); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -437,3 +437,3 @@ }); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
throw err; | ||
@@ -451,10 +451,36 @@ }); | ||
.set('Accept', 'application/json') | ||
.then(res => { | ||
.then((res) => { | ||
return chai.expect(res).to.have.status(400); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
return chai.expect(err.response).to.have.status(400); | ||
}); | ||
}); | ||
it('should use a custom IORedis client if supplied', async () => { | ||
this.app.use('/secrets', index_1.etherealSecrets({ | ||
local: { | ||
ttl: 9, | ||
cookie: { | ||
secret: 'supersecret', | ||
}, | ||
}, | ||
redis: { client: this.redis }, | ||
})); | ||
try { | ||
const res = await chai.request | ||
.agent(this.app) | ||
.get('/secrets') | ||
.set('Accept', 'application/json'); | ||
let cookieValue = res | ||
.get('Set-Cookie')[0] | ||
.replace(/sessionid=(.+?);.+/, '$1'); | ||
let unsignedCookie = cookieParser.signedCookie(decodeURIComponent(cookieValue), 'supersecret'); | ||
let ttl = await this.redis.ttl('sess:' + unsignedCookie); | ||
return chai.expect(ttl).to.be.lessThan(10); | ||
} | ||
catch (err) { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@neoskop/ethereal-secrets-middleware", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "typings": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150895
636