public User CreateNewUser(User user) { if (user.Email == null || UsersRepository.GetUserByEmail(user.Email) != null) { throw new InvalidEmailException(user.Email); } user.UserId = null; user.UserPassword = Hashing.HashPassword(user.UserPassword); return(UsersRepository.CreateNewUser(user)); }
protected async Task LoginOffline() { // grab the user from local resource Dsr = await LoginController.GetByDsrPhoneNumberAsync(Settings.DsrPhone); // no local profile present if (Dsr == null) { ShowLoginResultMessage(Resource.String.no_offline_profile); this._canLoginOffline = false; return; } // profile exists, continue // check whether it has been too long since the user logged in online if (!LoginService.LoginValid(Dsr.LastOnlineLogin, DateTime.Today, Settings.ExpirePeriodInDays)) { ShowLoginResultMessage(string.Format(GetString(Resource.String.logging_in_too_long), Settings.ExpirePeriodInDays)); this._canLoginOffline = false; return; } // check the amount of times logged in offline if (Dsr.OfflineLoginAttempts >= Settings.OfflineLoginAttempts) { ShowLoginResultMessage(string.Format(GetString(Resource.String.logging_in_offline_expire), Dsr.OfflineLoginAttempts)); this._canLoginOffline = false; return; } // we're still ok, check the hash IHashing hashing = Resolver.Instance.Get <IHashing>(); string hash = hashing.HashPassword(Settings.DsrPhone, EnteredPin); if (hash != Dsr.PinHash) { Dsr.OfflineLoginAttempts++; await LoginController.SaveAsync(Dsr); ShowLoginResultMessage(Resource.String.wrong_pin); return; } // seem to be right PIN, so continue Dsr.LastOfflineLogin = DateTime.Now; Dsr.OfflineLoginAttempts = 0; await LoginController.SaveAsync(Dsr); ContinueToWelcome(); }
/// <summary> /// Attempts to perform an online login /// </summary> /// <param name="phone">The phone number of the individual logging in</param> /// <param name="pin">The associated pin</param> /// <param name="isFirstTime">Flag for whether it is the first time the individual is log</param> /// <param name="filterFlags">Flags to help ignore some API errors</param> /// <returns>Null if login failed or on success a DsrProfile object containing details of user who's logged in</returns> public async Task <LoginResponse> Login(string phone, string pin, bool isFirstTime, ErrorFilterFlags filterFlags) { try { if (pin == null) { return(new LoginResponse { Code = LoginResponseCode.WrongParameters }); } if (phone == null) { return(new LoginResponse { Code = LoginResponseCode.WrongParameters }); } IHashing hashing = Resolver.Instance.Get <IHashing>(); string hash = hashing.HashPassword(phone, pin); string credentials = string.Format("{0}:{1}", phone, hash); byte[] bytes = hashing.GetBytes(credentials); string base64 = Convert.ToBase64String(bytes); this.RemoveHeader("Authorization"); this.AddHeader("Authorization", " Basic " + base64); ServerResponse <LoginResponse> response = await PostObjectAsync <LoginResponse, LoginDto>( new LoginDto { Hash = hash, IsFirstLogin = isFirstTime, DeviceInformation = Resolver.Instance.Get <IInformation>() }, null, filterFlags); this.Logger.Debug("Call to login api completed"); if (response == null) { this.Logger.Debug("Response is null"); return(new LoginResponse() { Code = LoginResponseCode.HttpError }); } if (response.StatusCode == HttpStatusCode.Unauthorized) { this.Logger.Debug("HttpStatusCode.Unauthorized"); return(new LoginResponse() { Code = LoginResponseCode.Unauthorized }); } if (!response.IsSuccessStatus) { this.Logger.Debug("IsSuccessStatus = false"); return(new LoginResponse() { Code = LoginResponseCode.HttpError }); } this.Logger.Debug("Persisting user hash"); Resolver.Instance.Get <ISalesAppSession>().UserHash = base64; this.Logger.Debug("deserializing response text to object"); LoginResponse loginResponse = response.GetObject(); if (loginResponse.Permissions == null || !loginResponse.Permissions.Any()) { this.Logger.Debug("Looks like we don't yet support permissions. Lets fake some."); var vals = Enum.GetNames(typeof(Permissions)); loginResponse.Permissions = new List <Permission>(); foreach (string value in vals) { this.Logger.Debug(string.Format("Faking permission: {0}", value)); loginResponse.Permissions.Add( new Permission { Name = value, PermissionId = (uint)Enum.Parse(typeof(Permissions), value) }); } } this.Logger.Debug(string.Format("Updating permissions total permissions count {0}", loginResponse.Permissions.Count)); await PermissionsController.Instance.UpdatePermissionsAsync(loginResponse.Permissions); this.Logger.Debug("Login went smoothly... Exiting method and returning result"); loginResponse.Code = LoginResponseCode.Success; return(loginResponse); } catch (Exception ex) { this.Logger.Error(ex); return(new LoginResponse() { Code = LoginResponseCode.Unknown }); } }