[BREAKING CHANGE] Improve security
This commit is contained in:
		
							parent
							
								
									86548f6468
								
							
						
					
					
						commit
						9458245382
					
				
					 5 changed files with 19 additions and 3 deletions
				
			
		|  | @ -70,5 +70,7 @@ block content | |||
| 				| 次に、<code>#{api_url}/auth/session/userkey</code>へ<code>app_secret</code>としてApp Secretを、<code>token</code>としてセッションのトークンをパラメータとして付与したリクエストを送信してください。 | ||||
| 				br | ||||
| 				| 上手くいけば、認証したユーザーのアクセストークンがレスポンスとして取得できます。おめでとうございます! | ||||
| 			p | ||||
| 				| 以降アクセストークンは、<strong>ユーザーのアクセストークン+アプリのシークレットキーをsha512したもの</strong>として扱います。 | ||||
| 
 | ||||
| 	p アクセストークンを取得できたら、あとは簡単です。REST APIなら、リクエストにアクセストークンを<code>i</code>としてパラメータに含めるだけです。 | ||||
|  |  | |||
|  | @ -69,6 +69,7 @@ | |||
|     "compression": "1.6.2", | ||||
|     "cors": "2.8.1", | ||||
|     "cropperjs": "1.0.0-beta", | ||||
|     "crypto": "0.0.3", | ||||
|     "deepcopy": "0.6.3", | ||||
|     "del": "2.2.2", | ||||
|     "elasticsearch": "12.1.3", | ||||
|  |  | |||
|  | @ -43,7 +43,7 @@ export default (req: express.Request) => new Promise<IAuthContext>(async (resolv | |||
| 		}); | ||||
| 	} else { | ||||
| 		const userkeyDoc = await Userkey.findOne({ | ||||
| 			key: token | ||||
| 			hash: token | ||||
| 		}); | ||||
| 
 | ||||
| 		if (userkeyDoc === null) { | ||||
|  |  | |||
|  | @ -4,6 +4,8 @@ | |||
|  * Module dependencies | ||||
|  */ | ||||
| import rndstr from 'rndstr'; | ||||
| const crypto = require('crypto'); | ||||
| import App from '../../models/app'; | ||||
| import AuthSess from '../../models/auth-session'; | ||||
| import Userkey from '../../models/userkey'; | ||||
| 
 | ||||
|  | @ -41,12 +43,23 @@ module.exports = (params, user) => | |||
| 	}); | ||||
| 
 | ||||
| 	if (exist === null) { | ||||
| 		// Lookup app
 | ||||
| 		const app = await App.findOne({ | ||||
| 			app_id: session.app_id | ||||
| 		}); | ||||
| 
 | ||||
| 		// Generate Hash
 | ||||
| 		const sha512 = crypto.createHash('sha512'); | ||||
| 		sha512.update(key + app.secret); | ||||
| 		const hash = sha512.digest('hex'); | ||||
| 
 | ||||
| 		// Insert userkey doc
 | ||||
| 		await Userkey.insert({ | ||||
| 			created_at: new Date(), | ||||
| 			app_id: session.app_id, | ||||
| 			user_id: user._id, | ||||
| 			key: key | ||||
| 			key: key, | ||||
| 			hash: hash | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -64,7 +64,7 @@ function authenticate(connection: websocket.connection, token: string): Promise< | |||
| 			resolve(user); | ||||
| 		} else { | ||||
| 			const userkey = await Userkey.findOne({ | ||||
| 				key: token | ||||
| 				hash: token | ||||
| 			}); | ||||
| 
 | ||||
| 			if (userkey == null) { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue