forked from DeviceFarmer/stf
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from VKCOM/di/fix-websocket-auth-crash/QA-16055
Add try catch to cookie parser
- Loading branch information
Showing
3 changed files
with
19 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env -S node --import ./lib/util/instrument.mjs | ||
console.log('Starting stf') | ||
console.log('Starting DeviceHub') | ||
import '../lib/cli/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import * as dbapi from '../../../db/api.js' | ||
import * as jwtutil from '../../../util/jwtutil.js' | ||
import * as cookie from 'cookie' | ||
import logger from '../../../util/logger.js' | ||
|
||
|
||
export default (function(options) { | ||
const log = logger.createLogger('websocket') | ||
return function(socket, next) { | ||
let req = socket.request | ||
let token | ||
const cookies = cookie.parse(req.headers.cookie) | ||
let token, cookies | ||
try { | ||
cookies = cookie.parse(req.headers.cookie) | ||
} | ||
catch (e) { | ||
return next(new Error('Missing authorization token')) | ||
} | ||
if (cookies.token) { | ||
token = jwtutil.decode(cookies.token, options.secret) | ||
req.internalJwt = cookies.token | ||
} | ||
else { | ||
next(new Error('Missing authorization token')) | ||
return next(new Error('Missing authorization token')) | ||
} | ||
if (token) { | ||
return dbapi.loadUser(token.email) | ||
.then(function(user) { | ||
if (user) { | ||
req.user = user | ||
next() | ||
return next() | ||
} | ||
else { | ||
next(new Error('Invalid user')) | ||
return next(new Error('Invalid user')) | ||
} | ||
}) | ||
.catch(next) | ||
.catch((e) => { | ||
log.error(e) | ||
return next(new Error('Unknown error')) | ||
}) | ||
} | ||
else { | ||
next(new Error('Missing authorization token')) | ||
return next(new Error('Missing authorization token')) | ||
} | ||
} | ||
}) |