我正在使用cookie-encrypter
模块,cookie-parser
和dotenv-safe来存储秘密密钥..
对于cookie-encrypter
模块,我用cryptor模块创建了一个 32位sha256密钥,我将此密钥插入cookie-parser
和{{1 }} ...
但是当我尝试创建cookie时,它给我一个错误,提示未提供签名,但是我不知道自己在做错什么,因为签名位于中间件中...
服务器
cookie-encrypter
COOKIE CONTROLLER
const consign = require('consign');
const cookieParser = require('cookie-parser');
const cookieEncrypter = require('cookie-encrypter');
const crypto = require('crypto');
const dotenv = require('dotenv-safe');
const express = require('express');
const morgan = require('morgan');
const nunjucks = require('nunjucks');
class Application {
constructor(){
this.consign = consign();
this.dotenv = dotenv.load();
this.express = express();
this.morgan = morgan('dev');
this.nunjucks = nunjucks;
// CRIA UM HASH DE 32 BITS PARA O COOKIE-ENCRYPTER
this.createCookieCryptoHash();
// VERIFICA SE ESTA EM AMBIENTE DE PRODUÇÃO OU DESENVOLVIMENTO
this.checkIsDevelopment();
// INICIALIZA AS FUNÇÕES AUTOMATICAMENTE
this.initMiddlewares();
this.initConsign();
this.initViews();
}
set cookieSecretKeyHash(cookieHash){ this.cookieHash = cookieHash; }
get cookieSecretKeyHash( ){ return this.cookieHash; }
set isDevelopment(boolean){ this.isDev = boolean; }
get isDevelopment( ){ return this.isDev; }
initMiddlewares(){
this.express.set('view engine', 'njk');
this.express.use(express.static('../../app/frontend'));
this.express.use(express.json());
this.express.use(express.urlencoded({ extended: true }));
this.express.use(cookieParser(this.cookieSecretKeyHash));
this.express.use(cookieEncrypter(this.cookieSecretKeyHash));
this.express.use(this.morgan);
}
initConsign(){
this.consign.include('./routes').then('./controllers').into(this.express);
}
initViews(){
this.nunjucks.configure('./views/pages', {
watch: this.isDevelopment,
express: this.express,
autoescape: true
});
}
checkIsDevelopment(){
const isDevelopment = process.env.NODE_ENV !== 'production';
this.isDevelopment = isDevelopment;
}
createCookieCryptoHash(){
const cryptoHash = crypto.createHash('sha256').update(process.env.APP_COOKIE_SECRET_KEY).digest('base32');
this.cookieSecretKeyHash = cryptoHash;
}
}
module.exports = new Application().express;
COOKIE ROUTE
const cookieMiddleware = require('../../middlewares/cookie/middleware');
module.exports.createUserAuthCookie = (req, res) => {
try {
const { cookieName, cookieData } = req.body;
// INSTANCIAMENTO DO CONTROLLER VIA MIDDLEWARE DO COOKIE
const cookieInstance = new cookieMiddleware.Cookie(cookieName);
cookieInstance.data = cookieData;
cookieInstance.createCookieParams();
res.cookie(cookieInstance.name, cookieInstance.data, cookieInstance.params).end('TESTE');
// CRIA O COOKIE ENCRIPTADO
//res.cookie(cookieInstance.name, cookieInstance.data, cookieInstance.params);
//res.status(200).send({ data: 'COOKIE CRIADO COM SUCESSO!' }).end();
} catch (error) {
console.log(error);
res.status(500).send({ data: 'OCORREU UM ERRO INTERNO!' }).end();
}
};
module.exports.getCookies = (req, res) => {
const { cookieName, cookieData } = req.body;
console.log(req.signedCookies);
};