-
Notifications
You must be signed in to change notification settings - Fork 34
[PM-31128] Add reinit_user_crypto for mobile #1148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thomas-Avery
wants to merge
6
commits into
main
Choose a base branch
from
km/pm-31128
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74929db
Add reinit_user_crypto for mobile
Thomas-Avery a300214
Fix build
Thomas-Avery 88487db
Merge branch 'main' into km/pm-31128
Thomas-Avery 07b8d1a
Cleanup
Thomas-Avery 9a65a2d
Code review updates
Thomas-Avery c1620a2
Handle state_bridge and setting v2_upgrade_token
Thomas-Avery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
503 changes: 503 additions & 0 deletions
503
crates/bitwarden-core/src/key_management/crypto/reinit_user_crypto.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
91 changes: 91 additions & 0 deletions
91
...twarden-uniffi/swift/integration-tests/Tests/IntegrationTests/ReinitUserCryptoTests.swift
This file contains hidden or 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,91 @@ | ||
| import BitwardenSdk | ||
| import XCTest | ||
|
|
||
| /// Swift integration tests for `CryptoClient.reinitUserCrypto`. | ||
| final class ReinitUserCryptoTests: XCTestCase { | ||
| var stateBridge: InMemoryStateBridge! | ||
|
|
||
| override func setUp() async throws { | ||
| try await super.setUp() | ||
| stateBridge = InMemoryStateBridge() | ||
| } | ||
|
|
||
| func testReturnsNotUnlockedWhenLocked() async throws { | ||
| // No `initializeUserCrypto` call β the user-key slot is empty. | ||
| let client = Client(tokenProvider: MockTokenProvider(), settings: nil) | ||
| client.kmStateBridge().registerBridgeImpl(bridgeImpl: stateBridge) | ||
|
|
||
| let req = ReinitUserCryptoRequest( | ||
| accountCryptographicState: makeV2AccountCryptographicState(), | ||
| upgradeToken: makeMockUpgradeToken() | ||
| ) | ||
|
|
||
| do { | ||
| try await client.crypto().reinitUserCrypto(req: req) | ||
| XCTFail("expected ReinitUserCryptoError.NotUnlocked") | ||
| } catch BitwardenError.ReinitUserCrypto(let inner) { | ||
| guard case .NotUnlocked = inner else { | ||
| XCTFail("expected .NotUnlocked, got \(inner)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testReturnsAlreadyV2WhenActiveUserIsV2() async throws { | ||
| let client = try await makeV2InitializedClient(stateBridge: stateBridge) | ||
|
|
||
| let req = ReinitUserCryptoRequest( | ||
| accountCryptographicState: makeV2AccountCryptographicState(), | ||
| upgradeToken: makeMockUpgradeToken() | ||
| ) | ||
|
|
||
| do { | ||
| try await client.crypto().reinitUserCrypto(req: req) | ||
| XCTFail("expected ReinitUserCryptoError.AlreadyV2Encryption") | ||
| } catch BitwardenError.ReinitUserCrypto(let inner) { | ||
| guard case .AlreadyV2Encryption = inner else { | ||
| XCTFail("expected .AlreadyV2Encryption, got \(inner)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testUpgradesV1ToV2WithValidToken() async throws { | ||
| let client = try await makeV1InitializedClient(stateBridge: stateBridge) | ||
|
|
||
| let upgradeToken = makeValidUpgradeToken() | ||
| await stateBridge.setV2UpgradeToken(value: upgradeToken) | ||
|
|
||
| let req = ReinitUserCryptoRequest( | ||
| accountCryptographicState: makeV2AccountCryptographicState(), | ||
| upgradeToken: upgradeToken | ||
| ) | ||
|
|
||
| try await client.crypto().reinitUserCrypto(req: req) | ||
|
|
||
| // After a successful V1βV2 reinit, the active user key in the slot | ||
| // must be the V2 test-vector key (returned base64-encoded by | ||
| // `getUserEncryptionKey` for V2 keys via COSE serialization). | ||
| let userKey = try await client.crypto().getUserEncryptionKey() | ||
| XCTAssertEqual(userKey, TEST_VECTOR_USER_KEY_V2_B64) | ||
| } | ||
|
|
||
| func testInvalidUpgradeTokenReturnsError() async throws { | ||
| let client = try await makeV1InitializedClient(stateBridge: stateBridge) | ||
|
|
||
| let req = ReinitUserCryptoRequest( | ||
| accountCryptographicState: makeV2AccountCryptographicState(), | ||
| upgradeToken: makeMockUpgradeToken() | ||
| ) | ||
|
|
||
| do { | ||
| try await client.crypto().reinitUserCrypto(req: req) | ||
| XCTFail("expected ReinitUserCryptoError.InvalidUpgradeToken") | ||
| } catch BitwardenError.ReinitUserCrypto(let inner) { | ||
| guard case .InvalidUpgradeToken = inner else { | ||
| XCTFail("expected .InvalidUpgradeToken, got \(inner)") | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving this check upstream seem like the best approach. Open to other suggestions.