Skip to content

Commit 0cdbd33

Browse files
committed
refactor: make authenticated async everywhere
Since this checks if they are authenticated using the hash/password and it's async, we need to update authenticated to be async, which means we have to update it everywhere it's used.
1 parent fcc3f0d commit 0cdbd33

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

src/node/http.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ export const replaceTemplates = <T extends object>(
4545
/**
4646
* Throw an error if not authorized. Call `next` if provided.
4747
*/
48-
export const ensureAuthenticated = (req: express.Request, _?: express.Response, next?: express.NextFunction): void => {
49-
if (!authenticated(req)) {
48+
export const ensureAuthenticated = async (
49+
req: express.Request,
50+
_?: express.Response,
51+
next?: express.NextFunction,
52+
): Promise<void> => {
53+
const isAuthenticated = await authenticated(req)
54+
if (!isAuthenticated) {
5055
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
5156
}
5257
if (next) {
@@ -57,17 +62,19 @@ export const ensureAuthenticated = (req: express.Request, _?: express.Response,
5762
/**
5863
* Return true if authenticated via cookies.
5964
*/
60-
export const authenticated = (req: express.Request): boolean => {
65+
export const authenticated = async (req: express.Request): Promise<boolean> => {
6166
switch (req.args.auth) {
6267
case AuthType.None:
6368
return true
6469
case AuthType.Password:
6570
// The password is stored in the cookie after being hashed.
71+
// TODO@jsjoeio this also needs to be refactored to check if they're using the legacy password
72+
// or the new one. we can't assume hashed-password means legacy
6673
return !!(
6774
req.cookies.key &&
6875
(req.args["hashed-password"]
6976
? safeCompare(req.cookies.key, req.args["hashed-password"])
70-
: req.args.password && isHashMatch(req.args.password, req.cookies.key))
77+
: req.args.password && (await isHashMatch(req.args.password, req.cookies.key)))
7178
)
7279
default:
7380
throw new Error(`Unsupported auth type ${req.args.auth}`)

src/node/routes/domainProxy.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ const maybeProxy = (req: Request): string | undefined => {
3232
return port
3333
}
3434

35-
router.all("*", (req, res, next) => {
35+
router.all("*", async (req, res, next) => {
3636
const port = maybeProxy(req)
3737
if (!port) {
3838
return next()
3939
}
4040

4141
// Must be authenticated to use the proxy.
42-
if (!authenticated(req)) {
42+
const isAuthenticated = await authenticated(req)
43+
if (!isAuthenticated) {
4344
// Let the assets through since they're used on the login page.
4445
if (req.path.startsWith("/static/") && req.method === "GET") {
4546
return next()

src/node/routes/login.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ const limiter = new RateLimiter()
4949

5050
export const router = Router()
5151

52-
router.use((req, res, next) => {
52+
router.use(async (req, res, next) => {
5353
const to = (typeof req.query.to === "string" && req.query.to) || "/"
54-
if (authenticated(req)) {
54+
if (await authenticated(req)) {
5555
return redirect(req, res, to, { to: undefined })
5656
}
5757
next()

src/node/routes/static.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ router.get("/(:commit)(/*)?", async (req, res) => {
4343

4444
// Make sure it's in code-server if you aren't authenticated. This lets
4545
// unauthenticated users load the login assets.
46-
if (!resourcePath.startsWith(rootPath) && !authenticated(req)) {
46+
const isAuthenticated = await authenticated(req)
47+
if (!resourcePath.startsWith(rootPath) && !isAuthenticated) {
4748
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
4849
}
4950

src/node/routes/vscode.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const router = Router()
1919
const vscode = new VscodeProvider()
2020

2121
router.get("/", async (req, res) => {
22-
if (!authenticated(req)) {
22+
const isAuthenticated = await authenticated(req)
23+
if (!isAuthenticated) {
2324
return redirect(req, res, "login", {
2425
// req.baseUrl can be blank if already at the root.
2526
to: req.baseUrl && req.baseUrl !== "/" ? req.baseUrl : undefined,

0 commit comments

Comments
 (0)