init
This commit is contained in:
		
						commit
						a9523058c4
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | node_modules | ||||||
|  | .DS_Store | ||||||
							
								
								
									
										33
									
								
								index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								index.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										13
									
								
								package.json
									
									
									
									
									
										Normal 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": "" | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user