diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-01-06 01:45:02 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-01-06 01:45:02 +0900 |
| commit | 2ded8ba8580f49741e0d5e436f65561c8dd9ef18 (patch) | |
| tree | e2b13194a3d33b1481ee1004865f51f699b5f8ac /src/api | |
| parent | アクセストークンは i に統一 (diff) | |
| download | sharkey-2ded8ba8580f49741e0d5e436f65561c8dd9ef18.tar.gz sharkey-2ded8ba8580f49741e0d5e436f65561c8dd9ef18.tar.bz2 sharkey-2ded8ba8580f49741e0d5e436f65561c8dd9ef18.zip | |
Fix bug, Support thirdparty streaming access
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/streaming.ts | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/api/streaming.ts b/src/api/streaming.ts index 38068d1e3d..93d5f217b9 100644 --- a/src/api/streaming.ts +++ b/src/api/streaming.ts @@ -2,6 +2,7 @@ import * as http from 'http'; import * as websocket from 'websocket'; import * as redis from 'redis'; import User from './models/user'; +import Userkey from './models/userkey'; import homeStream from './stream/home'; import messagingStream from './stream/messaging'; @@ -17,7 +18,13 @@ module.exports = (server: http.Server) => { ws.on('request', async (request) => { const connection = request.accept(); - const user = await authenticate(connection); + const user = await authenticate(connection, request.resourceURL.query.i); + + if (user == null) { + connection.send('authentication-failed'); + connection.close(); + return; + } // Connect to Redis const subscriber = redis.createClient( @@ -41,29 +48,36 @@ module.exports = (server: http.Server) => { }); }; -function authenticate(connection: websocket.connection): Promise<any> { - return new Promise((resolve, reject) => { - // Listen first message - connection.once('message', async (data) => { - const msg = JSON.parse(data.utf8Data); - +function authenticate(connection: websocket.connection, token: string): Promise<any> { + return new Promise(async (resolve, reject) => { + if (token[0] == '!') { // Fetch user // SELECT _id const user = await User .findOne({ - token: msg.i + token: token }, { _id: true }); - if (user === null) { - connection.close(); - return; + resolve(user); + } else { + const userkey = await Userkey.findOne({ + key: token + }); + + if (userkey == null) { + return reject('invalid userkey'); } - connection.send('authenticated'); + // Fetch user + // SELECT _id + const user = await User + .findOne({ _id: userkey.user_id }, { + _id: true + }); resolve(user); - }); + } }); } |