private Task <InvokeResult <AuthResponse> > HandleAuthRequest(AuthRequest req) { if (req.GrantType == AuthTokenManager.GRANT_TYPE_PASSWORD) { return(_tokenManager.AccessTokenGrantAsync(req)); } else if (req.GrantType == AuthTokenManager.GRANT_TYPE_REFRESHTOKEN) { return(_tokenManager.RefreshTokenGrantAsync(req)); } else if (String.IsNullOrEmpty(req.GrantType)) { throw new Exception($"Missing Grant Type."); } else { throw new Exception($"Invalid Grant Type - [{req.GrantType}]"); } }
public async Task <InvokeResult <AuthResponse> > CreateUserAsync(RegisterUser newUser) { if (String.IsNullOrEmpty(newUser.Email)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegMissingEmail.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegMissingEmail.ToErrorMessage())); } var user = await _appUserRepo.FindByEmailAsync(newUser.Email); if (user != null) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegErrorUserExists.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegErrorUserExists.ToErrorMessage())); } /* Need to check all these, if any fail, we want to aboart, we need to refactor this into the UserAdmin module :( */ if (String.IsNullOrEmpty(newUser.AppId)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.AuthMissingAppId.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.AuthMissingAppId.ToErrorMessage())); } if (String.IsNullOrEmpty(newUser.ClientType)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.AuthMissingClientType.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.AuthMissingClientType.ToErrorMessage())); } if (String.IsNullOrEmpty(newUser.DeviceId)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.AuthMissingDeviceId.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.AuthMissingDeviceId.ToErrorMessage())); } if (String.IsNullOrEmpty(newUser.FirstName)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegMissingFirstLastName.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegMissingFirstLastName.ToErrorMessage())); } if (String.IsNullOrEmpty(newUser.LastName)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegMissingLastName.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegMissingLastName.ToErrorMessage())); } if (String.IsNullOrEmpty(newUser.Password)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegMissingPassword.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegMissingPassword.ToErrorMessage())); } var emailRegEx = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); if (!emailRegEx.Match(newUser.Email).Success) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "UserServicesController_CreateNewAsync", UserAdminErrorCodes.RegInvalidEmailAddress.Message); return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.RegInvalidEmailAddress.ToErrorMessage())); } var appUser = new AppUser(newUser.Email, $"{newUser.FirstName} {newUser.LastName}") { FirstName = newUser.FirstName, LastName = newUser.LastName, }; /* In the testing environment we just go ahead and not make the user confirm by email or phone number so we can start using * right away, eventually we will probably build this into testing but need some way of getting the confirmation tokens */ if (_appConfig.Environment == Environments.Testing) { appUser.PhoneNumberConfirmed = true; appUser.EmailConfirmed = true; } var identityResult = await _userManager.CreateAsync(appUser, newUser.Password); if (identityResult.Successful) { await LogEntityActionAsync(appUser.Id, typeof(AppUser).Name, "New User Registered", null, appUser.ToEntityHeader()); await _signInManager.SignInAsync(appUser); if (newUser.ClientType != "WEBAPP") { var authRequest = new AuthRequest() { AppId = newUser.AppId, DeviceId = newUser.DeviceId, AppInstanceId = newUser.AppInstanceId, ClientType = newUser.ClientType, GrantType = "password", Email = newUser.Email, UserName = newUser.Email, Password = newUser.Password, }; var tokenResponse = await _authTokenManager.AccessTokenGrantAsync(authRequest); if (tokenResponse.Successful) { await _userVerificationmanager.SendConfirmationEmailAsync(null, appUser.ToEntityHeader()); return(InvokeResult <AuthResponse> .Create(tokenResponse.Result)); } else { var failedValidationResult = new InvokeResult <AuthResponse>(); failedValidationResult.Concat(tokenResponse); return(failedValidationResult); } } else { await _userVerificationmanager.SendConfirmationEmailAsync(null, appUser.ToEntityHeader()); /* If we are logging in as web app, none of this applies */ return(InvokeResult <AuthResponse> .Create(new AuthResponse() { AccessToken = "N/A", AccessTokenExpiresUTC = "N/A", RefreshToken = "N/A", AppInstanceId = "N/A", RefreshTokenExpiresUTC = "N/A", IsLockedOut = false, User = appUser.ToEntityHeader(), Roles = new List <EntityHeader>() })); } } else { return(InvokeResult <AuthResponse> .FromInvokeResult(identityResult)); } }