宇天 2020-09-18 14:45:42 +08:00
parent 1628dee066
commit 037918cd58
3 changed files with 11 additions and 14 deletions

View File

@ -1,12 +1,12 @@
![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true) ![module info](https://nodei.co/npm/@gm5/jwt.png?downloads=true&downloadRank=true&stars=true)
# @gm5/controller # @gm5/jwt
> 控制器基类 > json web token
## 安装 ## 安装
```bash ```bash
npm install @gm5/controller npm install @gm5/jwt
``` ```

View File

@ -12,11 +12,9 @@ function hmac(str, secret) {
return base64encode(buf, true) return base64encode(buf, true)
} }
export default class Jwt { export default {
constructor(expires, secret) { expires: 7 * 24 * 3600,
this.expires = expires secret: 'this is secret key',
this.secret = secret
}
// 签名, 返回token // 签名, 返回token
sign(data) { sign(data) {
@ -34,11 +32,10 @@ export default class Jwt {
auth_str = hmac(`${header}.${payload}`, secret) auth_str = hmac(`${header}.${payload}`, secret)
return [header, payload, auth_str].join('.') return [header, payload, auth_str].join('.')
} },
// 校验token // 校验token
verify(token) { verify(token = '') {
var { secret } = this
var jwt = token.split('.') var jwt = token.split('.')
var auth_str, payload var auth_str, payload
@ -53,7 +50,7 @@ export default class Jwt {
return 'expired' return 'expired'
} }
if (hmac(jwt.join('.'), secret) === auth_str) { if (hmac(jwt.join('.'), this.secret) === auth_str) {
return payload.data return payload.data
} }

View File

@ -2,7 +2,7 @@
"name": "@gm5/jwt", "name": "@gm5/jwt",
"version": "1.0.0", "version": "1.0.0",
"type": "module", "type": "module",
"description": "JSON WEB TOKEN", "description": "json web token",
"main": "index.js", "main": "index.js",
"author": "yutent", "author": "yutent",
"keywords": ["fivejs", "jwt", "http"], "keywords": ["fivejs", "jwt", "http"],