Select Git revision
controller.ts
-
Christian ZHENG authoredChristian ZHENG authored
controller.ts 3.40 KiB
import * as vscode from 'vscode';
import { KEYCLOAK_DEVICE_AUTH_URL, KEYCLOAK_TOKEN_CREATE_URL, KEYCLOAK_USER_INFO_URL } from '../config';
import { OPEN_GETTING_STARTED_COMMAND } from '../theia/command';
import BriefingView from '../view/briefingView';
import GettingStartedView from '../view/gettingStartedView';
import { CHOOSE_MISSION_WORKDIR_COMMAND, CommandHandler } from './commandHandler';
import ExtensionStore from './extensionStore';
import KeycloakOAuth2DeviceFlowConnection from './keycloakOAuth2DeviceFlowConnection';
export default class Controller {
public connection: KeycloakOAuth2DeviceFlowConnection;
private commandHandler: CommandHandler;
private briefingView: BriefingView;
private gettingStartedView: GettingStartedView;
constructor(private context: vscode.ExtensionContext) {
this.briefingView = new BriefingView();
this.gettingStartedView = new GettingStartedView(context.extensionUri);
this.commandHandler = new CommandHandler(this);
this.connection = new KeycloakOAuth2DeviceFlowConnection(
KEYCLOAK_DEVICE_AUTH_URL,
KEYCLOAK_TOKEN_CREATE_URL,
KEYCLOAK_USER_INFO_URL,
);
this.init();
}
private async init() {
const that = this;
vscode.window.registerUriHandler({
handleUri(uri: vscode.Uri) {
const queryParams: URLSearchParams = new URLSearchParams(uri.query);
const action: string | null = queryParams.get('action');
switch (action) {
case 'open-challenge':
that.launchMission(queryParams.get('missionId'));
break;
default:
vscode.window.showInformationMessage('Aucune action trouvée!');
}
},
});
const exensionStorage = ExtensionStore.getInstance();
this.gettingStartedView.isAlreadyConnected = !!(await exensionStorage.getAccessToken());
}
public async clear() {
const exensionStorage = ExtensionStore.getInstance();
await exensionStorage.clear();
this.gettingStartedView.isAlreadyConnected = false;
}
public async authenticate() {
// WARN generate a new device code every time student clicks on log in button. Should I keep it ?\
// The answer might be 'yes' because when the student is already authenticated, the log in button should be disabled.
await this.connection.registerDevice();
const tokens = await this.connection.getToken({ openLink: Controller.openBrowserWithUrl });
const exensionStorage = ExtensionStore.getInstance();
await exensionStorage.setAccessToken(tokens.accessToken);
await exensionStorage.setRefreshToken(tokens.refreshToken);
this.gettingStartedView.isAlreadyConnected = true;
}
public static openBrowserWithUrl(url: string) {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(url));
}
public async launchMission(missionId: string | null) {
if (missionId) {