From 037918cd58f9ac9c24840ae4f9bd1fa3cfad3970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 18 Sep 2020 14:45:42 +0800 Subject: [PATCH] 1.0.0 --- Readme.md | 8 ++++---- index.js | 15 ++++++--------- package.json | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Readme.md b/Readme.md index 77191b0..70bba39 100644 --- a/Readme.md +++ b/Readme.md @@ -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 -npm install @gm5/controller +npm install @gm5/jwt ``` diff --git a/index.js b/index.js index 13b3216..18a1b56 100644 --- a/index.js +++ b/index.js @@ -12,11 +12,9 @@ function hmac(str, secret) { return base64encode(buf, true) } -export default class Jwt { - constructor(expires, secret) { - this.expires = expires - this.secret = secret - } +export default { + expires: 7 * 24 * 3600, + secret: 'this is secret key', // 签名, 返回token sign(data) { @@ -34,11 +32,10 @@ export default class Jwt { auth_str = hmac(`${header}.${payload}`, secret) return [header, payload, auth_str].join('.') - } + }, // 校验token - verify(token) { - var { secret } = this + verify(token = '') { var jwt = token.split('.') var auth_str, payload @@ -53,7 +50,7 @@ export default class Jwt { return 'expired' } - if (hmac(jwt.join('.'), secret) === auth_str) { + if (hmac(jwt.join('.'), this.secret) === auth_str) { return payload.data } diff --git a/package.json b/package.json index 59008ad..c1376c7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@gm5/jwt", "version": "1.0.0", "type": "module", - "description": "JSON WEB TOKEN", + "description": "json web token", "main": "index.js", "author": "yutent", "keywords": ["fivejs", "jwt", "http"],