From 8cc03c337ee11fbc6de4d031a0179172eb34cb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 25 Sep 2020 18:33:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96;=E9=87=8D?= =?UTF-8?q?=E6=9E=84jwt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 47 ++++++++++++++++++++++++++++++++--------------- package.json | 3 +++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 62896b7..77d13d3 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,8 @@ * @date 2020/09/24 15:45:17 */ +import { sha1 } from 'crypto.js' + export default class Controller { // 初始化方法, 取代原先的构造方法 __f_i_v_e__(ctx, req, res) { @@ -12,9 +14,6 @@ export default class Controller { this.request = req this.response = res - this.jwt = Object.create(null) - this.jwt.sign = ctx.$$jwt.sign - this.smarty = ctx.$$smarty } @@ -44,12 +43,24 @@ export default class Controller { } } - checkAuth() { - var authorization = this.request.header('authorization') || '' - this.jwt.result = this.context.$$jwt.verify(authorization) - // token校验失败, 自动清除会话 - if (this.jwt.result === false) { - this.context.$$session.clear() + get jwt() { + var { enabled, ttl } = this.context.get('jwt') + var { mixKey } = this.request + + if (enabled) { + var tmp = Object.create(null) + + tmp.sign = data => this.context.$$jwt.sign(data, mixKey, ttl) + + tmp.check = _ => { + var str = this.request.header('authorization') + if (str) { + return this.context.$$jwt.verify(str, mixKey) + } + } + return tmp + } else { + throw Error('Jwt was disabled.') } } @@ -74,13 +85,19 @@ export default class Controller { // 会话读写 session(key, val) { - if (arguments.length < 2) { - // 这里返回的是Promise对象 - return this.context.$$session.get(key) - } + var { enabled } = this.context.get('session') + var { ssid } = this.request + if (enabled) { + if (arguments.length < 2) { + // 这里返回的是Promise对象 + return this.context.$$session.get(ssid, key) + } - key += '' - this.context.$$session.set(key, val) + key += '' + this.context.$$session.set(ssid, key, val) + } else { + throw Error('Session was disabled.') + } } // resfull-api规范的纯API返回 diff --git a/package.json b/package.json index 47961cc..b2f6d6d 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "description": "控制器基类。", "main": "index.js", "author": "yutent", + "dependencies": { + "crypto.js": "^2.0.2" + }, "keywords": ["fivejs", "controller", "http"], "repository": "https://github.com/bytedo/gmf.controller.git", "license": "MIT"