This commit is contained in:
陆柯 2020-06-01 16:26:32 +08:00
commit a9523058c4
3 changed files with 48 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
.DS_Store

33
index.js Normal file
View File

@ -0,0 +1,33 @@
const JsonWebToken = require('jsonwebtoken')
const crypto = require('crypto')
module.exports={
createToken: function(content, key) { // content 可以是数字,字符串或对象,不可以是数组。
key=key||(wo&&wo.Config&&wo.Config.tokenKey?wo.Config.tokenKey:'') // key或wo.Config.tokenKey其中之一必须存在
if (content && !Array.isArray(content) && typeof(key)==='string' && key.length>0){ // 注意jwt.sign(null|'') 会出错。但 sign(0)可以的。
try{
return JsonWebToken.sign(content, crypto.createHash('sha256').update(key, 'utf8').digest('hex'))
}catch(exp){
return null
}
}
return null
}
,
verifyToken: function(token, key) {
key=key||(wo&&wo.Config&&wo.Config.tokenKey?wo.Config.tokenKey:'') // key或wo.Config.tokenKey其中之一必须存在
if (token && typeof token==='string' && typeof(key)==='string' && key.length>0) {
try{
token=JsonWebToken.verify(token, crypto.createHash('sha256').update(key, 'utf8').digest('hex'))
}catch(exp){
return null
}
if (Date.now() - Date.parse(token.whenStamp) > 2*60*60*1000) { // 每过2小时核对一遍密码
}
return token
}
return null
}
}

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "so.base",
"version": "0.1.0",
"private": true,
"dependencies": {
"jsonwebtoken": "^7.2.1"
},
"devDependencies": {},
"scripts": {
"setup": "npm install"
},
"author": ""
}