Esempio n. 1
0
            public void SetFixture(MockRepository mocks)
            {
                var signer = new OAuthWebRequestSigner(new HmacSha1SignatureGenerator());


                webRequest = mocks.DynamicMock <IOAuthWebRequest>();
                var mockNonceGenerator     = mocks.DynamicMock <INonceGenerator>();
                var mockTimestampGenerator = mocks.DynamicMock <ITimestampGenerator>();
                var consumerToken          = new OAuthToken {
                    Token = consumerKey, TokenSecret = consumerSecret
                };

                mockNonceGenerator.Stub(nonceGen => nonceGen.Generate()).Return(nonce);
                mockTimestampGenerator.Stub(timestampGen => timestampGen.Generate()).Return(timestamp);
                webRequest.Stub(request => request.ConsumerToken).Return(consumerToken);
                webRequest.Stub(request => request.NonceGenerator).Return(mockNonceGenerator);
                webRequest.Stub(request => request.TimestampGenerator).Return(mockTimestampGenerator);
                webRequest.Stub(request => request.RequestUri).Return(new Uri(url));
                webRequest.Stub(request => request.Method).Return("GET");
                webRequest.Stub(request => request.SetUri(string.Empty)).IgnoreArguments().Do(
                    (Action <string>)(uri => setUri = uri));
                mocks.ReplayAll();

                signer.SignWebRequest(webRequest);
            }
            public WebResponse Create(IOAuthWebRequest request)
            {
                var httpWebRequest = (HttpWebRequest) WebRequest.Create(request.RequestUri);
                httpWebRequest.AutomaticDecompression = request.Compression;
                httpWebRequest.UserAgent = request.UserAgent ?? "OAuth WebRequest";
                if (request.Timeout != int.MinValue)
                    httpWebRequest.Timeout = request.Timeout;
                if (request.Proxy != null)
                    httpWebRequest.Proxy = request.Proxy;
                httpWebRequest.Method = request.Method??"GET";
                if(request.IfModifiedSince.HasValue)
                {
                    httpWebRequest.IfModifiedSince = request.IfModifiedSince.Value;
                }
                if (request.Method == "POST" || request.Method == "PUT")
                {
                    if (request.Form.Count > 0)
                    {
                        var formPost = GetPostString(request.Form);
                        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                        var swRequestWriter = new
                        StreamWriter(httpWebRequest.GetRequestStream());
                        swRequestWriter.Write(formPost);
                        swRequestWriter.Close();
                    }
                }

                if (request.Headers.Count > 0)
                {
                    httpWebRequest.Headers.Add(request.Headers);
                }
                return httpWebRequest.GetResponse();
            }
Esempio n. 3
0
        internal static List <QueryParameter> GetAllParameters(IOAuthWebRequest request)
        {
            var parameters = new List <QueryParameter>();

            parameters.AddRange(GetQueryStringParameters(request.RequestUri));
            if (request.Method == "POST")
            {
                parameters.AddRange(request.Form);
            }
            parameters.AddRange(GetSignatureBaseParameters(parameters, request));
            return(parameters);
        }
Esempio n. 4
0
            public WebResponse Create(IOAuthWebRequest request)
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(request.RequestUri);

                httpWebRequest.AutomaticDecompression = request.Compression;
                httpWebRequest.UserAgent = request.UserAgent ?? "OAuth WebRequest";
                if (request.Timeout != int.MinValue)
                {
                    httpWebRequest.Timeout = request.Timeout;
                }
                if (request.Proxy != null)
                {
                    httpWebRequest.Proxy = request.Proxy;
                }
                httpWebRequest.Method = request.Method ?? "GET";
                if (request.IfModifiedSince.HasValue)
                {
                    httpWebRequest.IfModifiedSince = request.IfModifiedSince.Value;
                }
                if (request.Method == "POST" || request.Method == "PUT")
                {
                    if (request.Form.Count > 0)
                    {
                        var formPost = GetPostString(request.Form);
                        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                        var swRequestWriter = new
                                              StreamWriter(httpWebRequest.GetRequestStream());
                        swRequestWriter.Write(formPost);
                        swRequestWriter.Close();
                    }
                }

                if (request.Headers.Count > 0)
                {
                    httpWebRequest.Headers.Add(request.Headers);
                }
                return(httpWebRequest.GetResponse());
            }
    public void SignWebRequest(IOAuthWebRequest request)
    {
      var secret = request.ConsumerToken.TokenSecret + "&";
      if (request.Token != null)
        secret = secret + request.Token.TokenSecret;
      var parameters = GetAllParameters(request);
      var signatureBase = GetSignatureBase(request,parameters);
      var signature = SignatureGenerator.Generate(secret, signatureBase);

      //sort out a clean request
      var oauthParameters = parameters.Where(p => oauthBaseSignatureParams.Contains(p.Key)).Concat(new []{new QueryParameter(Parameters.OAuth_Signature,signature)});
      var oauthExemptQueryParameters = HttpUtility.ParseQueryString(request.RequestUri.Query).ToQueryParameters().Where(p => !oauthBaseSignatureParams.Contains(p.Key));
      if (request.Method == "POST")
      {
        IEnumerable<QueryParameter> oauthExemptFormParameters = request.Form.Where(p => !oauthBaseSignatureParams.Contains(p.Key)).ToList();
        request.Form.Clear();
        request.Form.AddRange(oauthExemptFormParameters);
      }
      if(request.UseAuthorizationHeader)
      {
        request.Headers.Add(Parameters.OAuth_Authorization_Header,GenerateOAuthHeader(oauthParameters));
      }
      else if(request.Method == "POST")
      {
        request.Form.AddRange(oauthParameters);
      }
      else
      {
        oauthExemptQueryParameters = oauthExemptQueryParameters.Concat(oauthParameters);
      }
      request.SetUri(NormalizeUri(request.RequestUri) + "?" + GetQueryString(oauthExemptQueryParameters));




    }
