Compare commits
No commits in common. "master" and "v2" have entirely different histories.
15
Readme.md
15
Readme.md
|
@ -1,6 +1,4 @@
|
||||||
|
![module info](https://nodei.co/npm/@gm5/jwt.png?downloads=true&downloadRank=true&stars=true)
|
||||||
![downloads](https://img.shields.io/npm/dt/@gm5/jwt.svg)
|
|
||||||
![version](https://img.shields.io/npm/v/@gm5/jwt.svg)
|
|
||||||
|
|
||||||
# @gm5/jwt
|
# @gm5/jwt
|
||||||
|
|
||||||
|
@ -16,13 +14,10 @@ npm install @gm5/jwt
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
import { createApp } from '@gm5/code'
|
import Five from '@gm5/code'
|
||||||
import { createJwt, JwtModule } from '@gm5/jwt'
|
import jwt from '@gm5/jwt'
|
||||||
|
|
||||||
var app = createApp()
|
var app = new Five()
|
||||||
|
app.install(jwt)
|
||||||
app.install(JwtModule)
|
|
||||||
.use(createJwt())
|
|
||||||
.run()
|
|
||||||
|
|
||||||
```
|
```
|
11
index.js
11
index.js
|
@ -4,7 +4,8 @@
|
||||||
* @date 2020/09/16 17:23:52
|
* @date 2020/09/16 17:23:52
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { base64encode, base64decode, hmac, sha1 } from 'crypto.js'
|
import crypto from 'crypto.js'
|
||||||
|
import { base64encode, base64decode, sha1 } from 'crypto.js'
|
||||||
|
|
||||||
const HS256_HEADER = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
|
const HS256_HEADER = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
|
||||||
const DEFAULT_CONFIG = {
|
const DEFAULT_CONFIG = {
|
||||||
|
@ -13,8 +14,8 @@ const DEFAULT_CONFIG = {
|
||||||
secret: 'it_is_secret_key' // jwt密钥, 使用时请修改
|
secret: 'it_is_secret_key' // jwt密钥, 使用时请修改
|
||||||
}
|
}
|
||||||
|
|
||||||
function hmac_base64(str, secret) {
|
function hmac(str, secret) {
|
||||||
let buf = hmac('sha256', str, secret, 'buffer')
|
let buf = crypto.hmac('sha256', str, secret, 'buffer')
|
||||||
return base64encode(buf, true)
|
return base64encode(buf, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ export const JwtModule = {
|
||||||
let token = ''
|
let token = ''
|
||||||
|
|
||||||
payload = base64encode(JSON.stringify(payload), true)
|
payload = base64encode(JSON.stringify(payload), true)
|
||||||
token = hmac_base64(`${HS256_HEADER}.${payload}`, secret)
|
token = hmac(`${HS256_HEADER}.${payload}`, secret)
|
||||||
|
|
||||||
return `${HS256_HEADER}.${payload}.${token}`
|
return `${HS256_HEADER}.${payload}.${token}`
|
||||||
},
|
},
|
||||||
|
@ -64,7 +65,7 @@ export const JwtModule = {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hmac_base64(jwt.join('.'), secret) === auth) {
|
if (hmac(jwt.join('.'), secret) === auth) {
|
||||||
return payload.data
|
return payload.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@gm5/jwt",
|
"name": "@gm5/jwt",
|
||||||
"version": "2.0.2",
|
"version": "2.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "json web token",
|
"description": "json web token",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
@ -15,6 +15,6 @@
|
||||||
"repository": "https://git.wkit.fun/gm5/jwt.git",
|
"repository": "https://git.wkit.fun/gm5/jwt.git",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"crypto.js": "^3.1.2"
|
"crypto.js": "^2.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue