Example #1
0
        private void InitializeSecurity()
        {
            if (Conventions.HandleUnauthorizedResponse != null)
            {
                return;                 // already setup by the user
            }
            var basicAuthenticator   = new BasicAuthenticator(credentials, ApiKey, jsonRequestFactory);
            var securedAuthenticator = new SecuredAuthenticator(ApiKey, basicAuthenticator);

            jsonRequestFactory.ConfigureRequest += basicAuthenticator.ConfigureRequest;
            jsonRequestFactory.ConfigureRequest += securedAuthenticator.ConfigureRequest;

#if !SILVERLIGHT
            Conventions.HandleUnauthorizedResponse = response =>
            {
                var oauthSource = response.Headers["OAuth-Source"];
                response.Close();

                if (string.IsNullOrEmpty(oauthSource) == false)
                {
                    return(basicAuthenticator.HandleOAuthResponse(oauthSource));
                }

                if (ApiKey == null)
                {
                    return(null);
                }
                oauthSource = Url + "/OAuth/API-Key";

                return(securedAuthenticator.DoOAuthRequest(oauthSource));
            };
#endif

            Conventions.HandleUnauthorizedResponseAsync = unauthorizedResponse =>
            {
                var oauthSource = unauthorizedResponse.Headers["OAuth-Source"];
                unauthorizedResponse.Close();

                if (string.IsNullOrEmpty(oauthSource) == false)
                {
                    return(basicAuthenticator.HandleOAuthResponseAsync(oauthSource));
                }

                if (ApiKey == null)
                {
                    return(null);
                }
                oauthSource = Url + "/OAuth/API-Key";

                return(securedAuthenticator.DoOAuthRequestAsync(oauthSource));
            };
        }
Example #2
0
        private void InitializeSecurity()
        {
            if (Conventions.HandleUnauthorizedResponse != null)
            {
                return;                 // already setup by the user
            }
            if (String.IsNullOrEmpty(ApiKey) == false)
            {
                Credentials = null;
            }

            var basicAuthenticator   = new BasicAuthenticator(jsonRequestFactory.EnableBasicAuthenticationOverUnsecuredHttpEvenThoughPasswordsWouldBeSentOverTheWireInClearTextToBeStolenByHackers);
            var securedAuthenticator = new SecuredAuthenticator();

            jsonRequestFactory.ConfigureRequest += basicAuthenticator.ConfigureRequest;
            jsonRequestFactory.ConfigureRequest += securedAuthenticator.ConfigureRequest;

#if !SILVERLIGHT && !NETFX_CORE
            Conventions.HandleUnauthorizedResponse = (response, credentials) =>
            {
                var oauthSource = response.Headers["OAuth-Source"];

                if (string.IsNullOrEmpty(oauthSource) == false &&
                    oauthSource.EndsWith("/OAuth/API-Key", StringComparison.CurrentCultureIgnoreCase) == false)
                {
                    return(basicAuthenticator.DoOAuthRequest(oauthSource, credentials.ApiKey));
                }

                if (credentials.ApiKey == null)
                {
                    AssertUnauthorizedCredentialSupportWindowsAuth(response, credentials.Credentials);

                    return(null);
                }
                if (string.IsNullOrEmpty(oauthSource))
                {
                    oauthSource = Url + "/OAuth/API-Key";
                }

                return(securedAuthenticator.DoOAuthRequest(oauthSource, credentials.ApiKey));
            };
#endif

            Conventions.HandleForbiddenResponseAsync = (forbiddenResponse, credentials) =>
            {
                if (credentials.ApiKey == null)
                {
                    AssertForbiddenCredentialSupportWindowsAuth(forbiddenResponse);
                    return(null);
                }

                return(null);
            };

            Conventions.HandleUnauthorizedResponseAsync = (unauthorizedResponse, credentials) =>
            {
                var oauthSource = unauthorizedResponse.Headers["OAuth-Source"];

                if (string.IsNullOrEmpty(oauthSource) == false &&
                    oauthSource.EndsWith("/OAuth/API-Key", StringComparison.CurrentCultureIgnoreCase) == false)
                {
                    return(basicAuthenticator.HandleOAuthResponseAsync(oauthSource, credentials.ApiKey));
                }

                if (credentials.ApiKey == null)
                {
                    AssertUnauthorizedCredentialSupportWindowsAuth(unauthorizedResponse, credentials.Credentials);
                    return(null);
                }

                if (string.IsNullOrEmpty(oauthSource))
                {
                    oauthSource = this.Url + "/OAuth/API-Key";
                }

                return(securedAuthenticator.DoOAuthRequestAsync(Url, oauthSource, credentials.ApiKey));
            };
        }
Example #3
0
        private void InitializeSecurity()
        {
            if (Conventions.HandleUnauthorizedResponse != null)
            {
                return;                 // already setup by the user
            }
            if (String.IsNullOrEmpty(ApiKey) == false)
            {
                Credentials = null;
            }

            var basicAuthenticator   = new BasicAuthenticator(ApiKey, jsonRequestFactory);
            var securedAuthenticator = new SecuredAuthenticator(ApiKey);

            jsonRequestFactory.ConfigureRequest += basicAuthenticator.ConfigureRequest;
            jsonRequestFactory.ConfigureRequest += securedAuthenticator.ConfigureRequest;

#if !SILVERLIGHT
            Conventions.HandleUnauthorizedResponse = response =>
            {
                var oauthSource = response.Headers["OAuth-Source"];

                if (string.IsNullOrEmpty(oauthSource) == false &&
                    oauthSource.EndsWith("/OAuth/API-Key", StringComparison.CurrentCultureIgnoreCase) == false)
                {
                    return(basicAuthenticator.HandleOAuthResponse(oauthSource));
                }

                if (ApiKey == null)
                {
                    AssertUnuthorizedCredentialSupportWindowsAuth(response);

                    return(null);
                }
                oauthSource = Url + "/OAuth/API-Key";

                return(securedAuthenticator.DoOAuthRequest(oauthSource));
            };
#endif

            Conventions.HandleForbiddenResponseAsync = forbiddenResponse =>
            {
                if (ApiKey == null)
                {
                    AssertForbiddenCredentialSupportWindowsAuth(forbiddenResponse);
                    return(null);
                }

                return(null);
            };

            Conventions.HandleUnauthorizedResponseAsync = unauthorizedResponse =>
            {
                var oauthSource = unauthorizedResponse.Headers["OAuth-Source"];

                if (string.IsNullOrEmpty(oauthSource) == false &&
                    oauthSource.EndsWith("/OAuth/API-Key", StringComparison.CurrentCultureIgnoreCase) == false)
                {
                    return(basicAuthenticator.HandleOAuthResponseAsync(oauthSource));
                }

                if (ApiKey == null)
                {
                    AssertUnuthorizedCredentialSupportWindowsAuth(unauthorizedResponse);
                    return(null);
                }
                oauthSource = Url + "/OAuth/API-Key";

                return(securedAuthenticator.DoOAuthRequestAsync(oauthSource));
            };
        }