Skip to content

Commit

Permalink
PB-27793 - As a user I should be able to transfer my account to the m…
Browse files Browse the repository at this point in the history
…obile app
  • Loading branch information
Gamabunta57 committed Sep 21, 2023
1 parent 7d5fc4a commit 5bb1eaf
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 4.3.0
*/

import Keyring from "../../model/keyring";
import i18n from "../../sdk/i18n";
import GpgKeyError from "../../error/GpgKeyError";
import GetPassphraseService from "../../service/passphrase/getPassphraseService";

class GetUserPrivateKeyController {
/**
* GetUserPrivateKeyController constructor
* @param {Worker} worker
* @param {string} requestId uuid
*/
constructor(worker, requestId, account) {
this.worker = worker;
this.requestId = requestId;
this.keyring = new Keyring();
this.getPassphraseService = new GetPassphraseService(account);
}

/**
* Wrapper of exec function to run it with worker.
*
* @returns {Promise<void>}
*/
async _exec() {
try {
const armoredEncryptedPrivateKey = await this.exec();
this.worker.port.emit(this.requestId, "SUCCESS", armoredEncryptedPrivateKey);
} catch (error) {
console.error(error);
this.worker.port.emit(this.requestId, 'ERROR', error);
}
}
/**
* Returns the current user's armored private key after requesting its passphrase.
*
* @returns {Promise<string>}
*/
async exec() {
await this.getPassphraseService.requestPassphrase(this.worker);
const keyring = new Keyring();
const privateKeyInfo = keyring.findPrivate();
if (!privateKeyInfo) {
throw new GpgKeyError(i18n.t('Private key not found.'));
}
return privateKeyInfo.armoredKey;
}
}

export default GetUserPrivateKeyController;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 4.3.0
*/
import {enableFetchMocks} from "jest-fetch-mock";
import {pgpKeys} from "../../../../../test/fixtures/pgpKeys/keys";
import MockExtension from "../../../../../test/mocks/mockExtension";
import Keyring from '../../model/keyring';
import GetGpgKeyInfoService from "../../service/crypto/getGpgKeyInfoService";
import {v4 as uuidv4} from "uuid";
import {OpenpgpAssertion} from "../../utils/openpgp/openpgpAssertions";
import GetUserPrivateKeyController from "./getUserPrivateKeyController";
import GpgKeyError from "../../error/GpgKeyError";
import AccountEntity from "../../model/entity/account/accountEntity";
import {adminAccountDto} from "../../model/entity/account/accountEntity.test.data";

const keyring = new Keyring();
jest.mock("../../service/passphrase/getPassphraseService");

beforeAll(() => {
enableFetchMocks();
});

describe("GetUserPrivateKeyController", () => {
const account = new AccountEntity(adminAccountDto());
it(`Should return the user's armored encrypted private key`, async() => {
expect.assertions(1);
const userId = uuidv4();
await MockExtension.withConfiguredAccount();
await keyring.importPrivate(pgpKeys.ada.private, userId);

const controller = new GetUserPrivateKeyController(null, null, account);
controller.getPassphraseService.requestPassphrase.mockResolvedValue(pgpKeys.ada.passphrase);
const armoredPrivateKey = await controller.exec();

const adaPrivateKey = await OpenpgpAssertion.readKeyOrFail(pgpKeys.ada.private);
const adaKeyInfo = await GetGpgKeyInfoService.getKeyInfo(adaPrivateKey);

const returnedKey = await OpenpgpAssertion.readKeyOrFail(armoredPrivateKey);
const returnedKeyInfo = await GetGpgKeyInfoService.getKeyInfo(returnedKey);

expect(returnedKeyInfo.toDto()).toStrictEqual(adaKeyInfo.toDto());
});

it(`Should throw an exception if the private key does not exist`, async() => {
expect.assertions(1);
const controller = new GetUserPrivateKeyController(null, null, account);

try {
await controller.exec();
} catch (error) {
expect(error).toStrictEqual(new GpgKeyError('Private key not found.'));
}
});
});
17 changes: 3 additions & 14 deletions src/all/background_page/event/keyringEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* @copyright (c) 2019 Passbolt SA
* @licence GNU Affero General Public License http://www.gnu.org/licenses/agpl-3.0.en.html
*/
import Keyring from "../model/keyring";
import GetPassphraseService from "../service/passphrase/getPassphraseService";
import CheckPassphraseController from "../controller/crypto/checkPassphraseController";
import GetUserKeyInfoController from "../controller/crypto/getUserKeyInfoController";
import GetKeyInfoController from "../controller/crypto/getKeyInfoController";
import DownloadUserPublicKeyController from "../controller/crypto/downloadUserPublicKeyController";
import DownloadUserPrivateKeyController from "../controller/crypto/downloadUserPrivateKeyController";
import GetUserPrivateKeyController from "../controller/crypto/getUserPrivateKeyController";

const listen = function(worker, _, account) {
/*
Expand Down Expand Up @@ -97,18 +96,8 @@ const listen = function(worker, _, account) {
* @param requestId {uuid} The request identifier
*/
worker.port.on('passbolt.keyring.get-private-key', async requestId => {
try {
const getPassphraseService = new GetPassphraseService(account);
await getPassphraseService.request(worker);
const keyring = new Keyring();
const privateKeyInfo = keyring.findPrivate();
if (!privateKeyInfo) {
throw new Error('Private key not found.');
}
worker.port.emit(requestId, 'SUCCESS', privateKeyInfo.armoredKey);
} catch (error) {
worker.port.emit(requestId, 'ERROR', error);
}
const controller = new GetUserPrivateKeyController(worker, requestId, account);
await controller._exec();
});
};
export const KeyringEvents = {listen};
Expand Down

0 comments on commit 5bb1eaf

Please sign in to comment.