public void AccessProtectedResource()
 {
     OAuthConsumer consumer = CreateConsumer(SignatureMethod.RsaSha1);
     var contextFactory = new OAuthContextFactory();
     OAuthContext context = contextFactory.FromUri("GET", new Uri("http://localhost/protected.rails"));
     consumer.SignContext(context,
                          new TokenBase {ConsumerKey = "key", Token = "accesskey", TokenSecret = "accesssecret"});
     AccessOutcome outcome = provider.VerifyProtectedResourceRequest(context);
     Assert.IsTrue(outcome.Granted, outcome.AdditionalInfo);
     Assert.IsNotNull(outcome.AccessToken);
 }
        public void MakeAuthenticatedCallForTokenRsaSha1WithPost()
        {
            var accessToken = new TokenBase {ConsumerKey = "key", Token = "accesskey", TokenSecret = "accesssecret"};
            OAuthConsumer consumer = CreateConsumer(SignatureMethod.RsaSha1);

            var factory = new OAuthContextFactory();
            OAuthContext context = factory.FromUri("POST", new Uri("http://term.ie/oauth/example/echo_api.php"));
            context.FormEncodedParameters.Add("success", "true");

            HttpWebResponse response = consumer.GetResponse(context, accessToken);

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                string contents = reader.ReadToEnd();
                Assert.AreEqual("success=true", contents);
            }
        }
        public void MakeAuthenticatedCallForTokenRsaSha1WithPost()
        {
            var accessToken = new TokenBase {
                ConsumerKey = "key", Token = "accesskey", TokenSecret = "accesssecret"
            };
            OAuthConsumer consumer = CreateConsumer(SignatureMethod.RsaSha1);

            var          factory = new OAuthContextFactory();
            OAuthContext context = factory.FromUri("POST", new Uri("http://term.ie/oauth/example/echo_api.php"));

            context.FormEncodedParameters.Add("success", "true");

            HttpWebResponse response = consumer.GetResponse(context, accessToken);

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                string contents = reader.ReadToEnd();
                Assert.AreEqual("success=true", contents);
            }
        }
        private string CreateApiResourceRequest(IOAuthConsumer consumer, string relativePath)
        {
            // to request a protected resource via GET we:

            // 1. Construct the uri we want to access
            // 2. create an "OAuthContext" representing the request.
            // 3. Sign the context using our OAuth consumer implementation (calculates and sets the signature etc.)
            // 4. Generate a Uri from the OAuthContext context and return it.

            
            var absoloutePath = new Uri(Request.Uri, relativePath);

            var factory = new OAuthContextFactory();

            OAuthContext context = factory.FromUri("GET", absoloutePath);

            consumer.SignContext(context, _testAccessToekn);

            return context.GenerateUrl();
        }
Exemplo n.º 5
0
        public OAuthContext BuildRequestTokenContext(NameValueCollection additionalQueryParameters)
        {
            EnsureStateIsValid();

            var auth = new NonceGenerator();

            var          factory = new OAuthContextFactory();
            var          signer  = new OAuthContextSigner();
            OAuthContext context = factory.FromUri("GET", RequestTokenUri);

            if (additionalQueryParameters != null)
            {
                context.QueryParameters.Add(additionalQueryParameters);
            }

            context.ConsumerKey     = ConsumerKey;
            context.RequestMethod   = "GET";
            context.SignatureMethod = SignatureMethod;
            context.Timestamp       = DateTime.Now.EpocString();
            context.Nonce           = auth.GenerateNonce();
            context.Version         = "1.0";

            string signatureBase = context.GenerateSignatureBase();

            Console.WriteLine("signature_base: {0}", signatureBase);

            signer.SignContext(context,
                               new SigningContext
            {
                Algorithm = Key, SignatureBase = signatureBase, ConsumerSecret = ConsumerSecret
            });

            Console.WriteLine("oauth_singature: {0}", context.Signature);

            Uri uri = context.GenerateUri();

            Console.WriteLine("Uri: {0}", uri);

            return(context);
        }
        public OAuthContext BuildExchangeRequestTokenForAccessTokenContext(TokenBase requestToken,
                                                                           NameValueCollection additionalQueryParameters)
        {
            EnsureStateIsValid();

            if (requestToken.ConsumerKey != ConsumerKey)
                throw Error.SuppliedTokenWasNotIssuedToThisConsumer(ConsumerKey, requestToken.ConsumerKey);

            var auth = new NonceGenerator();

            var factory = new OAuthContextFactory();
            var signer = new OAuthContextSigner();
            OAuthContext context = factory.FromUri("GET", AccessTokenUri);

            if (additionalQueryParameters != null)
                context.QueryParameters.Add(additionalQueryParameters);

            context.ConsumerKey = ConsumerKey;
            context.Token = requestToken.Token;
            context.TokenSecret = requestToken.TokenSecret;
            context.RequestMethod = "GET";
            context.SignatureMethod = SignatureMethod;
            context.Timestamp = DateTime.Now.EpocString();
            context.Nonce = auth.GenerateNonce();
            context.Version = "1.0";

            string signatureBase = context.GenerateSignatureBase();

            Console.WriteLine("signature_base: {0}", signatureBase);

            signer.SignContext(context,
                               new SigningContext
                                   {Algorithm = Key, SignatureBase = signatureBase, ConsumerSecret = ConsumerSecret});

            Console.WriteLine("oauth_singature: {0}", context.Signature);

            Uri uri = context.GenerateUri();

            Console.WriteLine("Uri: {0}", uri);

            return context;
        }