コード例 #1
0
        public virtual async Task <AuthEventEnum> VerifyCodeAsync(string code)
        {
            if (CurrentAuthProcess == AuthProcessEnum.None)
            {
                return(AuthEventEnum.Alert_NoActiveAuthProcess);
            }

            if (CurrentChallenge != AuthChallengeEnum.Code)
            {
                return(AuthEventEnum.Alert_VerifyCalledButNoChallengeFound);
            }

            try
            {
                switch (CurrentAuthProcess)
                {
                case AuthProcessEnum.None:
                    return(AuthEventEnum.Alert_InternalProcessError);

                case AuthProcessEnum.ResettingPassword:
                    await CognitoUser.ConfirmForgotPasswordAsync(code, newPassword).ConfigureAwait(false);

                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.SigningUp:
                    var result = await providerClient.ConfirmSignUpAsync(
                        new ConfirmSignUpRequest
                    {
                        ClientId         = clientId,
                        Username         = login,
                        ConfirmationCode = code
                    }).ConfigureAwait(false);

                    IsCodeVerified = true;
                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.SigningIn:
                    if (authFlowResponse == null)     // authFlowResponse set during VerifyPassword
                    {
                        return(AuthEventEnum.Alert_InternalSignInError);
                    }

                    authFlowResponse = await CognitoUser.RespondToSmsMfaAuthAsync(
                        new RespondToSmsMfaRequest()
                    {
                        SessionID = authFlowResponse.SessionID,
                        MfaCode   = code
                    }
                        ).ConfigureAwait(false);

                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.UpdatingEmail:
                    await CognitoUser.VerifyAttributeAsync("email", code).ConfigureAwait(false);

                    IsCodeVerified = true;
                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.UpdatingPhone:
                    return(AuthEventEnum.Alert_InternalProcessError);

                default:
                    return(AuthEventEnum.Alert_InternalProcessError);
                }
            }
            catch (InvalidPasswordException) { return(AuthEventEnum.Alert_PasswordFormatRequirementsFailed); }
            catch (TooManyRequestsException) { return(AuthEventEnum.Alert_TooManyAttempts); }
            catch (TooManyFailedAttemptsException) { return(AuthEventEnum.Alert_TooManyAttempts); }
            catch (NotAuthorizedException) { return(AuthEventEnum.Alert_NotAuthorized); }
            catch (UserNotFoundException) { return(AuthEventEnum.Alert_UserNotFound); }
            catch (UserNotConfirmedException) { return(AuthEventEnum.Alert_NotConfirmed); }
            catch (CodeMismatchException) { return(AuthEventEnum.Alert_VerifyFailed); }
            catch (AliasExistsException) { return(AuthEventEnum.Alert_AccountWithThatEmailAlreadyExists); }
            catch (Exception e)
            {
                Debug.WriteLine($"VerifyCode() threw an exception {e}");
                CognitoUser = null;
                return(AuthEventEnum.Alert_Unknown);
            }
        }