/// <summary> /// Request the backend to start verification by sending us an verification code. /// </summary> /// <param name="countryCode">Clients local calling code</param> /// <param name="phonenumber">Clients phonenumber</param> /// <returns>Rrequest accepted by server?</returns> public async Task <bool> RequestVerificationCodeAsync(string countryCode, string phonenumber) { if (string.IsNullOrWhiteSpace(countryCode) || string.IsNullOrWhiteSpace(phonenumber)) { throw new ArgumentNullException("You must provide an countrycode and phonenumber"); } using (var client = new HttpClient(new NativeMessageHandler())) { client.Timeout = _timeOut; var dto = new VerificationChallengeDto { CountryCode = countryCode, PhoneNumber = phonenumber }; var encodedContent = new FormUrlEncodedContent(dto.ToDictionary()); try { var response = await client.PostAsync(_challengeAddress, encodedContent); return(response.IsSuccessStatusCode); } catch (Exception) { return(false); } } }
public async void RequestVerification_OnSmsSendingFailure_StatusCode500() { //Arrange var form = new VerificationChallengeDto { PhoneNumber = "1730456789", CountryCode = "49" }; var userManagerMock = MockHelpers.GetMockUserManager(); var persistenceMock = new Mock <IIdentityPersistence>(); var messageSenderMock = new Mock <ISmsSender>(); messageSenderMock .Setup(ms => ms.SendMessageAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(false); var configurationMock = new Mock <IConfigurationService>(); configurationMock .SetupGet(c => c.CountriesBlackList).Returns(new int[] { }); //Act var controller = new VerificationController(_logger, persistenceMock.Object, messageSenderMock.Object, configurationMock.Object, userManagerMock.Object); var response = await controller.RequestVerification(form); //Assert Assert.IsType <StatusCodeResult>(response); }
public async void RequestVerification_OnBlackListedCountryCode_BadRequest() { //Arrange var form = new VerificationChallengeDto { CountryCode = "49", PhoneNumber = "1730456789" }; var persistenceMock = new Mock <IIdentityPersistence>(); var messageSenderMock = new Mock <ISmsSender>(); var userManagerMock = MockHelpers.GetMockUserManager(); var configurationMock = new Mock <IConfigurationService>(); configurationMock .SetupGet(c => c.CountriesBlackList).Returns(new List <int> { 1, 12, 49 }); //Act var controller = new VerificationController(_logger, persistenceMock.Object, messageSenderMock.Object, configurationMock.Object, userManagerMock.Object); var response = await controller.RequestVerification(form); //Assert Assert.IsType <BadRequestObjectResult>(response); }
public async void RequestVerification_OnValidForm_Ok() { //Arrange var form = new VerificationChallengeDto { CountryCode = "49", PhoneNumber = "1730456789" }; var userManagerMock = MockHelpers.GetMockUserManager(); var persistenceMock = new Mock <IIdentityPersistence>(); persistenceMock .Setup(r => r.VerificationTokens .FindByUserAsync(It.IsAny <string>())); persistenceMock .Setup(r => r.VerificationTokens .AddAsync(It.IsAny <VerificationToken>())) .Returns(Task.FromResult(false)); var messageSenderMock = new Mock <ISmsSender>(); messageSenderMock .Setup(ms => ms.SendMessageAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(true); var configurationMock = new Mock <IConfigurationService>(); configurationMock .SetupGet(c => c.CountriesBlackList).Returns(new int[] { }); //Act var controller = new VerificationController(_logger, persistenceMock.Object, messageSenderMock.Object, configurationMock.Object, userManagerMock.Object); var response = await controller.RequestVerification(form); //Assert Assert.IsType <OkResult>(response); }
public async Task <IActionResult> RequestVerification([FromForm] VerificationChallengeDto dto) { if (!ModelState.IsValid) { return(BadRequest()); } if (_configuration.CountriesBlackList.Contains(dto.ParseCountryCode())) { return(BadRequest($"Country [{dto.CountryCode}] is not supported.")); } var number = dto.ToString(); _logger.LogInformation($"The PhoneNumber [{number}] is requesting an verification code."); //Create request object var request = new VerificationToken(number, _configuration.VerificationDuration, CodeGenerator.GetCode()); //Send SMS message with code to client var clientMessage = $"Hello from YoApp!\nYour verification Code is:\n{request.Code}"; var sendingResult = await _messageSender.SendMessageAsync("+" + number, clientMessage); if (!sendingResult) { return(new StatusCodeResult(500)); } //Persist the request to resolve it later await _dataWorker.VerificationTokens.AddOrReplaceAsync(request); await _dataWorker.CompleteAsync(); return(Ok()); }