Compare commits
No commits in common. "master" and "v1" 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()
|
|
||||||
|
|
||||||
```
|
```
|
67
index.js
67
index.js
|
@ -4,59 +4,44 @@
|
||||||
* @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'
|
function hmac(str, secret) {
|
||||||
const DEFAULT_CONFIG = {
|
var buf = crypto.hmac('sha256', str, secret, 'buffer')
|
||||||
ttl: 3600 * 24 * 7,
|
|
||||||
level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip
|
|
||||||
secret: 'it_is_secret_key' // jwt密钥, 使用时请修改
|
|
||||||
}
|
|
||||||
|
|
||||||
function hmac_base64(str, secret) {
|
|
||||||
let buf = hmac('sha256', str, secret, 'buffer')
|
|
||||||
return base64encode(buf, true)
|
return base64encode(buf, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const JwtModule = {
|
export const jwtPackage = {
|
||||||
name: 'jwt',
|
name: 'jwt',
|
||||||
install(conf = {}) {
|
install() {
|
||||||
if (!conf.secret) {
|
|
||||||
console.warn(
|
|
||||||
new Error(
|
|
||||||
'Please make sure to set the `secret` field, as the default value is not secure'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let jwt = Object.assign({}, DEFAULT_CONFIG, conf)
|
|
||||||
this.set({ jwt })
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ttl: jwt.ttl,
|
|
||||||
// 签名, 返回token
|
// 签名, 返回token
|
||||||
|
sign(data, secret, ttl) {
|
||||||
// header: base64("{"typ":"JWT","alg":"HS256"}")
|
// header: base64("{"typ":"JWT","alg":"HS256"}")
|
||||||
// 这里固定使用sha256
|
// 这里固定使用sha256,
|
||||||
sign(data, secret) {
|
var header = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
|
||||||
|
|
||||||
// 加入过期时间,
|
// 加入过期时间,
|
||||||
let payload = { data, expires: Date.now() + this.ttl * 1000 }
|
var payload = { data, expires: Date.now() + ttl * 1000 }
|
||||||
let token = ''
|
var auth_str = ''
|
||||||
|
|
||||||
payload = base64encode(JSON.stringify(payload), true)
|
payload = JSON.stringify(payload)
|
||||||
token = hmac_base64(`${HS256_HEADER}.${payload}`, secret)
|
payload = base64encode(payload, true)
|
||||||
|
auth_str = hmac(`${header}.${payload}`, secret)
|
||||||
|
|
||||||
return `${HS256_HEADER}.${payload}.${token}`
|
return [header, payload, auth_str].join('.')
|
||||||
},
|
},
|
||||||
|
|
||||||
// 校验token
|
// 校验token
|
||||||
verify(token = '', secret) {
|
verify(token = '', secret) {
|
||||||
let jwt = token.split('.')
|
var jwt = token.split('.')
|
||||||
let auth, payload
|
var auth_str, payload
|
||||||
|
|
||||||
if (jwt.length !== 3) {
|
if (jwt.length !== 3) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
auth = jwt.pop()
|
auth_str = jwt.pop()
|
||||||
payload = JSON.parse(base64decode(jwt[1], true))
|
payload = JSON.parse(base64decode(jwt[1], true))
|
||||||
|
|
||||||
// 如果已经过期, 则不再校验hash
|
// 如果已经过期, 则不再校验hash
|
||||||
|
@ -64,7 +49,7 @@ export const JwtModule = {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hmac_base64(jwt.join('.'), secret) === auth) {
|
if (hmac(jwt.join('.'), secret) === auth_str) {
|
||||||
return payload.data
|
return payload.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,11 +59,10 @@ export const JwtModule = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createJwt() {
|
export function jwtConnect(req, res, next) {
|
||||||
return function (req, res, next) {
|
var { secret, level } = this.get('jwt')
|
||||||
let { secret, level } = this.get('jwt')
|
var deviceID = ''
|
||||||
let deviceID = ''
|
var ssid
|
||||||
let ssid
|
|
||||||
|
|
||||||
// options请求不处理jwt
|
// options请求不处理jwt
|
||||||
if (req.method === 'OPTIONS') {
|
if (req.method === 'OPTIONS') {
|
||||||
|
@ -99,8 +83,7 @@ export function createJwt() {
|
||||||
deviceID = sha1(deviceID)
|
deviceID = sha1(deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.__mix_key__ = secret + deviceID
|
req.mixKey = secret + deviceID
|
||||||
|
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
14
package.json
14
package.json
|
@ -1,20 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "@gm5/jwt",
|
"name": "@gm5/jwt",
|
||||||
"version": "2.0.2",
|
"version": "1.1.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "json web token",
|
"description": "json web token",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "yutent",
|
"author": "yutent",
|
||||||
"keywords": [
|
"keywords": ["fivejs", "jwt", "http"],
|
||||||
"fivejs",
|
"repository": "https://github.com/bytedo/gmf.jwt.git",
|
||||||
"gm5",
|
|
||||||
"jwt",
|
|
||||||
"http",
|
|
||||||
"json_web_token"
|
|
||||||
],
|
|
||||||
"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