diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 37700e18..036e118e 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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" @@ -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, @@ -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) } } } diff --git a/typings/pluginapi.d.ts b/typings/pluginapi.d.ts index b93a82b5..f7797b6d 100644 --- a/typings/pluginapi.d.ts +++ b/typings/pluginapi.d.ts @@ -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. */