Skip to content

Commit

Permalink
Delegate authentication to plugins
Browse files Browse the repository at this point in the history
Unfortunately since plugins can now override the root this is necessary
or just can't log in.
  • Loading branch information
code-asher committed Feb 12, 2021
1 parent c2450d6 commit 3fc556d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as semver from "semver"
import * as pluginapi from "../../typings/pluginapi"
import { HttpCode, HttpError } from "../common/http"
import { version } from "./constants"
import { ensureAuthenticated, replaceTemplates } from "./http"
import { authenticated, ensureAuthenticated, replaceTemplates } from "./http"
import { proxy } from "./proxy"
import * as util from "./util"
import { Router as WsRouter, WebsocketRouter, wss } from "./wsRouter"
Expand All @@ -28,11 +28,13 @@ require("module")._load = function (request: string, parent: object, isMain: boo
* The module you get when importing "code-server".
*/
export const codeServer = {
express,
field,
HttpCode,
HttpError,
Level,
authenticated,
ensureAuthenticated,
express,
field,
proxy,
replaceTemplates,
WsRouter,
Expand Down Expand Up @@ -122,10 +124,10 @@ export class PluginAPI {
public mount(r: express.Router, wr: express.Router): void {
for (const [, p] of this.plugins) {
if (p.router) {
r.use(`${p.routerPath}`, ensureAuthenticated, p.router())
r.use(`${p.routerPath}`, p.router())
}
if (p.wsRouter) {
wr.use(`${p.routerPath}`, ensureAuthenticated, (p.wsRouter() as WebsocketRouter).router)
wr.use(`${p.routerPath}`, (p.wsRouter() as WebsocketRouter).router)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions typings/pluginapi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ export { field, Level, Logger }
*/
export const proxy: ProxyServer

/**
* Middleware to ensure the user is authenticated. Throws if they are not.
*/
export function ensureAuthenticated(req: express.Request, res?: express.Response, next?: express.NextFunction): void

/**
* Returns true if the user is authenticated.
*/
export function authenticated(req: express.Request): boolean

/**
* Replace variables in HTML: TO, BASE, CS_STATIC_BASE, and OPTIONS.
*/
Expand Down

0 comments on commit 3fc556d

Please sign in to comment.