更新依赖;重构jwt

v1
宇天 2020-09-25 18:33:09 +08:00
parent 3902c59b01
commit 8cc03c337e
2 changed files with 35 additions and 15 deletions

View File

@ -4,6 +4,8 @@
* @date 2020/09/24 15:45:17 * @date 2020/09/24 15:45:17
*/ */
import { sha1 } from 'crypto.js'
export default class Controller { export default class Controller {
// 初始化方法, 取代原先的构造方法 // 初始化方法, 取代原先的构造方法
__f_i_v_e__(ctx, req, res) { __f_i_v_e__(ctx, req, res) {
@ -12,9 +14,6 @@ export default class Controller {
this.request = req this.request = req
this.response = res this.response = res
this.jwt = Object.create(null)
this.jwt.sign = ctx.$$jwt.sign
this.smarty = ctx.$$smarty this.smarty = ctx.$$smarty
} }
@ -44,12 +43,24 @@ export default class Controller {
} }
} }
checkAuth() { get jwt() {
var authorization = this.request.header('authorization') || '' var { enabled, ttl } = this.context.get('jwt')
this.jwt.result = this.context.$$jwt.verify(authorization) var { mixKey } = this.request
// token校验失败, 自动清除会话
if (this.jwt.result === false) { if (enabled) {
this.context.$$session.clear() 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) { session(key, val) {
if (arguments.length < 2) { var { enabled } = this.context.get('session')
// 这里返回的是Promise对象 var { ssid } = this.request
return this.context.$$session.get(key) if (enabled) {
} if (arguments.length < 2) {
// 这里返回的是Promise对象
return this.context.$$session.get(ssid, key)
}
key += '' key += ''
this.context.$$session.set(key, val) this.context.$$session.set(ssid, key, val)
} else {
throw Error('Session was disabled.')
}
} }
// resfull-api规范的纯API返回 // resfull-api规范的纯API返回

View File

@ -5,6 +5,9 @@
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",
"author": "yutent", "author": "yutent",
"dependencies": {
"crypto.js": "^2.0.2"
},
"keywords": ["fivejs", "controller", "http"], "keywords": ["fivejs", "controller", "http"],
"repository": "https://github.com/bytedo/gmf.controller.git", "repository": "https://github.com/bytedo/gmf.controller.git",
"license": "MIT" "license": "MIT"