From 6fff69c0ea133e8bf8616b23ee34e45509846d1e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 15 May 2018 23:26:36 -0400 Subject: [PATCH] logout command --- src/bw.ts | 26 +++++++++++++++---- src/commands/logout.command.ts | 17 ++++++++++++ src/program.ts | 9 +++++-- ...ng.service.ts => noopMessaging.service.ts} | 4 +-- 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/commands/logout.command.ts rename src/services/{nodeMessaging.service.ts => noopMessaging.service.ts} (58%) diff --git a/src/bw.ts b/src/bw.ts index 3754818737..8db8e4f297 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -2,9 +2,9 @@ import { AuthService } from 'jslib/services/auth.service'; import { I18nService } from './services/i18n.service'; import { NodeEnvSecureStorageService } from './services/nodeEnvSecureStorage.service'; -import { NodeMessagingService } from './services/nodeMessaging.service'; import { NodePlatformUtilsService } from './services/nodePlatformUtils.service'; import { NodeStorageService } from './services/nodeStorage.service'; +import { NoopMessagingService } from './services/noopMessaging.service'; import { AppIdService } from 'jslib/services/appId.service'; import { AuditService } from 'jslib/services/audit.service'; @@ -28,7 +28,7 @@ import { UserService } from 'jslib/services/user.service'; import { Program } from './program'; export class Main { - messagingService: NodeMessagingService; + messagingService: NoopMessagingService; storageService: NodeStorageService; secureStorageService: NodeStorageService; i18nService: I18nService; @@ -64,9 +64,9 @@ export class Main { this.cryptoFunctionService); this.appIdService = new AppIdService(this.storageService); this.tokenService = new TokenService(this.storageService); - this.messagingService = new NodeMessagingService(); + this.messagingService = new NoopMessagingService(); this.apiService = new NodeApiService(this.tokenService, this.platformUtilsService, - (expired: boolean) => { /* do nothing */ }); + (expired: boolean) => { this.logout(); }); this.environmentService = new EnvironmentService(this.apiService, this.storageService); this.userService = new UserService(this.tokenService, this.storageService); this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService); @@ -82,7 +82,7 @@ export class Main { () => { /* do nothing */ }); this.syncService = new SyncService(this.userService, this.apiService, this.settingsService, this.folderService, this.cipherService, this.cryptoService, this.collectionService, - this.storageService, this.messagingService, (expired: boolean) => { /* do nothing */ }); + this.storageService, this.messagingService, (expired: boolean) => { this.logout(); }); this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService); this.totpService = new TotpService(this.storageService, this.cryptoFunctionService); this.authService = new AuthService(this.cryptoService, this.apiService, this.userService, this.tokenService, @@ -95,6 +95,22 @@ export class Main { this.program.run(); } + async logout() { + process.env.BW_SESSION = null; + const userId = await this.userService.getUserId(); + await Promise.all([ + this.syncService.setLastSync(new Date(0)), + this.tokenService.clearToken(), + this.cryptoService.clearKeys(), + this.userService.clear(), + this.settingsService.clear(userId), + this.cipherService.clear(userId), + this.folderService.clear(userId), + this.collectionService.clear(userId), + this.passwordGenerationService.clear(), + ]); + } + private async init() { this.containerService.attachToWindow(global); await this.environmentService.setUrlsFromStorage(); diff --git a/src/commands/logout.command.ts b/src/commands/logout.command.ts new file mode 100644 index 0000000000..97ea00c1b4 --- /dev/null +++ b/src/commands/logout.command.ts @@ -0,0 +1,17 @@ +import * as program from 'commander'; + +import { AuthService } from 'jslib/abstractions/auth.service'; + +import { Response } from '../models/response'; +import { MessageResponse } from '../models/response/messageResponse'; + +export class LogoutCommand { + constructor(private authService: AuthService, private logoutCallback: () => Promise) { } + + async run(cmd: program.Command) { + await this.logoutCallback(); + this.authService.logOut(() => { /* Do nothing */ }); + const res = new MessageResponse('You have logged out.', null); + return Response.success(res); + } +} diff --git a/src/program.ts b/src/program.ts index e11c7a26a4..cf6d72ddc7 100644 --- a/src/program.ts +++ b/src/program.ts @@ -10,6 +10,7 @@ import { EncodeCommand } from './commands/encode.command'; import { GetCommand } from './commands/get.command'; import { ListCommand } from './commands/list.command'; import { LoginCommand } from './commands/login.command'; +import { LogoutCommand } from './commands/logout.command'; import { SyncCommand } from './commands/sync.command'; import { Response } from './models/response'; @@ -48,7 +49,7 @@ export class Program { .option('-m, --method ', 'Two-step login method.') .option('-c, --code ', 'Two-step login code.') .action(async (email: string, password: string, cmd: program.Command) => { - // await this.exitIfAuthed(); + await this.exitIfAuthed(); const command = new LoginCommand(this.main.authService, this.main.apiService, this.main.cryptoFunctionService); const response = await command.run(email, password, cmd); @@ -60,7 +61,11 @@ export class Program { .description('Log out of the current Bitwarden user account.') .action(async (cmd) => { await this.exitIfNotAuthed(); - // TODO + const command = new LogoutCommand(this.main.authService, async () => { + await this.main.logout(); + }); + const response = await command.run(cmd); + this.processResponse(response, cmd); }); program diff --git a/src/services/nodeMessaging.service.ts b/src/services/noopMessaging.service.ts similarity index 58% rename from src/services/nodeMessaging.service.ts rename to src/services/noopMessaging.service.ts index 5eacfd9143..d6a824edb0 100644 --- a/src/services/nodeMessaging.service.ts +++ b/src/services/noopMessaging.service.ts @@ -1,7 +1,7 @@ import { MessagingService } from 'jslib/abstractions/messaging.service'; -export class NodeMessagingService implements MessagingService { +export class NoopMessagingService implements MessagingService { send(subscriber: string, arg: any = {}) { - // TODO + // Do nothing... } }