-
Notifications
You must be signed in to change notification settings - Fork 308
feat(sdk-api,sdk-core): add password rotation progress callback with truthful keychain total #9825
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
noegutierrez5
wants to merge
6
commits into
master
Choose a base branch
from
noegutierrez/wcn-2084-add-progress-indication-for-login-password-rotation
base: master
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
67e0e3a
feat(sdk-api,sdk-core): add progress callback for password rotation
noegutierrez5 f2a647a
feat(sdk-api,sdk-core): thread truthful keychain total into rotation …
noegutierrez5 f3bce88
fix(sdk-api): cover both keychain sets in the rotation total
noegutierrez5 245aa94
fix(sdk-api): emit terminal total for v1-only accounts
noegutierrez5 2c456e9
feat(sdk-core): report an outcome for every listed keychain (option A)
noegutierrez5 f7ee764
test(sdk-api): fix stale encryptedTotalCount mention in mixed-account…
noegutierrez5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ import { | |
| GetUserOptions, | ||
| ListWebhookNotificationsOptions, | ||
| LoginResponse, | ||
| PasswordRotationProgress, | ||
| PingOptions, | ||
| ProcessedAuthenticationOptions, | ||
| ReconstitutedSecret, | ||
|
|
@@ -2010,7 +2011,12 @@ export class BitGoAPI implements BitGoBase { | |
| * @param oldPassword {String} - the current password | ||
| * @param newPassword {String} - the new password | ||
| */ | ||
| async changePassword({ oldPassword, newPassword, encryptionVersion }: ChangePasswordOptions): Promise<any> { | ||
| async changePassword({ | ||
| oldPassword, | ||
| newPassword, | ||
| encryptionVersion, | ||
| progressCallback, | ||
|
Comment on lines
+2015
to
+2018
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also params need to be documented in the jsDocs. |
||
| }: ChangePasswordOptions): Promise<any> { | ||
| if (!_.isString(oldPassword)) { | ||
| throw new Error('expected string oldPassword'); | ||
| } | ||
|
|
@@ -2029,6 +2035,58 @@ export class BitGoAPI implements BitGoBase { | |
| throw new Error('the provided oldPassword is incorrect'); | ||
| } | ||
|
|
||
| const emitProgress = (progress: PasswordRotationProgress) => { | ||
| if (!_.isFunction(progressCallback)) { | ||
| return; | ||
| } | ||
| try { | ||
| progressCallback(progress); | ||
| } catch (e) { | ||
| // ignore observer exceptions so a throwing callback never affects rotation results | ||
| } | ||
| }; | ||
|
|
||
| // Single counter set shared by both the v1 and v2 lower-level keychain callbacks below. | ||
| const counters = { attempted: 0, completed: 0, succeeded: 0, skipped: 0 }; | ||
| // The v1 set has no server-provided count; its size (the /user/encrypted map) is | ||
| // known only once v1 processing returns. Captured so v2 events can add it to the | ||
| // backend-served totalCount — without this, a mixed account finishes at | ||
| // `completed > total` (2 v1 + 32 v2 keychains read "34 of 32"). | ||
| let v1KeychainCount = 0; | ||
| // A v1-only account emits no v2 event at all, so nothing would ever carry a total; | ||
| // tracked so the terminal denominator below can fire for those accounts. | ||
| let v2EventCount = 0; | ||
| emitProgress({ phase: 'keychains', status: 'started', ...counters }); | ||
|
|
||
| const makeKeychainProgressCallback = | ||
| (keychainVersion: 'v1' | 'v2') => | ||
| (progress: { status: 'updated' | 'skipped'; currentKeychainId?: string; total?: number }) => { | ||
| counters.attempted++; | ||
| counters.completed++; | ||
| if (keychainVersion === 'v2') { | ||
| v2EventCount++; | ||
| } | ||
| if (progress.status === 'updated') { | ||
| counters.succeeded++; | ||
| } else { | ||
| counters.skipped++; | ||
| } | ||
| emitProgress({ | ||
| phase: 'keychains', | ||
| status: 'updated', | ||
| ...counters, | ||
| // v1 events stay indeterminate: the v1 set has no server count, and surfacing | ||
| // its client-side size mid-flight would make the denominator jump the moment | ||
| // v2's totalCount arrives. v2 events add the v1 set size so the | ||
| // counter reaches the denominator exactly on mixed v1/v2 accounts. When the | ||
| // backend supplies no count, the total stays undefined (never fabricated). | ||
| total: | ||
| keychainVersion === 'v2' && progress.total !== undefined ? v1KeychainCount + progress.total : undefined, | ||
| currentKeychainId: progress.currentKeychainId, | ||
| keychainVersion, | ||
| }); | ||
| }; | ||
|
|
||
| // it doesn't matter which coin we choose because the v2 updatePassword functions updates all v2 keychains | ||
| // we just need to choose a coin that exists in the current environment | ||
| const coin = common.Environments[this.getEnv()].network === 'bitcoin' ? 'btc' : 'tbtc'; | ||
|
|
@@ -2038,14 +2096,36 @@ export class BitGoAPI implements BitGoBase { | |
| const encryptionSession = | ||
| encryptionVersion === 2 ? await this.createEncryptionSession(newPassword, encryptionVersion) : undefined; | ||
| try { | ||
| const updateKeychainPasswordParams = { | ||
| const v1KeychainUpdatePWResult = await this.keychains().updatePassword({ | ||
| oldPassword, | ||
| newPassword, | ||
| encryptionVersion, | ||
| encryptionSession, | ||
| }; | ||
| const v1KeychainUpdatePWResult = await this.keychains().updatePassword(updateKeychainPasswordParams); | ||
| const v2Keychains = await this.coin(coin).keychains().updatePassword(updateKeychainPasswordParams); | ||
| progressCallback: makeKeychainProgressCallback('v1'), | ||
| }); | ||
| // Every v1 entry emits exactly one progress event, so the returned map's size is | ||
| // the v1 contribution to the denominator; v2 events below add it to their total. | ||
| v1KeychainCount = Object.keys(v1KeychainUpdatePWResult.keychains).length; | ||
| const v2Keychains = await this.coin(coin) | ||
| .keychains() | ||
| .updatePassword({ | ||
| oldPassword, | ||
| newPassword, | ||
| encryptionVersion, | ||
| encryptionSession, | ||
| progressCallback: makeKeychainProgressCallback('v2'), | ||
| }); | ||
|
|
||
| // A v1-only account emits no v2 event, so its total would stay undefined for the | ||
| // whole rotation and the observer could never render "n of n". Emit the client-known | ||
| // v1 set size as the terminal denominator — guarded on zero v2 events so mixed | ||
| // accounts keep the single stable total from totalCount instead of one | ||
| // that jumps mid-rotation. | ||
| if (v2EventCount === 0 && v1KeychainCount > 0) { | ||
| emitProgress({ phase: 'keychains', status: 'updated', ...counters, total: v1KeychainCount }); | ||
| } | ||
|
|
||
| emitProgress({ phase: 'finalizing', status: 'started' }); | ||
|
|
||
| const [hmacOldPassword, hmacNewPassword] = await Promise.all([ | ||
| this._hmacAuthStrategy.calculateHMAC(user.username, oldPassword), | ||
|
|
@@ -2065,6 +2145,7 @@ export class BitGoAPI implements BitGoBase { | |
| const payloadSizeKB = Math.ceil(payloadSizeBytes / 1024); | ||
|
|
||
| // Check if batching flow is enabled | ||
| let useBatchingFlow = false; | ||
| try { | ||
| const batchingFlowCheck = await this.get(this.url('/user/checkBatchingPasswordFlow', 2)) | ||
| .query({ payloadSize: payloadSizeKB.toString() }) | ||
|
|
@@ -2077,20 +2158,30 @@ export class BitGoAPI implements BitGoBase { | |
| batchingFlowCheck.maxBatchSizeKB, | ||
| 3 | ||
| ); | ||
| // Call changepassword API without keychains for batching flow | ||
| return this.post(this.url('/user/changepassword')) | ||
| .send({ | ||
| version: updatePasswordParams.version, | ||
| oldPassword: updatePasswordParams.oldPassword, | ||
| password: updatePasswordParams.password, | ||
| }) | ||
| .result(); | ||
| useBatchingFlow = true; | ||
| } | ||
| } catch (error) { | ||
| // batching flow check failed | ||
| } | ||
|
|
||
| return this.post(this.url('/user/changepassword')).send(updatePasswordParams).result(); | ||
| if (useBatchingFlow) { | ||
| // Call changepassword API without keychains for batching flow. Awaited outside the | ||
| // check/upload try-catch above, so a rejection here still propagates to the caller | ||
| // with no legacy-POST fallback, exactly as before. | ||
| const result = await this.post(this.url('/user/changepassword')) | ||
| .send({ | ||
| version: updatePasswordParams.version, | ||
| oldPassword: updatePasswordParams.oldPassword, | ||
| password: updatePasswordParams.password, | ||
| }) | ||
| .result(); | ||
| emitProgress({ phase: 'finalizing', status: 'completed' }); | ||
| return result; | ||
| } | ||
|
|
||
| const result = await this.post(this.url('/user/changepassword')).send(updatePasswordParams).result(); | ||
| emitProgress({ phase: 'finalizing', status: 'completed' }); | ||
| return result; | ||
| } finally { | ||
| encryptionSession?.destroy(); | ||
| } | ||
|
|
||
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.
maybe something like this? The
onErrorhook lets callers decide to handle cases. This can be called on the catch block