Example #1
0
        public async Task <OtpCheckOperationResult> OtpCheckAsync(string account, OtpCheckDescription description, string requestId, CancellationToken cancellationToken)
        {
            OtpCheckOperationResult result = null;
            var subscriptionId             = await RequestHelper.GetSubscriptionId(account);

            try
            {
                var otpcode = await this.otpStore.QueryOtpCodeAsync(account, description.PhoneNumber);

                if (otpcode == null)
                {
                    result = new OtpCheckOperationResult(OtpOperationStatus.WRONG_CODE);
                    return(result);
                }

                // delete expired code in otp store
                if (otpcode.ExpiredTime < DateTime.UtcNow)
                {
                    await this.otpStore.DeleteOtpCodeAsync(account, description.PhoneNumber);

                    await this.otpStorage.CreateOtpCodeHistoryRecord(account, description.PhoneNumber, ActionType.ExpireDelete.ToString(), DateTime.UtcNow);

                    result = new OtpCheckOperationResult(OtpOperationStatus.CODE_EXPIRED);
                    return(result);
                }

                if (otpcode.Code != description.Code)
                {
                    result = new OtpCheckOperationResult(OtpOperationStatus.WRONG_CODE);
                    return(result);
                }

                // Create otp check history in otp storage table
                await this.otpStorage.CreateOtpCodeHistoryRecord(account, description.PhoneNumber, ActionType.CheckDelete.ToString(), DateTime.UtcNow);

                // delete code in otp store if check is success
                await this.otpStore.DeleteOtpCodeAsync(account, description.PhoneNumber);

                result = new OtpCheckOperationResult(OtpOperationStatus.SUCCESS);
                this.metricManager.LogOtpCheckSuccess(1, account, subscriptionId, string.Empty);
                OtpProviderEventSource.Current.Info(requestId, this, nameof(this.OtpCheckAsync), OperationStates.Succeeded, $"account: {account}, phoneNumber: {description.PhoneNumber}");

                return(result);
            }
            catch (Exception ex)
            {
                this.metricManager.LogOtpCheckFailed(1, account, subscriptionId, string.Empty);
                OtpProviderEventSource.Current.ErrorException(requestId, this, nameof(this.OtpCheckAsync), OperationStates.Failed, $"Failed to check OTP code for account: {account}, phoneNumber: {description.PhoneNumber}", ex);
                throw new Exception(string.Format($"Failed to check OTP code for account: {account}, phoneNumber: {description.PhoneNumber}"));
            }
        }
        public async Task <ServiceProviderResponse> CheckOtpAsync(
            [FromHeader(Constant.OperationTrackingIdHeader)] string requestId,
            [FromHeader] string account,
            [FromBody] OtpCheckDescription description)
        {
            Validator.ArgumentNotNull(account, nameof(account));
            Validator.ArgumentNotNull(description, nameof(description));
            Validator.ArgumentNotNullOrEmpty(description.PhoneNumber, nameof(description.PhoneNumber));
            Validator.ArgumentNotNullOrEmpty(description.Code, nameof(description.Code));

            var result = await this.engine.OtpCheckAsync(account, description, requestId, CancellationToken.None);

            return(new ServiceProviderResponse
            {
                StatusCode = HttpStatusCode.OK,
                JsonContent = result
            });
        }