u
This commit is contained in:
		
							parent
							
								
									df2db84fa8
								
							
						
					
					
						commit
						a5e6d8112c
					
				
							
								
								
									
										20
									
								
								webtoken.js
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								webtoken.js
									
									
									
									
									
								
							| @ -2,13 +2,16 @@ const JsonWebToken = require('jsonwebtoken') | |||||||
| 
 | 
 | ||||||
| const crypto = require('crypto') | const crypto = require('crypto') | ||||||
| 
 | 
 | ||||||
| const my = {} | const my = { envar: typeof wo !== 'undefined' ? wo.envar : globalThis.envar || {} } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|   init (envar) { |   init (envar) { | ||||||
|     my.tokenKey = envar.tokenKey |     my.envar = envar | ||||||
|   },     |   }, | ||||||
|   createToken (content, key) { |   createToken (content, key = my.envar.tokenKey) { | ||||||
|  |     if (!key) { | ||||||
|  |       console.warn('*** tokenkey is empty! ***') | ||||||
|  |     } | ||||||
|     // content 可以是数字,非空字符串或非空对象,不可以是数组。
 |     // content 可以是数字,非空字符串或非空对象,不可以是数组。
 | ||||||
|     // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
 |     // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
 | ||||||
|     try { |     try { | ||||||
| @ -16,20 +19,23 @@ module.exports = { | |||||||
|         content, |         content, | ||||||
|         crypto |         crypto | ||||||
|           .createHash('sha256') |           .createHash('sha256') | ||||||
|           .update(key || my.tokenKey || wo.envar.tokenKey || '', 'utf8') |           .update(key || '', 'utf8') | ||||||
|           .digest('hex') |           .digest('hex') | ||||||
|       ) |       ) | ||||||
|     } catch (exp) { |     } catch (exp) { | ||||||
|       return null |       return null | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   verifyToken (token, key) { |   verifyToken (token, key = my.envar.tokenKey) { | ||||||
|  |     if (!key) { | ||||||
|  |       console.warn('*** tokenkey is empty! ***') | ||||||
|  |     } | ||||||
|     try { |     try { | ||||||
|       return JsonWebToken.verify( |       return JsonWebToken.verify( | ||||||
|         token, |         token, | ||||||
|         crypto |         crypto | ||||||
|           .createHash('sha256') |           .createHash('sha256') | ||||||
|           .update(key || my.tokenKey || wo.envar.tokenKey || '', 'utf8') |           .update(key || '', 'utf8') | ||||||
|           .digest('hex') |           .digest('hex') | ||||||
|       ) |       ) | ||||||
|     } catch (exp) { |     } catch (exp) { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user