public async Task When_Null_Parameter_Is_Passed_Then_Exception_Is_Thrown()
 {
     await Assert
     .ThrowsAsync <NullReferenceException>(
         () => _smsAuthenticationOperation.Execute(null, CancellationToken.None))
     .ConfigureAwait(false);
 }
Exemple #2
0
        public async Task <IActionResult> Send(
            [FromBody] ConfirmationCodeRequest confirmationCodeRequest,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(confirmationCodeRequest.PhoneNumber))
            {
                return(BuildError(
                           ErrorCodes.InvalidRequest,
                           "parameter phone_number is missing",
                           HttpStatusCode.BadRequest));
            }

            try
            {
                var option = await _smsAuthenticationOperation.Execute(confirmationCodeRequest.PhoneNumber, cancellationToken)
                             .ConfigureAwait(false);

                if (option is Option <ResourceOwner> .Error e)
                {
                    return(new ObjectResult(e.Details)
                    {
                        StatusCode = (int)e.Details.Status
                    });
                }
                return(new OkResult());
            }
            catch (Exception)
            {
                return(BuildError(
                           ErrorCodes.UnhandledExceptionCode,
                           "unhandled exception occurred please contact the administrator",
                           HttpStatusCode.InternalServerError));
            }
        }
        public async Task <IActionResult> LocalLogin(
            SmsAuthenticationViewModel localAuthenticationViewModel,
            CancellationToken cancellationToken)
        {
            var authenticatedUser = await SetUser().ConfigureAwait(false);

            if (authenticatedUser?.Identity != null && authenticatedUser.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "User", new { Area = "pwd" }));
            }

            if (localAuthenticationViewModel == null)
            {
                throw new ArgumentNullException(nameof(localAuthenticationViewModel));
            }

            if (ModelState.IsValid && !string.IsNullOrWhiteSpace(localAuthenticationViewModel.PhoneNumber))
            {
                ResourceOwner?resourceOwner = null;
                var           option        = await _smsAuthenticationOperation
                                              .Execute(localAuthenticationViewModel.PhoneNumber, cancellationToken)
                                              .ConfigureAwait(false);

                if (option is Option <ResourceOwner> .Error e)
                {
                    await _eventPublisher.Publish(
                        new SimpleAuthError(
                            Id.Create(),
                            e.Details.Title,
                            e.Details.Detail,
                            string.Empty,
                            DateTimeOffset.UtcNow))
                    .ConfigureAwait(false);

                    ModelState.AddModelError("message_error", e.Details.Detail);
                }
                else
                {
                    resourceOwner = ((Option <ResourceOwner> .Result)option).Item;
                }

                if (resourceOwner != null)
                {
                    resourceOwner.Claims = resourceOwner.Claims.Add(
                        new Claim(
                            ClaimTypes.AuthenticationInstant,
                            DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture),
                            ClaimValueTypes.Integer));
                    await SetPasswordLessCookie(resourceOwner.Claims).ConfigureAwait(false);

                    try
                    {
                        return(RedirectToAction("ConfirmCode"));
                    }
                    catch (Exception ex)
                    {
                        await _eventPublisher.Publish(
                            new SimpleAuthError(
                                Id.Create(),
                                ex.Message,
                                ex.Message,
                                string.Empty,
                                DateTimeOffset.UtcNow))
                        .ConfigureAwait(false);

                        ModelState.AddModelError("message_error", "SMS account is not valid");
                    }
                }
            }

            var viewModel = new AuthorizeViewModel();

            await SetIdProviders(viewModel).ConfigureAwait(false);

            RouteData.Values["view"] = "Index";
            return(Ok(viewModel));
        }