Exemplo n.º 1
0
        private void AuthQueueResponseCallback(AsyncHttpResponse response, ClientAuthenticationQueue queue)
        {
            var queueState = (AuthQueueState)response.State;
            var peer = queueState.Peer;
            var authRequest = queueState.AuthenticateRequest;
            var sendParameters = queueState.SendParameters;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Authenticate client finished: conId={0}, result={1}", peer.ConnectionId, response.Status);
            }

            switch (response.Status)
            {
                case HttpRequestQueueResultCode.Success:
                    {
                        if (response.ResponseData == null)
                        {
                            log.ErrorFormat("Custom authentication: failed. ResponseData is empty. AppId={0}/{1}",
                                authRequest.ApplicationId, authRequest.ApplicationId);

                            return;
                        }

                        // deserialize
                        var responseString = Encoding.UTF8.GetString(response.ResponseData).Trim(new char[] { '\uFEFF', '\u200B' });
                        CustomAuthenticationResult customAuthResult;
                        try
                        {
                            customAuthResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomAuthenticationResult>(responseString);
                            //TBD: handle backward compatibility incustomAuthResult class
                            if (customAuthResult.AuthCookie == null)
                            {
                                customAuthResult.AuthCookie = customAuthResult.Secure;
                            }
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Custom authentication: failed to deserialize response. AppId={0}/{1}, Response={2}, Uri={3}",
                                authRequest.ApplicationId, authRequest.ApplicationVersion, responseString, queue.Uri);

                            peer.OnCustomAuthenticationError(
                                ErrorCode.CustomAuthenticationFailed, "Custom authentication deserialization failed: " + ex.Message, authRequest, sendParameters, queueState.State);
                            this.IncrementFailedCustomAuth();
                            return;
                        }

                        this.IncrementResultCounters(customAuthResult, (CustomAuthResultCounters.Instance)queue.CustomData);
                        peer.OnCustomAuthenticationResult(customAuthResult, authRequest, sendParameters, queueState.State);
                        return;
                    }

                case HttpRequestQueueResultCode.Offline:
                case HttpRequestQueueResultCode.QueueFull:
                case HttpRequestQueueResultCode.QueueTimeout:
                case HttpRequestQueueResultCode.RequestTimeout:
                case HttpRequestQueueResultCode.Error:
                    {
                        if (response.RejectIfUnavailable)
                        {
                            peer.OnCustomAuthenticationError(
                                ErrorCode.CustomAuthenticationFailed, "Custom authentication service error: " + response.Status, authRequest, sendParameters, queueState.State);
                            this.IncrementFailedCustomAuth();
                        }
                        else
                        {
                            var result = new CustomAuthenticationResult { ResultCode = CustomAuthenticationResultCode.Ok };
                            peer.OnCustomAuthenticationResult(result, authRequest, sendParameters, queueState.State);
                        }
                        return;
                    }
            }
        }
Exemplo n.º 2
0
        protected void AddNewAuthProvider(string url, string nameValuePairAsQueryString, bool rejectIfUnavailable,
            ClientAuthenticationType authenticationType, string instanceName)
        {
            var authService = new ClientAuthenticationQueue(
                url,
                nameValuePairAsQueryString,
                rejectIfUnavailable,
                Settings.Default.HttpRequestTimeoutMS)
                              {
                                  MaxQueuedRequests = Settings.Default.MaxQueuedRequests,
                                  MaxConcurrentRequests = Settings.Default.MaxConcurrentRequests,
                                  ReconnectInterval = TimeSpan.FromSeconds(Settings.Default.ReconnectIntervalS),
                                  QueueTimeout = TimeSpan.FromSeconds(Settings.Default.QueueTimeoutS),
                                  MaxErrorRequests = Settings.Default.MaxErrorRequests,
                                  MaxTimedOutRequests = Settings.Default.MaxTimedOutRequests,
                                  MaxBackoffTimeInMiliseconds = Settings.Default.MaxBackoffTimeInMiliseconds,
                                  CustomData = CustomAuthResultCounters.GetInstance(instanceName)
                              };

            var counters = this.httpQueueCountersFactory != null ? this.httpQueueCountersFactory.Create(instanceName) : null;
            authService.SetHttpRequestQueueCounters(counters);
            this.authenticationServices.Add(authenticationType, authService);
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Auth Provider added. provider:{0}", url);
            }
        }