-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PB-27793 - As a user I should be able to transfer my account to the m…
…obile app
- Loading branch information
1 parent
7d5fc4a
commit 5bb1eaf
Showing
3 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/all/background_page/controller/crypto/getUserPrivateKeyController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
64 changes: 64 additions & 0 deletions
64
src/all/background_page/controller/crypto/getUserPrivateKeyController.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.')); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters