Exemplo n.º 1
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <returns>Task&lt;System.String&gt;.</returns>
        /// <exception cref="CommunicationException"></exception>
        public async Task <AuthenticationHeaderValue> GetAuthenticationHeader()
        {
            if (_userName == null)
            {
                // no point in looking if the username hasn't been set up
                return(null);
            }

            if (_cachedAuthenticationHeader != null && DateTime.UtcNow < _tokenExpiryDateUtc)
            {
                // used the cached token;
                return(_cachedAuthenticationHeader);
            }
            else
            {
                Dictionary <string, string> postValues = new Dictionary <string, string>();

                postValues["grant_type"] = "password";
                postValues["username"]   = _userName;
                postValues["password"]   = _password;

                var timeoutSeconds = _settingsService.Setting <int?>("Communication.TimeoutSeconds");
                var timeout        = (timeoutSeconds.HasValue)
                                                                                ? (TimeSpan?)TimeSpan.FromSeconds(timeoutSeconds.Value)
                                                                                : null;

                var communicationHelper = new CommunicationHelper <LoginResponse, LoginErrorResponse> {
                    Timeout = timeout
                };

                var loginResponse = await communicationHelper.PostRequest(_urlHelper.BuildUrl("oauth2/token"), postValues);

                if (loginResponse == null)
                {
                    throw new CommunicationException(communicationHelper.ErrorResponse?.ToString());
                }
                else
                {
                    _cachedAuthenticationHeader = new AuthenticationHeaderValue("Bearer", loginResponse.AccessToken);

                    // remember the expiry date, but give ourselves a bit of leeway with the time
                    _tokenExpiryDateUtc = DateTime.UtcNow + TimeSpan.FromSeconds(loginResponse.ExpiresInSeconds.Value) - TimeSpan.FromMinutes(2);

                    return(_cachedAuthenticationHeader);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <SessionUpdateResponse> PostSessionUpdateRequestToServer(SessionUpdateRequest sessionUpdateRequest)
        {
            var communicationHelper = new CommunicationHelper <SessionUpdateResponse, ErrorResponse> {
                Timeout = GetTimeout()
            };

            var authenticationHeader = await _accountService.GetAuthenticationHeader();

            var response = await communicationHelper.PostRequest(_urlHelper.BuildUrl("api/Cases/SessionUpdate"), JsonConvert.SerializeObject(sessionUpdateRequest), authenticationHeader);

            if (response == null)
            {
                throw new CommunicationException(communicationHelper.ErrorResponse?.ToStringList());
            }

            return(response);
        }
Exemplo n.º 3
0
        public async Task <ValidationResponse> ValidateRequest(ValidatedSession caseAuthorizationRequest, bool save = true)
        {
            try
            {
                var communicationHelper = new CommunicationHelper <ValidationResponse, ErrorResponse> {
                    Timeout = GetTimeout()
                };

                var authenticationHeader = await _accountService.GetAuthenticationHeader();

                var validationRequest = new ValidationRequest {
                    RequestedValidatedSession = caseAuthorizationRequest
                };

                var queryStringValues = new Dictionary <string, string>();
                queryStringValues["save"] = save.ToString();

                var response = await communicationHelper.PostRequest(_urlHelper.BuildUrl("api/Cases/Validate", queryStringValues), JsonConvert.SerializeObject(validationRequest), authenticationHeader);

                if (response == null)
                {
                    throw new CommunicationException(communicationHelper.ErrorResponse?.ToStringList());
                }

                return(response);
            }
            catch (Exception ex)
            {
                var validationResponse = new ValidationResponse()
                {
                    Errors = new List <string>()
                };

#if DEBUG
                string errorMessage = "Unable to validate request : " + ex.GetType().Name + " " + ex.Message;
#else
                string errorMessage = "Unable to validate request.";
#endif
                validationResponse.Errors.Add(errorMessage);

                return(validationResponse);
            }
        }