-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Gracefully handle expired JWTs (#4195)
- Loading branch information
1 parent
1c3b7a8
commit 24fdb28
Showing
16 changed files
with
142 additions
and
55 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { ErrorRequestHandler } from "express"; | ||
import { ServerError } from "./serverError.js"; | ||
import airbrake from "../airbrake.js"; | ||
|
||
/** | ||
* Check for expired JWTs, redirect to /logout if found | ||
*/ | ||
export const expiredJWTHandler: ErrorRequestHandler = ( | ||
errorObject, | ||
_res, | ||
res, | ||
next, | ||
) => { | ||
const isJWTExpiryError = | ||
errorObject?.name === "UnauthorizedError" && | ||
errorObject?.message === "jwt expired"; | ||
|
||
if (!isJWTExpiryError) return next(errorObject); | ||
|
||
const logoutPage = new URL("/logout", process.env.EDITOR_URL_EXT!).toString(); | ||
return res.redirect(logoutPage); | ||
}; | ||
|
||
/** | ||
* Fallback error handler | ||
* Must be final Express middleware | ||
*/ | ||
export const errorHandler: ErrorRequestHandler = ( | ||
errorObject, | ||
_req, | ||
res, | ||
_next, | ||
) => { | ||
const { status = 500, message = "Something went wrong" } = (() => { | ||
if ( | ||
airbrake && | ||
(errorObject instanceof Error || errorObject instanceof ServerError) | ||
) { | ||
airbrake.notify(errorObject); | ||
return { | ||
...errorObject, | ||
message: errorObject.message.concat(", this error has been logged"), | ||
}; | ||
} else { | ||
console.log(errorObject); | ||
return errorObject; | ||
} | ||
})(); | ||
|
||
res.status(status).send({ | ||
error: message, | ||
}); | ||
}; |
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
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
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
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
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,7 +1,13 @@ | ||
import type { Role } from "@opensystemslab/planx-core/types"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
function getJWT({ role }: { role: Role }) { | ||
function getTestJWT({ | ||
role, | ||
isExpired = false, | ||
}: { | ||
role: Role; | ||
isExpired?: boolean; | ||
}) { | ||
const data = { | ||
sub: "123", | ||
email: "[email protected]", | ||
|
@@ -10,13 +16,23 @@ function getJWT({ role }: { role: Role }) { | |
"x-hasura-default-role": role, | ||
"x-hasura-user-id": "123", | ||
}, | ||
// 1st Jan, 1970 | ||
...(isExpired && { exp: 0 }), | ||
}; | ||
|
||
return jwt.sign(data, process.env.JWT_SECRET!, { expiresIn: "24h" }); | ||
return isExpired | ||
? // Expiry hardcoded to token | ||
jwt.sign(data, process.env.JWT_SECRET!) | ||
: // Generate standard 24hr expiry | ||
jwt.sign(data, process.env.JWT_SECRET!, { expiresIn: "24h" }); | ||
} | ||
|
||
function authHeader({ role }: { role: Role }) { | ||
return { Authorization: `Bearer ${getJWT({ role })}` }; | ||
return { Authorization: `Bearer ${getTestJWT({ role })}` }; | ||
} | ||
|
||
export { authHeader, getJWT }; | ||
function expiredAuthHeader({ role }: { role: Role }) { | ||
return { Authorization: `Bearer ${getTestJWT({ role, isExpired: true })}` }; | ||
} | ||
|
||
export { authHeader, getTestJWT, expiredAuthHeader }; |
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
Oops, something went wrong.