1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

make keypair on login if missing

This commit is contained in:
Kyle Spearrin 2018-07-03 12:06:01 -04:00
parent 3454d93fef
commit af43232567
6 changed files with 36 additions and 16 deletions

View File

@ -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<any>;
postAccountPayment: (request: PaymentRequest) => Promise<any>;
postAccountLicense: (data: FormData) => Promise<any>;
postAccountKeys: (request: KeysRequest) => Promise<any>;
postFolder: (request: FolderRequest) => Promise<FolderResponse>;
putFolder: (id: string, request: FolderRequest) => Promise<FolderResponse>;
deleteFolder: (id: string) => Promise<any>;

View File

@ -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);

View File

@ -0,0 +1,9 @@
export class KeysRequest {
publicKey: string;
encryptedPrivateKey: string;
constructor(publicKey: string, encryptedPrivateKey: string) {
this.publicKey = publicKey;
this.encryptedPrivateKey = encryptedPrivateKey;
}
}

View File

@ -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;
}
}

View File

@ -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<any> {
return this.send('POST', '/accounts/keys', request, true, false);
}
// Folder APIs
async postFolder(request: FolderRequest): Promise<FolderResponse> {

View File

@ -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);
}