public object BeforeSendRequest(ref Message request, IClientChannel channel) { HttpRequestMessageProperty httpRequestProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; NameValueCollection rawParameters = HttpUtility.ParseQueryString(request.Headers.To.Query); UriBuilder uriBuilder = new UriBuilder(request.Headers.To); uriBuilder.Query = Rfc3986.EncodeAndJoin(rawParameters); request.Headers.To = uriBuilder.Uri; OAuthConsumer consumer = new OAuthConsumer(_credentials.OAuthKey, _credentials.OAuthSecret); int unixTime = UnixTime.ToUnixTime(DateTime.UtcNow); OAuthParameters parameters = new OAuthParameters(); parameters.Version = Constants.Version1_0; parameters.SignatureMethod = "RSA-SHA1"; parameters.ConsumerKey = _credentials.OAuthKey; parameters.Token = _credentials.OAuthToken; parameters.TokenSecret = _credentials.OAuthTokenSecret; parameters.Timestamp = unixTime.ToString(); parameters.Nonce = _nonceProvider.GenerateNonce(unixTime); RsaSha1SigningProvider provider = new RsaSha1SigningProvider(); provider.PfxFile = _credentials.OAuthPfxFile; provider.PfxPassword = _credentials.OAuthPfxPassword; parameters.Sign(request.Headers.To, httpRequestProperty.Method, consumer, null, provider); httpRequestProperty.Headers["Authorization"] = parameters.ToHeaderFormat(); return null; }
public void TestConsumerRequestSignature() { OAuthService service = OAuthService.Create( new EndPoint("http://example.com/request_token"), new Uri("http://example.com/authorize"), new EndPoint("http://example.com/access_token"), new MockConsumer() { Key = "dpf43f3p2l4k3l03", Secret = "kd94hf93k423kf44", Status = ConsumerStatus.Valid }); OAuthRequest consumerRequest = OAuthConsumerRequest.Create( new EndPoint("http://provider.example.net/profile", "GET"), service); OAuthParameters authParameters = new OAuthParameters() { ConsumerKey = service.Consumer.Key, Realm = service.Realm, SignatureMethod = service.SignatureMethod, Timestamp = "1191242096", Nonce = "kllo9940pd9333jh", Version = service.OAuthVersion }; Assert.AreEqual( SignatureBase.Create(consumerRequest.ResourceEndPoint.HttpMethod, consumerRequest.ResourceEndPoint.Uri, authParameters), "GET&http%3A%2F%2Fprovider.example.net%2Fprofile&oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_version%3D1.0"); authParameters.Sign(consumerRequest.ResourceEndPoint.Uri, consumerRequest.ResourceEndPoint.HttpMethod, service.Consumer, consumerRequest.RequestToken, new OAuth.Net.Components.HmacSha1SigningProvider()); Assert.AreEqual( authParameters.Signature, "SGtGiOrgTGF5Dd4RUMguopweOSU="); }
protected virtual void SignParameters(Uri requestUri, string httpMethod, OAuthParameters authParameters, IToken token) { // Check there is a signing provider for the signature method ISigningProvider signingProvider = this.Service.ComponentLocator.GetInstance<ISigningProvider>(Constants.SigningProviderIdPrefix + this.Service.SignatureMethod); if (signingProvider == null) { // There is no signing provider for this signature method OAuthRequestException.ThrowSignatureMethodRejected(null); } // Double check the signing provider declares that it can handle the signature method if (!signingProvider.SignatureMethod.Equals(this.Service.SignatureMethod)) OAuthRequestException.ThrowSignatureMethodRejected(null); // Compute the signature authParameters.Sign(requestUri, httpMethod, this.Service.Consumer, token, signingProvider); }