Esempio n. 6
0
        public void SignWebRequest(IOAuthWebRequest request)
        {
            var secret = request.ConsumerToken.TokenSecret + "&";

            if (request.Token != null)
            {
                secret = secret + request.Token.TokenSecret;
            }
            var parameters    = GetAllParameters(request);
            var signatureBase = GetSignatureBase(request, parameters);
            var signature     = SignatureGenerator.Generate(secret, signatureBase);

            //sort out a clean request
            var oauthParameters            = parameters.Where(p => oauthBaseSignatureParams.Contains(p.Key)).Concat(new [] { new QueryParameter(Parameters.OAuth_Signature, signature) });
            var oauthExemptQueryParameters = HttpUtility.ParseQueryString(request.RequestUri.Query).ToQueryParameters().Where(p => !oauthBaseSignatureParams.Contains(p.Key));

            if (request.Method == "POST")
            {
                IEnumerable <QueryParameter> oauthExemptFormParameters = request.Form.Where(p => !oauthBaseSignatureParams.Contains(p.Key)).ToList();
                request.Form.Clear();
                request.Form.AddRange(oauthExemptFormParameters);
            }
            if (request.UseAuthorizationHeader)
            {
                request.Headers.Add(Parameters.OAuth_Authorization_Header, GenerateOAuthHeader(oauthParameters));
            }
            else if (request.Method == "POST")
            {
                request.Form.AddRange(oauthParameters);
            }
            else
            {
                oauthExemptQueryParameters = oauthExemptQueryParameters.Concat(oauthParameters);
            }
            request.SetUri(NormalizeUri(request.RequestUri) + "?" + GetQueryString(oauthExemptQueryParameters));
        }
    private static IEnumerable<QueryParameter> GetSignatureBaseParameters(List<QueryParameter> parameters, IOAuthWebRequest request)
    {
      var baseParameters = new List<QueryParameter>();
      
      foreach (var parameter in oauthBaseSignatureParams)
      {
        string expectedParameter = parameter;
        if (!parameters.Exists(p => p.Key == expectedParameter))
        {
          baseParameters.Add(new QueryParameter(parameter,ParameterFetchers[parameter].Invoke(request)));
        }
        else
        {
          var param = parameters.Single(kvp => kvp.Key == expectedParameter);
          parameters.Remove(param);
          baseParameters.Add(param);
        }
      }

      return baseParameters.Where(param => !(param.Key == Parameters.OAuth_Token && string.IsNullOrEmpty(param.Value)));
    }
 internal static List<QueryParameter> GetAllParameters(IOAuthWebRequest request)
 {
   var parameters = new List<QueryParameter>();
   parameters.AddRange(GetQueryStringParameters(request.RequestUri));
   if (request.Method == "POST")
   {
     parameters.AddRange(request.Form);
   }
   parameters.AddRange(GetSignatureBaseParameters(parameters, request));
   return parameters;
 }
 private string GetSignatureBase(IOAuthWebRequest request, List<QueryParameter> parameters)
 {
   
   var method = request.Method;
   return GetSignatureBase(method, request.RequestUri, parameters);
 }
Esempio n. 10
0
        private static IEnumerable <QueryParameter> GetSignatureBaseParameters(List <QueryParameter> parameters, IOAuthWebRequest request)
        {
            var baseParameters = new List <QueryParameter>();

            foreach (var parameter in oauthBaseSignatureParams)
            {
                string expectedParameter = parameter;
                if (!parameters.Exists(p => p.Key == expectedParameter))
                {
                    baseParameters.Add(new QueryParameter(parameter, ParameterFetchers[parameter].Invoke(request)));
                }
                else
                {
                    var param = parameters.Single(kvp => kvp.Key == expectedParameter);
                    parameters.Remove(param);
                    baseParameters.Add(param);
                }
            }

            return(baseParameters.Where(param => !(param.Key == Parameters.OAuth_Token && string.IsNullOrEmpty(param.Value))));
        }
Esempio n. 11
0
        private string GetSignatureBase(IOAuthWebRequest request, List <QueryParameter> parameters)
        {
            var method = request.Method;

            return(GetSignatureBase(method, request.RequestUri, parameters));
        }