Skip to content
Snippets Groups Projects
Select Git revision
  • 7a5917903a6ad2d924d671a8062bd5111df43816
  • master default protected
  • docs-improve_user_path_description
  • fix-pipeline
  • ci-change_exercises_image_registry
  • feat-merge_recorder_in_extension
  • feat-default_folder
  • feat-change_user
  • develop protected
  • refactor-mission
  • feat-exercise_automatic_save
  • docs-improve_documentation
  • feat-create_little_container_for_developer
  • feat-local-dev
  • 0.1.12
  • 0.1.11
  • 0.1.10
  • 0.1.5
18 results

controller.ts

Blame
  • controller.ts 3.47 KiB
    import * as vscode from 'vscode';
    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(
    			'https://auth.dev.deadlock.io/auth/realms/Deadlock/protocol/openid-connect/auth/device',
    			'https://auth.dev.deadlock.io/auth/realms/Deadlock/protocol/openid-connect/token',
    			'https://auth.dev.deadlock.io/auth/realms/Deadlock/protocol/openid-connect/userinfo',
    		);
    
    		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) {
    			vscode.window.showInformationMessage(`vous lancez la mission ${missionId}`);