예제 #1
0
        /// <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
                });
            }
        }