Compare commits

..

No commits in common. "master" and "v1" have entirely different histories.
master ... v1

4 changed files with 65 additions and 122 deletions

View File

@ -1,6 +1,4 @@
![downloads](https://img.shields.io/npm/dt/@gm5/jwt.svg)
![version](https://img.shields.io/npm/v/@gm5/jwt.svg)
![module info](https://nodei.co/npm/@gm5/jwt.png?downloads=true&downloadRank=true&stars=true)
# @gm5/jwt
@ -16,13 +14,10 @@ npm install @gm5/jwt
```js
import { createApp } from '@gm5/code'
import { createJwt, JwtModule } from '@gm5/jwt'
import Five from '@gm5/code'
import jwt from '@gm5/jwt'
var app = createApp()
app.install(JwtModule)
.use(createJwt())
.run()
var app = new Five()
app.install(jwt)
```

21
index.d.ts vendored
View File

@ -1,21 +0,0 @@
declare module '@gm5/jwt' {
//
interface JwtConfig {
secret: string
ttl?: number
level?: number
}
interface JwtInstance {
ttl: number
sign(data: object, secret: string): string
verify(token: string, secret: string): object | false
}
interface JwtModule {
name: 'jwt'
install(conf?: JwtConfig): JwtInstance
}
export function createJwt(): JwtModule
}

130
index.js
View File

@ -4,24 +4,65 @@
* @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 DEFAULT_CONFIG = {
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')
function hmac(str, secret) {
var buf = crypto.hmac('sha256', str, secret, 'buffer')
return base64encode(buf, true)
}
function JwtMiddleware(req, res, next) {
let { secret, level } = this.get('jwt')
let deviceID = ''
let ssid
export const jwtPackage = {
name: 'jwt',
install() {
return {
// 签名, 返回token
sign(data, secret, ttl) {
// header: base64("{"typ":"JWT","alg":"HS256"}")
// 这里固定使用sha256,
var header = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
// 加入过期时间,
var payload = { data, expires: Date.now() + ttl * 1000 }
var auth_str = ''
payload = JSON.stringify(payload)
payload = base64encode(payload, true)
auth_str = hmac(`${header}.${payload}`, secret)
return [header, payload, auth_str].join('.')
},
// 校验token
verify(token = '', secret) {
var jwt = token.split('.')
var auth_str, payload
if (jwt.length !== 3) {
return false
}
auth_str = jwt.pop()
payload = JSON.parse(base64decode(jwt[1], true))
// 如果已经过期, 则不再校验hash
if (payload.expires < Date.now()) {
return false
}
if (hmac(jwt.join('.'), secret) === auth_str) {
return payload.data
}
return false
}
}
}
}
export function jwtConnect(req, res, next) {
var { secret, level } = this.get('jwt')
var deviceID = ''
var ssid
// options请求不处理jwt
if (req.method === 'OPTIONS') {
@ -42,66 +83,7 @@ function JwtMiddleware(req, res, next) {
deviceID = sha1(deviceID)
}
req.__mix_key__ = secret + deviceID
req.mixKey = secret + deviceID
next()
}
export function createJwt() {
return {
name: 'jwt',
install(conf = {}) {
if (!conf.secret) {
console.warn(
new Error(
'You must set a `secret` key for jwt, or it will use the default key.'
)
)
}
let jwt = Object.assign({}, DEFAULT_CONFIG, conf)
this.set({ jwt })
this.use(JwtMiddleware)
return {
ttl: jwt.ttl,
// 签名, 返回token
// header: base64("{"typ":"JWT","alg":"HS256"}")
// 这里固定使用sha256
sign(data, secret) {
// 加入过期时间,
let payload = { data, expires: Date.now() + this.ttl * 1000 }
let token = ''
payload = base64encode(JSON.stringify(payload), true)
token = hmac_base64(`${HS256_HEADER}.${payload}`, secret)
return `${HS256_HEADER}.${payload}.${token}`
},
// 校验token
verify(token = '', secret) {
let jwt = token.split('.')
let [_, payload, auth] = jwt
if (jwt.length !== 3) {
return false
}
payload = JSON.parse(base64decode(payload, true))
// 如果已经过期, 则不再校验hash
if (payload.expires < Date.now()) {
return false
}
if (hmac_base64(jwt.join('.'), secret) === auth) {
return payload.data
}
return false
}
}
}
}
}

View File

@ -1,27 +1,14 @@
{
"name": "@gm5/jwt",
"version": "3.0.1",
"version": "1.1.0",
"type": "module",
"description": "json web token",
"main": "index.js",
"types": "index.d.ts",
"author": "yutent",
"keywords": [
"fivejs",
"gm5",
"jwt",
"http",
"json_web_token"
],
"repository": {
"type": "git",
"url": "https://git.wkit.fun/gm5/jwt.git"
},
"keywords": ["fivejs", "jwt", "http"],
"repository": "https://github.com/bytedo/gmf.jwt.git",
"license": "MIT",
"dependencies": {
"crypto.js": "^3.3.4"
},
"engines": {
"node": ">=16.0.0"
"crypto.js": "^2.0.1"
}
}