diff --git a/deadlock-plugins/deadlock-extension/package.json b/deadlock-plugins/deadlock-extension/package.json index bd2be05fb0d82950b6d6d0e3d739ee55c61844f9..9b43895b7d3006c2a4bee64493129f32027c550b 100644 --- a/deadlock-plugins/deadlock-extension/package.json +++ b/deadlock-plugins/deadlock-extension/package.json @@ -36,7 +36,7 @@ "category": "Deadlock Coding" }, { - "command": "deadlock.clear", + "command": "deadlock.disconnect", "title": "Clear cache", "category": "Deadlock Coding" } diff --git a/deadlock-plugins/deadlock-extension/resources/js/gettingStartedView.js b/deadlock-plugins/deadlock-extension/resources/js/gettingStartedView.js index 547953b63bc964364cd695380fdad8a1982613d7..8ff4e3bf981fe8a0a219c3ce46fc8da79d95c1ca 100644 --- a/deadlock-plugins/deadlock-extension/resources/js/gettingStartedView.js +++ b/deadlock-plugins/deadlock-extension/resources/js/gettingStartedView.js @@ -11,3 +11,9 @@ function openAuthenticationPageAction() { command: 'openAuthenticationPageAction', }); } + +function disconnectUserAction() { + vscode.postMessage({ + command: 'disconnectUserAction', + }); +} diff --git a/deadlock-plugins/deadlock-extension/src/core/commandHandler.ts b/deadlock-plugins/deadlock-extension/src/core/commandHandler.ts index 75f5518f904cb3848ddc01608e06a714724c3c8f..fe216cce724da44b5ccbd5435382ca1dde2f3f49 100644 --- a/deadlock-plugins/deadlock-extension/src/core/commandHandler.ts +++ b/deadlock-plugins/deadlock-extension/src/core/commandHandler.ts @@ -11,12 +11,12 @@ export class CommandHandler { chooseMissionWorkdirCommand.cmd, this.controller.chooseMissionWorkdir.bind(this.controller), ); + commands.registerCommand(disconnectCommand.cmd, this.controller.disconnect.bind(this.controller)); commands.registerCommand(authenticateCommand.cmd, this.controller.authenticate.bind(this.controller)); - commands.registerCommand(clearCommand.cmd, this.controller.clear.bind(this.controller)); } } export const chooseMissionWorkdirCommand = new Command('Choose mission workdir', 'deadlock.chooseMissionWorkdir'); +export const disconnectCommand = new Command('Disconnect', 'deadlock.disconnect'); export const authenticateCommand = new Command('Authenticate', 'deadlock.authenticate'); -export const clearCommand = new Command('Clear', 'deadlock.clear'); export const openUrlInBrowserCommand = new Command('Open url in browser', 'vscode.open'); diff --git a/deadlock-plugins/deadlock-extension/src/core/controller.ts b/deadlock-plugins/deadlock-extension/src/core/controller.ts index b93c6060deb7a59f8701dc8292636c907064819f..07810e71f591b55319fe7cc63eb63b67b27f3f11 100644 --- a/deadlock-plugins/deadlock-extension/src/core/controller.ts +++ b/deadlock-plugins/deadlock-extension/src/core/controller.ts @@ -10,7 +10,9 @@ import { Mission } from './mission/models/mission'; import { MissionDevContainer } from './mission/missionDevContainer'; import { extensionLog as log } from '../recorder/utils/log'; import { commands, ExtensionContext, Uri, window } from 'vscode'; -import { createSshKeyFiles, isSshKeyPairExist } from './sshKeyManager'; +import { createSshKeyFiles } from './sshKeyManager'; +import { removeFiles } from '../recorder/utils/workdir'; +import { userSshKeyFolderPath } from './config'; import { hasStatusNumber } from './utils/typeguards'; import { User } from './mission/models/userChallenge'; @@ -97,13 +99,13 @@ export default class Controller { } } - public async clear() { + public async disconnect() { + await removeFiles(`${userSshKeyFolderPath}/id_rsa`, `${userSshKeyFolderPath}/id_rsa.pub`); await this.extensionStore.clear(); this.quickSetupView.isAlreadyConnected = false; } - public async createSshKeyPairIfNotExist() { - if (isSshKeyPairExist()) return; + public async createSshKeyPair() { const { publicKey, privateKey } = await this.apiService.getUserSshKey(); await createSshKeyFiles(publicKey, privateKey); } @@ -113,7 +115,7 @@ export default class Controller { const tokens = await this.connection.getToken({ openLink: Controller.openBrowserWithUrl }); await this.extensionStore.setAccessToken(tokens.accessToken); await this.extensionStore.setRefreshToken(tokens.refreshToken); - await this.createSshKeyPairIfNotExist(); + await this.createSshKeyPair(); this.quickSetupView.isAlreadyConnected = true; } diff --git a/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts b/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts index ce92b124f79d3488b23582f6c3edece7d1a1cc0e..9759b5e1559f2e1252a1c3a5740dc9beb47e99a9 100644 --- a/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts +++ b/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts @@ -1,6 +1,5 @@ import { ExtensionContext, Memento, SecretStorage, window } from 'vscode'; import { extensionLog as log } from '../recorder/utils/log'; -import { removeFiles } from '../recorder/utils/workdir'; export type GlobalStorageType = Memento & { setKeysForSync(keys: readonly string[]): void }; @@ -23,7 +22,6 @@ export default class ExtensionStore { } if (await this.secretStorage.get(StoreKey.AccessTokenKey)) this.secretStorage.delete(StoreKey.AccessTokenKey); if (await this.secretStorage.get(StoreKey.RefreshTokenKey)) this.secretStorage.delete(StoreKey.RefreshTokenKey); - await removeFiles(`${process.env.HOME}/.deadlock/.ssh/id_rsa`, `${process.env.HOME}/.deadlock/.ssh/id_rsa.pub`); } public static getInstance(context?: ExtensionContext): ExtensionStore { diff --git a/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts b/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts index c3fdbb764f09ffa71ca1db5313e6cd09becb886a..7526d29da5f70472c7620e1d9038ad7fca97f645 100644 --- a/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts +++ b/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts @@ -1,30 +1,31 @@ import { existsSync, promises } from 'fs'; import { userSshKeyFolderPath } from './config'; - export function isSshKeyPairExist(): boolean { - return isPrivateKeyExist() && isPublicKeyExist(); - } +export function isSshKeyPairExist(): boolean { + return isPrivateKeyExist() && isPublicKeyExist(); +} - function isPublicKeyExist(): boolean { - return existsSync(`${userSshKeyFolderPath}/id_rsa.pub`); - } +function isPublicKeyExist(): boolean { + return existsSync(`${userSshKeyFolderPath}/id_rsa.pub`); +} - function isPrivateKeyExist(): boolean { - return existsSync(`${userSshKeyFolderPath}/id_rsa`); - } +function isPrivateKeyExist(): boolean { + return existsSync(`${userSshKeyFolderPath}/id_rsa`); +} - export async function createSshKeyFiles(publicKey: string, privateKey: string) { - await createSshKeyFolderIfNotExist(userSshKeyFolderPath); - await promises.writeFile(`${userSshKeyFolderPath}/id_rsa.pub`, publicKey); +export async function createSshKeyFiles(publicKey: string, privateKey: string) { + await createSshKeyFolderIfNotExist(userSshKeyFolderPath); + await promises.writeFile(`${userSshKeyFolderPath}/id_rsa.pub`, publicKey); - await promises.writeFile(`${userSshKeyFolderPath}/id_rsa`, privateKey, { mode: 0o600 }); - } - async function createSshKeyFolderIfNotExist(sshKeyFolderPath) { - if (!isSshKeyFolderExist(sshKeyFolderPath)) { - await promises.mkdir(sshKeyFolderPath, { recursive: true }); - } - } + await promises.writeFile(`${userSshKeyFolderPath}/id_rsa`, privateKey, { mode: 0o600 }); +} - export function isSshKeyFolderExist(sshKeyFolderPath: string) { - return existsSync(sshKeyFolderPath); +async function createSshKeyFolderIfNotExist(sshKeyFolderPath) { + if (!isSshKeyFolderExist(sshKeyFolderPath)) { + await promises.mkdir(sshKeyFolderPath, { recursive: true }); } +} + +export function isSshKeyFolderExist(sshKeyFolderPath: string) { + return existsSync(sshKeyFolderPath); +} diff --git a/deadlock-plugins/deadlock-extension/src/view/quickSetupView.ts b/deadlock-plugins/deadlock-extension/src/view/quickSetupView.ts index a87f244bd0578fcadd44a99c1c3d1471b2763a3e..39fa8f8f5aa3fae2ffb31365d4279b41f554de71 100644 --- a/deadlock-plugins/deadlock-extension/src/view/quickSetupView.ts +++ b/deadlock-plugins/deadlock-extension/src/view/quickSetupView.ts @@ -1,5 +1,5 @@ import { commands, Uri } from 'vscode'; -import { authenticateCommand, chooseMissionWorkdirCommand } from '../core/commandHandler'; +import { authenticateCommand, chooseMissionWorkdirCommand, disconnectCommand } from '../core/commandHandler'; import ExtensionStore from '../core/extensionStore'; import { extensionLog as log } from '../recorder/utils/log'; import { openQuickSetupCommand } from '../theia/command'; @@ -47,8 +47,15 @@ export default class QuickSetupView extends WebviewBase { ${this.renderCardHtml( 'Connexion à Deadlock', "Tu as besoin d'être connecté à Deadlock pour continuer.", - { name: 'Se connecter', onClickFunctionName: 'openAuthenticationPageAction' }, - this._isAlreadyConnected, + this._isAlreadyConnected + ? { + name: 'Déconnexion', + onClickFunctionName: 'openAuthenticationPageAction', + } + : { + name: 'Connexion', + onClickFunctionName: 'disconnectUserAction', + }, this._isAlreadyConnected, )} ${this.renderCardHtml( @@ -126,6 +133,9 @@ export default class QuickSetupView extends WebviewBase { case 'openAuthenticationPageAction': commands.executeCommand(authenticateCommand.cmd); return; + case 'disconnectUserAction': + commands.executeCommand(disconnectCommand.cmd); + return; } }