From af43232567fa63911725929d549f2b01927fa243 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 3 Jul 2018 12:06:01 -0400 Subject: [PATCH] make keypair on login if missing --- src/abstractions/api.service.ts | 2 ++ src/angular/components/register.component.ts | 8 +++----- src/models/request/keysRequest.ts | 9 +++++++++ src/models/request/registerRequest.ts | 14 +++----------- src/services/api.service.ts | 5 +++++ src/services/auth.service.ts | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/models/request/keysRequest.ts diff --git a/src/abstractions/api.service.ts b/src/abstractions/api.service.ts index 21dc924ec12..6e1a21261c1 100644 --- a/src/abstractions/api.service.ts +++ b/src/abstractions/api.service.ts @@ -12,6 +12,7 @@ import { FolderRequest } from '../models/request/folderRequest'; import { ImportCiphersRequest } from '../models/request/importCiphersRequest'; import { ImportDirectoryRequest } from '../models/request/importDirectoryRequest'; import { ImportOrganizationCiphersRequest } from '../models/request/importOrganizationCiphersRequest'; +import { KeysRequest } from '../models/request/keysRequest'; import { OrganizationCreateRequest } from '../models/request/organizationCreateRequest'; import { PasswordHintRequest } from '../models/request/passwordHintRequest'; import { PasswordRequest } from '../models/request/passwordRequest'; @@ -74,6 +75,7 @@ export abstract class ApiService { postAccountStorage: (request: StorageRequest) => Promise; postAccountPayment: (request: PaymentRequest) => Promise; postAccountLicense: (data: FormData) => Promise; + postAccountKeys: (request: KeysRequest) => Promise; postFolder: (request: FolderRequest) => Promise; putFolder: (id: string, request: FolderRequest) => Promise; deleteFolder: (id: string) => Promise; diff --git a/src/angular/components/register.component.ts b/src/angular/components/register.component.ts index 336073e50e3..a83c89f5f0c 100644 --- a/src/angular/components/register.component.ts +++ b/src/angular/components/register.component.ts @@ -3,10 +3,8 @@ import { Router } from '@angular/router'; import { ToasterService } from 'angular2-toaster'; import { Angulartics2 } from 'angulartics2'; -import { - RegisterKeysRequest, - RegisterRequest, -} from '../../models/request/registerRequest'; +import { KeysRequest } from '../../models/request/keysRequest'; +import { RegisterRequest } from '../../models/request/registerRequest'; import { ApiService } from '../../abstractions/api.service'; import { AuthService } from '../../abstractions/auth.service'; @@ -64,7 +62,7 @@ export class RegisterComponent { const keys = await this.cryptoService.makeKeyPair(encKey[0]); const request = new RegisterRequest(this.email, this.name, hashedPassword, this.hint, encKey[1].encryptedString); - request.keys = new RegisterKeysRequest(keys[0], keys[1].encryptedString); + request.keys = new KeysRequest(keys[0], keys[1].encryptedString); try { this.formPromise = this.apiService.postRegister(request); diff --git a/src/models/request/keysRequest.ts b/src/models/request/keysRequest.ts new file mode 100644 index 00000000000..6ce11e8e004 --- /dev/null +++ b/src/models/request/keysRequest.ts @@ -0,0 +1,9 @@ +export class KeysRequest { + publicKey: string; + encryptedPrivateKey: string; + + constructor(publicKey: string, encryptedPrivateKey: string) { + this.publicKey = publicKey; + this.encryptedPrivateKey = encryptedPrivateKey; + } +} diff --git a/src/models/request/registerRequest.ts b/src/models/request/registerRequest.ts index 9f375af0c11..7d3e25fc36c 100644 --- a/src/models/request/registerRequest.ts +++ b/src/models/request/registerRequest.ts @@ -1,10 +1,12 @@ +import { KeysRequest } from './keysRequest'; + export class RegisterRequest { name: string; email: string; masterPasswordHash: string; masterPasswordHint: string; key: string; - keys: RegisterKeysRequest; + keys: KeysRequest; token: string; organizationUserId: string; @@ -16,13 +18,3 @@ export class RegisterRequest { this.key = key; } } - -export class RegisterKeysRequest { - publicKey: string; - encryptedPrivateKey: string; - - constructor(publicKey: string, encryptedPrivateKey: string) { - this.publicKey = publicKey; - this.encryptedPrivateKey = encryptedPrivateKey; - } -} diff --git a/src/services/api.service.ts b/src/services/api.service.ts index 13c0a2d314f..03807490145 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -18,6 +18,7 @@ import { FolderRequest } from '../models/request/folderRequest'; import { ImportCiphersRequest } from '../models/request/importCiphersRequest'; import { ImportDirectoryRequest } from '../models/request/importDirectoryRequest'; import { ImportOrganizationCiphersRequest } from '../models/request/importOrganizationCiphersRequest'; +import { KeysRequest } from '../models/request/keysRequest'; import { OrganizationCreateRequest } from '../models/request/organizationCreateRequest'; import { PasswordHintRequest } from '../models/request/passwordHintRequest'; import { PasswordRequest } from '../models/request/passwordRequest'; @@ -218,6 +219,10 @@ export class ApiService implements ApiServiceAbstraction { return this.send('POST', '/accounts/license', data, true, false); } + postAccountKeys(request: KeysRequest): Promise { + return this.send('POST', '/accounts/keys', request, true, false); + } + // Folder APIs async postFolder(request: FolderRequest): Promise { diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 3c06bc6f4fb..3e2364acf02 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -4,6 +4,7 @@ import { AuthResult } from '../models/domain/authResult'; import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey'; import { DeviceRequest } from '../models/request/deviceRequest'; +import { KeysRequest } from '../models/request/keysRequest'; import { TokenRequest } from '../models/request/tokenRequest'; import { IdentityTokenResponse } from '../models/response/identityTokenResponse'; @@ -239,6 +240,19 @@ export class AuthService { await this.cryptoService.setKey(key); await this.cryptoService.setKeyHash(hashedPassword); await this.cryptoService.setEncKey(tokenResponse.key); + + // User doesn't have a key pair yet (old account), let's generate one for them + if (tokenResponse.privateKey == null) { + try { + const keyPair = await this.cryptoService.makeKeyPair(); + await this.apiService.postAccountKeys(new KeysRequest(keyPair[0], keyPair[1].encryptedString)); + tokenResponse.privateKey = keyPair[1].encryptedString; + } catch (e) { + // tslint:disable-next-line + console.error(e); + } + } + await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey); }