Skip to content

feat: SAML #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"default": "https://sourceacademy.nus.edu.sg",
"description": "URL to the Source Academy frontend"
},
"source-academy.backendBaseUrl": {
"type": "string",
"default": "https://api.sourceacademy.nus.edu.sg",
"description": "URL to the Source Academy backend (only needed for SAML logins)"
},
"source-academy.workspaceFolder": {
"type": "string",
"default": ".sourceacademy",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { runLanguagePicker } from "./language";
import { evalEditor } from "./evalEditor";
import { showPanel } from "./showPanel";
import { navigate } from "./navigate";
import { login } from "./login";

const EXTENSION_ID = "source-academy";

Expand All @@ -12,9 +13,11 @@ const EXTENSION_ID = "source-academy";
*/
const commands = (context: vscode.ExtensionContext) => ({
pick: () => runLanguagePicker(context),
"show-panel": (route?: string) => showPanel(context, route),
"show-panel": (route?: string, altUrl?: string) =>
showPanel(context, route, altUrl),
"eval-editor": () => evalEditor(context),
navigate: (route: string) => navigate(context, route),
login: () => login(context),
});

/**
Expand Down
6 changes: 4 additions & 2 deletions src/commands/showPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let messageHandler = MessageHandler.getInstance();
export async function showPanel(
context: vscode.ExtensionContext,
route?: string,
altUrl?: string,
) {
let language: string | undefined = context.workspaceState.get("language");
if (!language) {
Expand All @@ -41,9 +42,10 @@ export async function showPanel(
);
}

const iframeUrl = new URL(route ?? "/playground", config.frontendBaseUrl)
.href;
const iframeUrl =
altUrl ?? new URL(route ?? "/playground", config.frontendBaseUrl).href;

// equivalent to panel.webview.html = ...
setWebviewContent(
messageHandler.panel,
context,
Expand Down
24 changes: 18 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,26 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.registerUriHandler({
handleUri(uri: vscode.Uri) {
const searchParams = new URLSearchParams(uri.query);
// The following two params are available when logging in via OAuth providers

const code = searchParams.get("code");
const clientRequestId = searchParams.get("client-request-id");
// The following params are available conditionally based on provider
const clientRequestId = searchParams.get("client-request-id"); // OAuth (NUS's IdP)
const provider = searchParams.get("provider"); // SAML

let route, url;
if (clientRequestId) {
// OAuth
route = `/login/vscode_callback?code=${code}&client-request-id=${clientRequestId}`;
url = null;
} else if (provider) {
// SAML
route = null;
// TODO: Let the frontend handle the contacting of backend, instead of us.
// Then, remove backendBaseUrl from schema and altUrl from showPanel
url = `${config.backendBaseUrl}/v2/auth/exchange?code=${code}&provider=${provider}`;
}

vscode.commands.executeCommand(
"source-academy.show-panel",
`/login/vscode_callback?code=${code}&client-request-id=${clientRequestId}`,
);
vscode.commands.executeCommand("source-academy.show-panel", route, url);
},
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export default {
type: "string",
default: "https://sourceacademy.nus.edu.sg",
},
// TODO: Remove this config in the future. See the login URI handler for details.
backendBaseUrl: {
type: "string",
default: "https://api.sourceacademy.nus.edu.sg",
},
workspaceFolder: {
type: "string",
default: ".sourceacademy",
Expand Down
8 changes: 5 additions & 3 deletions src/utils/messageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ export class MessageHandler {
this.panel?.reveal(vscode.ViewColumn.Two);
}
break;
case MessageTypeNames.LoginWithBrowser:
const { route } = message;
vscode.env.openExternal(vscode.Uri.parse(route));
case MessageTypeNames.LoginWithBrowser:
let { route } = message;
// TODO: Remove this hack! This should be changed in the frontend
route = route.replace("saml_redirect", "saml_redirect_vscode");
vscode.env.openExternal(vscode.Uri.parse(route));
}
console.log(`${Date.now()} Finish handleMessage: ${message.type}`);
}
Expand Down