Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 92x 92x 92x 92x 1x 1x 1x 1x 1x 5x 3x 3x 2x 1x 4x 4x 4x 2x 2x 1x 3x 3x 1x 1x 1x 1x 8x 8x 8x 8x 8x 4x 4x 15x 15x 15x 15x 15x 3x 3x 3x 3x 3x 3x 3x 3x 18x 18x 18x 18x 18x 18x 17x 17x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 20x 20x 20x 20x 20x 24x 24x 20x 4x 20x 18x 18x 18x 18x 20x 20x 20x 20x 20x 20x 14x 14x 14x 14x 14x 14x 14x 18x 18x 7x 7x 7x 7x 7x 5x 2x 2x 9x 9x 9x 2x 2x 7x 3x 1x 1x 1x 1x 7x 7x 7x 7x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 50x 50x 50x 50x | import { KeysService } from '@affinidi/common' import { profile } from '@affinidi/tools-common' import WalletStorageService from '../services/WalletStorageService' import { SdkOptions, CognitoUserTokens, MessageParameters, KeyParams } from '../dto/shared.dto' import { withDidData } from '../shared/getDidData' import { getOptionsFromEnvironment, ParsedOptions } from '../shared/getOptionsFromEnvironment' import { ParametersValidator } from '../shared/ParametersValidator' import { randomBytes } from '../shared/randomBytes' import { validateUsername } from '@affinidi/user-management' import { StaticDependencies, ConstructorUserData, createKeyManagementService } from './BaseNetworkMember' import { LegacyNetworkMember } from './LegacyNetworkMember' import { createUserManagementService } from '../shared/createUserManagementService' import { normalizeShortPassword } from '../shared/normalizeShortPassword' import { generatePassword } from '../shared/generatePassword' type UserDataWithCognito = ConstructorUserData & { cognitoUserTokens: CognitoUserTokens | undefined } /** * @deprecated, will be removed in SDK v7 */ @profile() export class LegacyNetworkMemberWithFactories extends LegacyNetworkMember { private readonly _userManagementService protected cognitoUserTokens: CognitoUserTokens | undefined private _keysService: KeysService constructor(userData: UserDataWithCognito, dependencies: StaticDependencies, options: ParsedOptions) { super(userData, dependencies, options) this._keysService = new KeysService(userData.encryptedSeed, userData.password) this._userManagementService = createUserManagementService(options) this.cognitoUserTokens = userData.cognitoUserTokens } /** * @description Returns user's accessToken * @returns encrypted seed */ get accessToken(): string { return this.cognitoUserTokens.accessToken } /** * @description Checks if registration for the user was completed * @param username - a valid email, phone number or arbitrary username * @param inputOptions - object with environment, dev is default { env: 'dev' } * @returns `true` if user is uncofirmed in Cognito, and `false` otherwise. */ static async isUserUnconfirmed(username: string, inputOptions: SdkOptions) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) return userManagementService.doesUnconfirmedUserExist(username) } /** * @description Initilizes instance of SDK from seed * @param seedHexWithMethod - seed for derive keys in string hex format * @param inputOptions - optional parameter { registryUrl: 'https://affinity-registry.apse1.dev.affinidi.io' } * @param password - optional password, will be generated, if not provided * @returns initialized instance of SDK */ static async fromSeed( dependencies: StaticDependencies, seedHexWithMethod: string, inputOptions: SdkOptions, password: string = null, ) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: seedHexWithMethod }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: 'string', isRequired: false, value: password }, ]) let passwordBuffer if (password) { passwordBuffer = KeysService.normalizePassword(password) } else { passwordBuffer = await randomBytes(32) password = passwordBuffer.toString('hex') } const options = getOptionsFromEnvironment(inputOptions) const userData = withDidData({ encryptedSeed: await KeysService.encryptSeed(seedHexWithMethod, passwordBuffer), password, }) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens: undefined }, dependencies, options) } /** * @description Pulls encrypted seed from Affinity Guardian Wallet * 1. get AWS cognito access token * 2. using access token pull encrypted seed from Affinity Guardian Wallet * associated with this AWS Cognito userId * @param username - email or phone number used to create Affinity user * @param inputPassword - password used on create Affinity user */ async pullEncryptedSeed(username: string, inputPassword: string): Promise<string> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: 'string', isRequired: true, value: inputPassword }, ]) const password = normalizeShortPassword(inputPassword, username) const { accessToken } = await this._userManagementService.logInWithPassword(username, password) const { encryptedSeed } = await this._keyManagementService.pullKeyAndSeed(accessToken) return encryptedSeed } /** * @description Saves encrypted seed to Guardian Wallet * @param username - email/phoneNumber, registered in Cognito * @param inputPassword - password for Cognito user * @param token - Cognito access token, required for authorization. * If not provided, in order to get it, sign in to Cognito will be initiated. */ async storeEncryptedSeed(username: string, inputPassword: string, token: string = undefined): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: 'string', isRequired: true, value: inputPassword }, { isArray: false, type: 'jwt', isRequired: false, value: token }, ]) let accessToken = token /* istanbul ignore else: code simplicity */ if (!token) { const password = normalizeShortPassword(inputPassword, username) this.cognitoUserTokens = await this._userManagementService.logInWithPassword(username, password) accessToken = this.cognitoUserTokens.accessToken } const { seedHexWithMethod } = this._keysService.decryptSeed() await this._keyManagementService.pullEncryptionKeyAndStoreEncryptedSeed(accessToken, seedHexWithMethod) } /** * @description Initiates passwordless login to Affinity * @param username - email/phoneNumber, registered in Cognito * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message * @returns token */ static async passwordlessLogin( login: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<string> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) return await userManagementService.initiateLogInPasswordless(login, messageParameters) } /** * @description Completes login * @param token - received from #passwordlessLogin method * @param confirmationCode - OTP sent by AWS Cognito/SES * @param options - optional parameters for BaseNetworkMember initialization * @returns initialized instance of SDK */ static async completeLoginChallenge( dependencies: StaticDependencies, token: string, confirmationCode: string, inputOptions: SdkOptions, ) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: token }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const keyManagementService = createKeyManagementService(options) const cognitoUserTokens = await userManagementService.completeLogInPasswordless(token, confirmationCode) const userData = await keyManagementService.pullUserData(cognitoUserTokens.accessToken) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens }, dependencies, options) } /** * @description Signs out current user */ async signOut(inputOptions: SdkOptions): Promise<void> { await ParametersValidator.validate([{ isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const newTokens = await userManagementService.logOut(this.cognitoUserTokens) this.cognitoUserTokens = newTokens } /** * @description Initiates reset password flow * @param username - email/phoneNumber, registered in Cognito * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message */ static async forgotPassword( login: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) await userManagementService.initiateForgotPassword(login, messageParameters) } /** * @description Completes reset password flow * @param username - email/phoneNumber, registered in Cognito * @param confirmationCode - OTP sent by AWS Cognito/SES * @param newPassword - new password * @param options - optional parameters with specified environment */ static async forgotPasswordSubmit( login: string, confirmationCode: string, newPassword: string, inputOptions: SdkOptions, ): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, { isArray: false, type: 'string', isRequired: true, value: newPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) await userManagementService.completeForgotPassword(login, confirmationCode, newPassword) } /** * @description Logins to Affinity with login and password * @param username - email/phoneNumber, registered in Cognito * @param inputPassword - password for Cognito user * @param options - optional parameters for BaseNetworkMember initialization * @returns initialized instance of SDK */ static async fromLoginAndPassword( dependencies: StaticDependencies, username: string, inputPassword: string, inputOptions: SdkOptions, ) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: 'password', isRequired: true, value: inputPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const keyManagementService = createKeyManagementService(options) const password = normalizeShortPassword(inputPassword, username) const cognitoUserTokens = await userManagementService.logInWithPassword(username, password) const userData = await keyManagementService.pullUserData(cognitoUserTokens.accessToken) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens }, dependencies, options) } /** * @description Initiates sign up flow to Affinity wallet with already created did * @param keyParams - { ecnryptedSeed, password } - previously created keys to be storead at wallet. * @param username - arbitrary username, email or phoneNumber * @param inputPassword - is required if arbitrary username was provided. * It is optional and random one will be generated, if not provided when * email or phoneNumber was given as a username. * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message * @returns token or, in case when arbitrary username was used, it returns * initialized instance of SDK */ static async signUpWithExistsEntity( dependencies: StaticDependencies, keyParams: KeyParams, login: string, inputPassword: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<string | LegacyNetworkMemberWithFactories> { const { isUsername } = validateUsername(login) Iif (!isUsername) { return LegacyNetworkMemberWithFactories._signUpByEmailOrPhone( login, inputPassword, inputOptions, messageParameters, ) } const username = login await ParametersValidator.validate([ { isArray: false, type: KeyParams, isRequired: true, value: keyParams }, { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: 'password', isRequired: true, value: inputPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) LegacyNetworkMemberWithFactories._validateKeys(keyParams) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const password = normalizeShortPassword(inputPassword, username) const cognitoTokens = await userManagementService.signUpWithUsernameAndConfirm(username, password) return LegacyNetworkMemberWithFactories._confirmSignUp(dependencies, options, cognitoTokens, password, keyParams) } private static async _signUpByUsernameAutoConfirm( dependencies: StaticDependencies, username: string, inputPassword: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: username }, { isArray: false, type: 'password', isRequired: true, value: inputPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const password = normalizeShortPassword(inputPassword, username) const cognitoTokens = await userManagementService.signUpWithUsernameAndConfirm(username, password) const result = await LegacyNetworkMemberWithFactories._confirmSignUp( dependencies, options, cognitoTokens, password, undefined, ) result.afterConfirmSignUp() return result } private static async _signUpByEmailOrPhone( login: string, inputPassword: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<string> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: 'password', isRequired: false, value: inputPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const password = normalizeShortPassword(inputPassword || (await generatePassword()), login) return userManagementService.initiateSignUpWithEmailOrPhone(login, password, messageParameters) } /** * @description Initiates sign up flow * @param username - arbitrary username, email or phoneNumber * @param password - is required if arbitrary username was provided. * It is optional and random one will be generated, if not provided when * email or phoneNumber was given as a username. * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message * @returns token or, in case when arbitrary username was used, it returns * initialized instance of SDK */ static async signUp( dependencies: StaticDependencies, login: string, password: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<string | LegacyNetworkMemberWithFactories> { const { isUsername } = validateUsername(login) if (!isUsername) { return LegacyNetworkMemberWithFactories._signUpByEmailOrPhone(login, password, inputOptions, messageParameters) } return LegacyNetworkMemberWithFactories._signUpByUsernameAutoConfirm( dependencies, login, password, inputOptions, messageParameters, ) } /** * @description Completes sign up flow with already created did * (as result created keys will be stored at the Affinity Wallet) * NOTE: no need calling this method in case of arbitrary username was given, * as registration is already completed * NOTE: This method will throw an error if called for arbitrary username * @param keyParams - { ecnryptedSeed, password } - previously created keys to be storead at wallet. * @param token - Token returned by signUpWithExistsEntity method. * @param confirmationCode - OTP sent by AWS Cognito/SES. * @param options - optional parameters for BaseNetworkMember initialization * @returns initialized instance of SDK */ static async confirmSignUpWithExistsEntity( dependencies: StaticDependencies, keyParams: KeyParams, signUpToken: string, confirmationCode: string, inputOptions: SdkOptions, ) { ParametersValidator.validate([ { isArray: false, type: KeyParams, isRequired: true, value: keyParams }, { isArray: false, type: 'string', isRequired: true, value: signUpToken }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const { cognitoTokens, shortPassword } = await userManagementService.completeSignUpForEmailOrPhone( signUpToken, confirmationCode, ) return LegacyNetworkMemberWithFactories._confirmSignUp( dependencies, options, cognitoTokens, shortPassword, keyParams, ) } private static async _confirmSignUp( dependencies: StaticDependencies, options: ParsedOptions, cognitoUserTokens: CognitoUserTokens, shortPassword: string, keyParams: KeyParams, ) { if (!keyParams?.encryptedSeed) { const passwordHash = WalletStorageService.hashFromString(shortPassword) const registerResult = await LegacyNetworkMemberWithFactories._register(dependencies, options, passwordHash) const encryptedSeed = registerResult.encryptedSeed keyParams = { password: passwordHash, encryptedSeed } } const keyManagementService = createKeyManagementService(options) const userManagementService = createUserManagementService(options) const { encryptionKey, updatedEncryptedSeed } = await keyManagementService.reencryptSeed( cognitoUserTokens.accessToken, keyParams, !options.otherOptions.skipBackupEncryptedSeed, ) await userManagementService.markRegistrationComplete(cognitoUserTokens) const userData = withDidData({ encryptedSeed: updatedEncryptedSeed, password: encryptionKey, }) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens }, dependencies, options) } /** * @description Completes sign up flow * NOTE: no need calling this method in case of arbitrary username was given, * as registration is already completed. * NOTE: this method will throw an error if called for arbitrary username * @param confirmationCode - OTP sent by AWS Cognito/SES. * @param options - optional parameters for BaseNetworkMember initialization * @returns initialized instance of SDK */ static async confirmSignUp( dependencies: StaticDependencies, signUpToken: string, confirmationCode: string, inputOptions: SdkOptions, key?: string, passwordless?: boolean, ) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: signUpToken }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const { cognitoTokens, shortPassword } = await userManagementService.completeSignUpForEmailOrPhone( signUpToken, confirmationCode, key, passwordless, ) const result = await LegacyNetworkMemberWithFactories._confirmSignUp( dependencies, options, cognitoTokens, shortPassword, undefined, ) await result.afterConfirmSignUp() return result } private async afterConfirmSignUp() { const { idToken } = this.cognitoUserTokens Iif (this._options.otherOptions.issueSignupCredential) { const signedCredentials = await this._getSignupCredentials(idToken, this._options) await this.saveCredentials(signedCredentials) } } /** * @description Resends OTP for sign up flow * @param username - email/phoneNumber, registered and unconfirmed in Cognito * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message */ static async resendSignUpConfirmationCode( login: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) await userManagementService.resendSignUpByLogin(login, messageParameters) } /** * @description Initiates passwordless sign in of an existing user, * or signs up a new one, if user was not registered * @param username - email/phoneNumber, registered in Cognito * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message * @returns token */ static async signIn(login: string, inputOptions: SdkOptions, messageParameters?: MessageParameters): Promise<string> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: login }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) // NOTE: This is a passwordless login/sign up flow, // case when user signs up more often const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const doesConfirmedUserExist = await userManagementService.doesConfirmedUserExist(login) if (doesConfirmedUserExist) { return userManagementService.initiateLogInPasswordless(login, messageParameters) } else { const password = normalizeShortPassword(await generatePassword(), login) return userManagementService.initiateSignUpWithEmailOrPhone( login, password, messageParameters, options.accessApiKey, true, ) } } /** * @description Completes sign in * @param token - received from #signIn method * @param confirmationCode - OTP sent by AWS Cognito/SES * @param options - optional parameters for BaseNetworkMember initialization * @returns an object with a flag, identifying whether new account was created, and initialized instance of SDK */ static async confirmSignIn( dependencies: StaticDependencies, token: string, confirmationCode: string, options: SdkOptions, ): Promise<{ isNew: boolean; commonNetworkMember: LegacyNetworkMemberWithFactories }> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: token }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, { isArray: false, type: SdkOptions, isRequired: true, value: options }, ]) // NOTE: loginToken = '{"ChallengeName":"CUSTOM_CHALLENGE","Session":"...","ChallengeParameters":{"USERNAME":"...","email":"..."}}' // signUpToken = 'username::password' const isSignUpToken = token.split('::')[1] !== undefined if (isSignUpToken) { const commonNetworkMember = await LegacyNetworkMemberWithFactories.confirmSignUp( dependencies, token, confirmationCode, options, options.accessApiKey, true, ) return { isNew: true, commonNetworkMember } } const commonNetworkMember = await LegacyNetworkMemberWithFactories.completeLoginChallenge( dependencies, token, confirmationCode, options, ) return { isNew: false, commonNetworkMember } } /** * @description Initiates change user password * @param oldPassword * @param newPassword * @param options - optional parameters with specified environment */ async changePassword(oldPassword: string, newPassword: string, inputOptions: SdkOptions): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: oldPassword }, { isArray: false, type: 'string', isRequired: true, value: newPassword }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) this.cognitoUserTokens = await userManagementService.changePassword( this.cognitoUserTokens, oldPassword, newPassword, ) } /** * @description Initiates change user attribute (email/phoneNumber) flow * @param newLogin - new email/phoneNumber * @param options - optional parameters with specified environment * @param messageParameters - optional parameters with specified welcome message */ // NOTE: operation is used for change the attribute, not username. Consider renaming // New email/phoneNumber can be useded as a username to login. async changeUsername( newLogin: string, inputOptions: SdkOptions, messageParameters?: MessageParameters, ): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: newLogin }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, { isArray: false, type: MessageParameters, isRequired: false, value: messageParameters }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) this.cognitoUserTokens = await userManagementService.initiateChangeLogin( this.cognitoUserTokens, newLogin, messageParameters, ) } /** * @description Completes change user attribute (email/phoneNumber) flow * @param newUsername - new email/phoneNumber * @param confirmationCode - OTP sent by AWS Cognito/SES * @param options - optional parameters with specified environment */ async confirmChangeUsername(newLogin: string, confirmationCode: string, inputOptions: SdkOptions): Promise<void> { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: true, value: newLogin }, { isArray: false, type: 'confirmationCode', isRequired: true, value: confirmationCode }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) this.cognitoUserTokens = await userManagementService.completeChangeLogin( this.cognitoUserTokens, newLogin, confirmationCode, ) } /** * @description Creates a new instance of SDK by access token * @param accessToken * @param options - optional parameters for AffinityWallet initialization * @returns initialized instance of SDK */ static async fromAccessToken(dependencies: StaticDependencies, accessToken: string, inputOptions: SdkOptions) { await ParametersValidator.validate([ { isArray: false, type: 'string', isRequired: false, value: accessToken }, { isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }, ]) const options = getOptionsFromEnvironment(inputOptions) const keyManagementService = await createKeyManagementService(options) const userData = await keyManagementService.pullUserData(accessToken) const cognitoUserTokens = { accessToken } return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens }, dependencies, options) } /** * @description Logins with access token of Cognito user registered in Affinity * @param options - optional parameters for AffinityWallet initialization * @returns initialized instance of SDK or throws `COR-9` UnprocessableEntityError, * if user is not logged in. */ static async init(dependencies: StaticDependencies, inputOptions: SdkOptions) { await ParametersValidator.validate([{ isArray: false, type: SdkOptions, isRequired: true, value: inputOptions }]) const options = getOptionsFromEnvironment(inputOptions) const userManagementService = createUserManagementService(options) const cognitoUserTokens = userManagementService.readUserTokensFromSessionStorage() const keyManagementService = createKeyManagementService(options) const userData = await keyManagementService.pullUserData(cognitoUserTokens.accessToken) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens }, dependencies, options) } static legacyConstructor( dependencies: StaticDependencies, password: string, encryptedSeed: string, inputOptions: SdkOptions, ) { Iif (!encryptedSeed || !password) { // TODO: implement appropriate error wrapper throw new Error('`password` and `encryptedSeed` must be provided!') } const options = getOptionsFromEnvironment(inputOptions) const userData = withDidData({ encryptedSeed, password }) return new LegacyNetworkMemberWithFactories({ ...userData, cognitoUserTokens: undefined }, dependencies, options) } } |