Skip to content

Commit

Permalink
fix(auth): send a 401 status on expired token (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-field authored and benjie committed May 25, 2017
1 parent 0c16a3e commit fa241f4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/postgraphql/withPostGraphQLContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function setupPgClientTransaction ({
// Try to run `jwt.verify`. If it fails, capture the error and re-throw it
// as a 403 error because the token is not trustworthy.
try {
// If a JWT token was defined, but a secret was not procided to the server
// If a JWT token was defined, but a secret was not provided to the server
// throw a 403 error.
if (typeof jwtSecret !== 'string')
throw new Error('Not allowed to provide a JWT token.')
Expand All @@ -136,9 +136,16 @@ async function setupPgClientTransaction ({
}
}
catch (error) {
// In case this error is thrown in an HTTP context, we want to add a 403
// status code.
error.statusCode = 403
// In case this error is thrown in an HTTP context, we want to add status code
// Note. jwt.verify will add a name key to its errors. (https://github.com/auth0/node-jsonwebtoken#errors--codes)
if ( ('name' in error) && error.name === 'TokenExpiredError') {
// The correct status code for an expired ( but otherwise acceptable token is 401 )
error.statusCode = 401
} else {
// All other authentication errors should get a 403 status code.
error.statusCode = 403
}

throw error
}
}
Expand Down

0 comments on commit fa241f4

Please sign in to comment.