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