public async Task <ActionResult> Verify([FromBody] TokenVerificationModel tokenVerification) { if (ModelState.IsValid) { var userId = User.Claims.First(c => c.Type == ClaimsHelper.UserIdProperty).Value; var user = _accountService.GetUser(int.Parse(userId)); if (user != null) { var phone = user.Phone.StartsWith('0') ? user.Phone.Remove(0, 1) : user.Phone; var validationResult = await _authy.VerifyPhoneTokenAsync( phone, "+40", tokenVerification.Token ); if (validationResult.Succeeded) { // Create the JWT security token and encode it. var jwt = new JwtSecurityToken( issuer: _jwtOptions.Issuer, audience: _jwtOptions.Audience, claims: User.Claims, notBefore: _jwtOptions.NotBefore, expires: _jwtOptions.Expiration, signingCredentials: _jwtOptions.SigningCredentials); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); return(Ok(new { access_token = encodedJwt, expires_in = (int)_jwtOptions.ValidFor.TotalSeconds, })); } else { return(Ok(validationResult)); } } else { return(BadRequest()); // NotFound(); } } else { return(BadRequest(ModelState)); } }
public async Task <JsonResult> VerifyAndCompletePhoneRegistration(TokenVerificationModel tokenVerification) { var verificationRequest = HttpContext.Session.Get <PhoneVerificationRequestModel>("phone_verification_request"); if (verificationRequest == null) { var response = new ResultModel(); response.Errors.Add(new ErrorModel(string.Empty, "A request to send the token to the phone was not made")); response.Errors.Add(new ErrorModel(string.Empty, $"To start, call: {nameof(StartRegisterBySendingVerificationRequest)}")); return(Json(response)); } if (!ModelState.IsValid) { return(JsonModelStateErrors()); } var validationResult = await _authy.VerifyPhoneTokenAsync( verificationRequest.PhoneNumber, verificationRequest.CountryCode, tokenVerification.Token ); if (!validationResult.IsSuccess) { return(Json(validationResult)); } var phone = _authy.NormalizePhoneNumber(verificationRequest.CountryCode, verificationRequest.PhoneNumber); var addNewUserRequest = await _authy.RegisterUserAsync(new RegisterViewModel { CountryCode = verificationRequest.CountryCode, PhoneNumber = verificationRequest.PhoneNumber, Password = verificationRequest.Pin, UserName = verificationRequest.PhoneNumber }); if (!addNewUserRequest.IsSuccess) { return(Json(addNewUserRequest)); } var user = new GearUser { PhoneNumber = phone, UserName = phone, IsEditable = true, PhoneNumberConfirmed = true }; var createRequest = await _userManager.CreateUserAsync(user, verificationRequest.Pin); if (!createRequest.IsSuccess) { return(!createRequest.IsSuccess ? Json(createRequest) : Json(validationResult)); } var setTokenResult = await _userManager .UserManager .SetAuthenticationTokenAsync(user, PhoneVerificationResources.LOGIN_PROVIDER_NAME, PhoneVerificationResources.AUTHY_TOKEN, addNewUserRequest.Result); if (setTokenResult.Succeeded) { return(!createRequest.IsSuccess ? Json(createRequest) : Json(validationResult)); } var tokenResponse = new ResultModel(); tokenResponse.AppendIdentityErrors(setTokenResult.Errors); return(Json(tokenResponse)); }