1.0.0
parent
1628dee066
commit
037918cd58
|
@ -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
|
||||
```
|
||||
|
||||
|
|
15
index.js
15
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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"],
|
||||
|
|
Loading…
Reference in New Issue