Skip to content

Commit

Permalink
默认开启cookie签名机制
Browse files Browse the repository at this point in the history
  • Loading branch information
yafoo committed Mar 7, 2024
1 parent 992892e commit 9a686e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const Koa = require('koa');
const {app: cfg_app} = require('./config');
const {app: cfg_app, cookie: cfg_cookie} = require('./config');
const Logger = require('./logger');
const Response = require('./response');
const router = require('./router');
Expand Down Expand Up @@ -54,6 +54,9 @@ class App extends Koa
// router
this.use(router.routes()).use(router.allowedMethods());

// cookie
cfg_cookie.keys && (this.keys = cfg_app.app_debug ? ['jj.js'] : cfg_cookie.keys);

// types
if(cfg_app.app_debug) {
const {isFileSync} = require('./utils/fs');
Expand Down
7 changes: 6 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const cache = {
clear_time: undefined // (undefined: 清理一次, 0: 关闭自动清理, >0: 为自动清理周期)
}

const cookie = {
keys: [Math.random().toString(36).slice(-8)], // cookie密钥
signed: true // 默认开启签名
}

const page = {
page_key : 'page', // 默认分页标识
key_origin : 'query', // query 或 params
Expand Down Expand Up @@ -94,6 +99,6 @@ module.exports = {
cache: {...cache, ...base_config.cache},
page: {...page, ...base_config.page},
routes: base_config.routes,
cookie: base_config.cookie,
cookie: {...cookie, ...base_config.cookie},
tpl: {...tpl, ...base_config.tpl}
};
7 changes: 4 additions & 3 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ class Cookie extends Context
if(key === undefined) {
return this.all();
}
return this.ctx.cookies.get(key, options);
return this.ctx.cookies.get(key, {signed: cfg_cookie.signed, ...options});
}

/**
* 删除或清理cookie
* @public
* @param {*} [key] - 为空则清理所有cookie
* @param {import('cookies').GetOption} [options]
* @returns {this}
*/
delete(key) {
delete(key, options) {
if(key === undefined) {
this.clear();
} else {
this.ctx.cookies.set(key, '', {maxAge: 0});
this.ctx.cookies.set(key, '', {...cfg_cookie, ...options, maxAge: 0});
}
return this;
}
Expand Down

0 comments on commit 9a686e5

Please sign in to comment.