diff --git a/jslib b/jslib index 6bcadc4f40..1370006f6e 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 6bcadc4f408db2c150753f53a07d6f8888b6e9ff +Subproject commit 1370006f6ea310cf85a12bcbd8213f74f9552c4d diff --git a/src/bw.ts b/src/bw.ts index 424b635035..cbe94e82ef 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -231,6 +231,26 @@ export class Main { this.cryptoFunctionService ); + this.twoFactorService = new TwoFactorService(this.i18nService, this.platformUtilsService); + + this.authService = new AuthService( + this.cryptoService, + this.apiService, + this.tokenService, + this.appIdService, + this.platformUtilsService, + this.messagingService, + this.logService, + this.keyConnectorService, + this.environmentService, + this.stateService, + this.twoFactorService, + this.i18nService + ); + + const lockedCallback = async () => + await this.cryptoService.clearStoredKey(KeySuffixOptions.Auto); + this.vaultTimeoutService = new VaultTimeoutService( this.cipherService, this.folderService, @@ -243,7 +263,8 @@ export class Main { this.policyService, this.keyConnectorService, this.stateService, - async () => await this.cryptoService.clearStoredKey(KeySuffixOptions.Auto), + this.authService, + lockedCallback, null ); @@ -294,23 +315,6 @@ export class Main { this.cryptoFunctionService ); - this.twoFactorService = new TwoFactorService(this.i18nService, this.platformUtilsService); - - this.authService = new AuthService( - this.cryptoService, - this.apiService, - this.tokenService, - this.appIdService, - this.platformUtilsService, - this.messagingService, - this.logService, - this.keyConnectorService, - this.environmentService, - this.stateService, - this.twoFactorService, - this.i18nService - ); - this.auditService = new AuditService(this.cryptoFunctionService, this.apiService); this.program = new Program(this); this.vaultProgram = new VaultProgram(this); diff --git a/src/commands/create.command.ts b/src/commands/create.command.ts index 8bb3c36e93..fa80861654 100644 --- a/src/commands/create.command.ts +++ b/src/commands/create.command.ts @@ -7,9 +7,9 @@ import { CryptoService } from "jslib-common/abstractions/crypto.service"; import { FolderService } from "jslib-common/abstractions/folder.service"; import { StateService } from "jslib-common/abstractions/state.service"; import { Utils } from "jslib-common/misc/utils"; -import { Cipher } from "jslib-common/models/export/cipher"; -import { Collection } from "jslib-common/models/export/collection"; -import { Folder } from "jslib-common/models/export/folder"; +import { CipherExport } from "jslib-common/models/export/cipherExport"; +import { CollectionExport } from "jslib-common/models/export/collectionExport"; +import { FolderExport } from "jslib-common/models/export/folderExport"; import { CollectionRequest } from "jslib-common/models/request/collectionRequest"; import { SelectionReadOnlyRequest } from "jslib-common/models/request/selectionReadOnlyRequest"; import { Response } from "jslib-node/cli/models/response"; @@ -72,8 +72,8 @@ export class CreateCommand { } } - private async createCipher(req: Cipher) { - const cipher = await this.cipherService.encrypt(Cipher.toView(req)); + private async createCipher(req: CipherExport) { + const cipher = await this.cipherService.encrypt(CipherExport.toView(req)); try { await this.cipherService.saveWithServer(cipher); const newCipher = await this.cipherService.get(cipher.id); @@ -145,8 +145,8 @@ export class CreateCommand { } } - private async createFolder(req: Folder) { - const folder = await this.folderService.encrypt(Folder.toView(req)); + private async createFolder(req: FolderExport) { + const folder = await this.folderService.encrypt(FolderExport.toView(req)); try { await this.folderService.saveWithServer(folder); const newFolder = await this.folderService.get(folder.id); @@ -183,7 +183,7 @@ export class CreateCommand { request.externalId = req.externalId; request.groups = groups; const response = await this.apiService.postCollection(req.organizationId, request); - const view = Collection.toView(req); + const view = CollectionExport.toView(req); view.id = response.id; const res = new OrganizationCollectionResponse(view, groups); return Response.success(res); diff --git a/src/commands/edit.command.ts b/src/commands/edit.command.ts index 82afa26f81..17b1c4b58b 100644 --- a/src/commands/edit.command.ts +++ b/src/commands/edit.command.ts @@ -3,9 +3,9 @@ import { CipherService } from "jslib-common/abstractions/cipher.service"; import { CryptoService } from "jslib-common/abstractions/crypto.service"; import { FolderService } from "jslib-common/abstractions/folder.service"; import { Utils } from "jslib-common/misc/utils"; -import { Cipher } from "jslib-common/models/export/cipher"; -import { Collection } from "jslib-common/models/export/collection"; -import { Folder } from "jslib-common/models/export/folder"; +import { CipherExport } from "jslib-common/models/export/cipherExport"; +import { CollectionExport } from "jslib-common/models/export/collectionExport"; +import { FolderExport } from "jslib-common/models/export/folderExport"; import { CollectionRequest } from "jslib-common/models/request/collectionRequest"; import { SelectionReadOnlyRequest } from "jslib-common/models/request/selectionReadOnlyRequest"; import { Response } from "jslib-node/cli/models/response"; @@ -69,7 +69,7 @@ export class EditCommand { } } - private async editCipher(id: string, req: Cipher) { + private async editCipher(id: string, req: CipherExport) { const cipher = await this.cipherService.get(id); if (cipher == null) { return Response.notFound(); @@ -79,7 +79,7 @@ export class EditCommand { if (cipherView.isDeleted) { return Response.badRequest("You may not edit a deleted item. Use the restore command first."); } - cipherView = Cipher.toView(req, cipherView); + cipherView = CipherExport.toView(req, cipherView); const encCipher = await this.cipherService.encrypt(cipherView); try { await this.cipherService.saveWithServer(encCipher); @@ -115,14 +115,14 @@ export class EditCommand { } } - private async editFolder(id: string, req: Folder) { + private async editFolder(id: string, req: FolderExport) { const folder = await this.folderService.get(id); if (folder == null) { return Response.notFound(); } let folderView = await folder.decrypt(); - folderView = Folder.toView(req, folderView); + folderView = FolderExport.toView(req, folderView); const encFolder = await this.folderService.encrypt(folderView); try { await this.folderService.saveWithServer(encFolder); @@ -167,7 +167,7 @@ export class EditCommand { request.externalId = req.externalId; request.groups = groups; const response = await this.apiService.putCollection(req.organizationId, id, request); - const view = Collection.toView(req); + const view = CollectionExport.toView(req); view.id = response.id; const res = new OrganizationCollectionResponse(view, groups); return Response.success(res); diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index f0ee9e6189..27ad8a99ea 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -13,15 +13,15 @@ import { SendType } from "jslib-common/enums/sendType"; import { Utils } from "jslib-common/misc/utils"; import { EncString } from "jslib-common/models/domain/encString"; import { Organization } from "jslib-common/models/domain/organization"; -import { Card } from "jslib-common/models/export/card"; -import { Cipher } from "jslib-common/models/export/cipher"; -import { Collection } from "jslib-common/models/export/collection"; -import { Field } from "jslib-common/models/export/field"; -import { Folder } from "jslib-common/models/export/folder"; -import { Identity } from "jslib-common/models/export/identity"; -import { Login } from "jslib-common/models/export/login"; -import { LoginUri } from "jslib-common/models/export/loginUri"; -import { SecureNote } from "jslib-common/models/export/secureNote"; +import { CardExport } from "jslib-common/models/export/cardExport"; +import { CipherExport } from "jslib-common/models/export/cipherExport"; +import { CollectionExport } from "jslib-common/models/export/collectionExport"; +import { FieldExport } from "jslib-common/models/export/fieldExport"; +import { FolderExport } from "jslib-common/models/export/folderExport"; +import { IdentityExport } from "jslib-common/models/export/identityExport"; +import { LoginExport } from "jslib-common/models/export/loginExport"; +import { LoginUriExport } from "jslib-common/models/export/loginUriExport"; +import { SecureNoteExport } from "jslib-common/models/export/secureNoteExport"; import { ErrorResponse } from "jslib-common/models/response/errorResponse"; import { CipherView } from "jslib-common/models/view/cipherView"; import { CollectionView } from "jslib-common/models/view/collectionView"; @@ -459,31 +459,31 @@ export class GetCommand extends DownloadCommand { let template: any = null; switch (id.toLowerCase()) { case "item": - template = Cipher.template(); + template = CipherExport.template(); break; case "item.field": - template = Field.template(); + template = FieldExport.template(); break; case "item.login": - template = Login.template(); + template = LoginExport.template(); break; case "item.login.uri": - template = LoginUri.template(); + template = LoginUriExport.template(); break; case "item.card": - template = Card.template(); + template = CardExport.template(); break; case "item.identity": - template = Identity.template(); + template = IdentityExport.template(); break; case "item.securenote": - template = SecureNote.template(); + template = SecureNoteExport.template(); break; case "folder": - template = Folder.template(); + template = FolderExport.template(); break; case "collection": - template = Collection.template(); + template = CollectionExport.template(); break; case "item-collections": template = ["collection-id1", "collection-id2"]; diff --git a/src/commands/serve.command.ts b/src/commands/serve.command.ts index bbea6caaa1..f5685efeac 100644 --- a/src/commands/serve.command.ts +++ b/src/commands/serve.command.ts @@ -96,7 +96,7 @@ export class ServeCommand { this.main.environmentService, this.main.syncService, this.main.stateService, - this.main.vaultTimeoutService + this.main.authService ); this.deleteCommand = new DeleteCommand( this.main.cipherService, diff --git a/src/commands/status.command.ts b/src/commands/status.command.ts index d10dbc92ac..724b2b03d5 100644 --- a/src/commands/status.command.ts +++ b/src/commands/status.command.ts @@ -1,7 +1,8 @@ +import { AuthService } from "jslib-common/abstractions/auth.service"; import { EnvironmentService } from "jslib-common/abstractions/environment.service"; import { StateService } from "jslib-common/abstractions/state.service"; import { SyncService } from "jslib-common/abstractions/sync.service"; -import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service"; +import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus"; import { Response } from "jslib-node/cli/models/response"; import { TemplateResponse } from "../models/response/templateResponse"; @@ -11,7 +12,7 @@ export class StatusCommand { private envService: EnvironmentService, private syncService: SyncService, private stateService: StateService, - private vaultTimeoutService: VaultTimeoutService + private authService: AuthService ) {} async run(): Promise { @@ -40,13 +41,14 @@ export class StatusCommand { return this.envService.getUrls().base; } - private async status(): Promise { - const authed = await this.stateService.getIsAuthenticated(); - if (!authed) { + private async status(): Promise<"unauthenticated" | "locked" | "unlocked"> { + const authStatus = await this.authService.getAuthStatus(); + if (authStatus === AuthenticationStatus.Unlocked) { + return "unlocked"; + } else if (authStatus === AuthenticationStatus.Locked) { + return "locked"; + } else { return "unauthenticated"; } - - const isLocked = await this.vaultTimeoutService.isLocked(); - return isLocked ? "locked" : "unlocked"; } } diff --git a/src/models/request/organizationCollectionRequest.ts b/src/models/request/organizationCollectionRequest.ts index a22ab1614a..71106ef70b 100644 --- a/src/models/request/organizationCollectionRequest.ts +++ b/src/models/request/organizationCollectionRequest.ts @@ -1,8 +1,8 @@ -import { Collection } from "jslib-common/models/export/collection"; +import { CollectionExport } from "jslib-common/models/export/collectionExport"; import { SelectionReadOnly } from "../selectionReadOnly"; -export class OrganizationCollectionRequest extends Collection { +export class OrganizationCollectionRequest extends CollectionExport { static template(): OrganizationCollectionRequest { const req = new OrganizationCollectionRequest(); req.organizationId = "00000000-0000-0000-0000-000000000000"; diff --git a/src/models/response/cipherResponse.ts b/src/models/response/cipherResponse.ts index 550ffe771d..3c61a5347b 100644 --- a/src/models/response/cipherResponse.ts +++ b/src/models/response/cipherResponse.ts @@ -1,5 +1,5 @@ import { CipherType } from "jslib-common/enums/cipherType"; -import { CipherWithIds } from "jslib-common/models/export/cipherWithIds"; +import { CipherWithIdExport } from "jslib-common/models/export/cipherWithIdsExport"; import { CipherView } from "jslib-common/models/view/cipherView"; import { BaseResponse } from "jslib-node/cli/models/response/baseResponse"; @@ -7,7 +7,7 @@ import { AttachmentResponse } from "./attachmentResponse"; import { LoginResponse } from "./loginResponse"; import { PasswordHistoryResponse } from "./passwordHistoryResponse"; -export class CipherResponse extends CipherWithIds implements BaseResponse { +export class CipherResponse extends CipherWithIdExport implements BaseResponse { object: string; attachments: AttachmentResponse[]; revisionDate: Date; diff --git a/src/models/response/collectionResponse.ts b/src/models/response/collectionResponse.ts index 4ee31364c8..6a7228b237 100644 --- a/src/models/response/collectionResponse.ts +++ b/src/models/response/collectionResponse.ts @@ -1,8 +1,8 @@ -import { CollectionWithId } from "jslib-common/models/export/collectionWithId"; +import { CollectionWithIdExport } from "jslib-common/models/export/collectionWithIdExport"; import { CollectionView } from "jslib-common/models/view/collectionView"; import { BaseResponse } from "jslib-node/cli/models/response/baseResponse"; -export class CollectionResponse extends CollectionWithId implements BaseResponse { +export class CollectionResponse extends CollectionWithIdExport implements BaseResponse { object: string; constructor(o: CollectionView) { diff --git a/src/models/response/folderResponse.ts b/src/models/response/folderResponse.ts index 3d59a24878..f1bc18eb31 100644 --- a/src/models/response/folderResponse.ts +++ b/src/models/response/folderResponse.ts @@ -1,8 +1,8 @@ -import { FolderWithId } from "jslib-common/models/export/folderWithId"; +import { FolderWithIdExport } from "jslib-common/models/export/folderWithIdExport"; import { FolderView } from "jslib-common/models/view/folderView"; import { BaseResponse } from "jslib-node/cli/models/response/baseResponse"; -export class FolderResponse extends FolderWithId implements BaseResponse { +export class FolderResponse extends FolderWithIdExport implements BaseResponse { object: string; constructor(o: FolderView) { diff --git a/src/models/response/loginResponse.ts b/src/models/response/loginResponse.ts index 9fec19db41..226ca4e8c5 100644 --- a/src/models/response/loginResponse.ts +++ b/src/models/response/loginResponse.ts @@ -1,7 +1,7 @@ -import { Login } from "jslib-common/models/export/login"; +import { LoginExport } from "jslib-common/models/export/loginExport"; import { LoginView } from "jslib-common/models/view/loginView"; -export class LoginResponse extends Login { +export class LoginResponse extends LoginExport { passwordRevisionDate: Date; constructor(o: LoginView) { diff --git a/src/program.ts b/src/program.ts index 2644bac9d6..4299eb34d3 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1,6 +1,7 @@ import * as chalk from "chalk"; import * as program from "commander"; +import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus"; import { KeySuffixOptions } from "jslib-common/enums/keySuffixOptions"; import { BaseProgram } from "jslib-node/cli/baseProgram"; import { LogoutCommand } from "jslib-node/cli/commands/logout.command"; @@ -232,12 +233,15 @@ export class Program extends BaseProgram { writeLn("", true); }) .option("--check", "Check lock status.", async () => { - const locked = await this.main.vaultTimeoutService.isLocked(); - if (!locked) { + await this.exitIfNotAuthed(); + + const authStatus = await this.main.authService.getAuthStatus(); + if (authStatus === AuthenticationStatus.Unlocked) { const res = new MessageResponse("Vault is unlocked!", null); this.processResponse(Response.success(res), true); + } else { + this.processResponse(Response.error("Vault is locked."), true); } - this.processResponse(Response.error("Vault is locked."), true); }) .option("--passwordenv ", "Environment variable storing your password") .option( @@ -460,7 +464,7 @@ export class Program extends BaseProgram { this.main.environmentService, this.main.syncService, this.main.stateService, - this.main.vaultTimeoutService + this.main.authService ); const response = await command.run(); this.processResponse(response);