Ejemplo n.º 1
0
        public async Task <StateRequest> Login(string email, string password)
        {
            var state = new StateRequest();

            try
            {
                using (var client = new HttpClient(new NativeMessageHandler()))
                {
                    var url     = String.Format(AppSettings.MicrosoftAuthEndpoint, AppSettings.MicrosoftTenant);
                    var content = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        { "grant_type", "password" },
                        { "client_id", AppSettings.MicrosoftApiClientId },
                        { "resource", AppSettings.MicrosoftResource },
                        { "username", email },
                        { "password", password },
                        { "redirect_uri", "urn:ietf:wg:oauth:2.0:oob" }
                    });
                    var response = await client.PostAsync(url, content);

                    var json = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        var tokenResponse = JsonConvert.DeserializeObject <AuthenticatedUserResponse>(json);
                        AppSettings.AuthenticatedUserResponse = tokenResponse;

                        var user = new User(email, password);
                        await _cacheEntity.InsertObjectAsync(AppSettings.IdAppUserCache, user);

                        await _cacheEntity.InsertObjectAsync(AppSettings.IdAppCache, true, DateTimeOffset.Now.AddSeconds(Double.Parse(AppSettings.AuthenticatedUserResponse.ExpiresIn)));
                    }
                    else
                    {
                        state.Message = "Error : User or password invalid.";
                    }

                    state.Success = response.IsSuccessStatusCode;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Error with Token authentication: {ex}");
                throw new ServiceAuthenticationException();
            }


            return(state);
        }
Ejemplo n.º 2
0
        public async Task <Review> GetReview(int year, int month)
        {
            try
            {
                Review review = await _cacheService.GetObjectAsync <Review>($"/{year}/{month}/review");

                if (review == null)
                {
                    var nowLess3Month = DateTime.Now.AddMonths(-3);
                    var requestDate   = new DateTime(year, month, 1);

                    var url = $"{AppSettings.TimesheetUrlEndPoint}/{year}/{month}/review";

                    review = await Policy.Handle <Exception>()
                             .WaitAndRetryAsync(1, retryAtemp => TimeSpan.FromMilliseconds(100), async(exception, timeSpan, retryCount, context) => { await _authenticationService.UserIsAuthenticatedAndValidAsync(true); })
                             .ExecuteAsync <Review>(async() => {
                        return(await _requestService.GetAsync <Review>(url, AppSettings.AuthenticatedUserResponse.AccessToken));
                    });

                    if (requestDate < nowLess3Month)
                    {
                        await _cacheService.InsertObjectAsync($"/{year}/{month}/review", review);
                    }
                }


                return(review);
            }
            catch (Exception e)
            {
                MessagingCenter.Send <ISubscribeMessagingCenter>(this, nameof(UnauthorizedAccessException));
                Crashes.TrackError(e);
                throw e;
            }
        }