@Test public void shouldIncludeOAuthAndQueryAndBodyParams() throws Exception { // mock a request that has custom query, body, and header params set HttpRequest request = mock(HttpRequest.class); when(request.getRequestUrl()).thenReturn("http://example.com?a=1+1"); ByteArrayInputStream body = new ByteArrayInputStream("b=2+2".getBytes()); when(request.getMessagePayload()).thenReturn(body); when(request.getContentType()).thenReturn( "application/x-www-form-urlencoded; charset=ISO-8859-1"); when(request.getHeader("Authorization")).thenReturn( "OAuth realm=\"http%3A%2F%2Fexample.com\", oauth_token=\"12%25345\", oauth_signature=\"1234\""); OAuthMessageSigner signer = mock(HmacSha1MessageSigner.class); consumer.setMessageSigner(signer); consumer.sign(request); // verify that all custom params are properly read and passed to the // message signer ArgumentMatcher<HttpParameters> hasAllParameters = new ArgumentMatcher<HttpParameters>() { public boolean matches(Object argument) { HttpParameters params = (HttpParameters) argument; assertEquals("1 1", params.getFirst("a", true)); assertEquals("2 2", params.getFirst("b", true)); assertEquals("http://example.com", params.getFirst("realm", true)); assertEquals("12%345", params.getFirst("oauth_token", true)); // signature should be dropped, not valid to pre-set assertNull(params.getFirst("oauth_signature")); return true; } }; verify(signer).sign(same(request), argThat(hasAllParameters)); }
@Test public void shouldHonorManuallySetSigningParameters() throws Exception { // mock a request that has custom query, body, and header params set HttpRequest request = mock(HttpRequest.class); when(request.getRequestUrl()).thenReturn("http://example.com?a=1"); OAuthMessageSigner signer = mock(HmacSha1MessageSigner.class); consumer.setMessageSigner(signer); HttpParameters params = new HttpParameters(); params.put("oauth_callback", "http://mycallback"); consumer.setAdditionalParameters(params); consumer.sign(request); // verify that all custom params are properly read and passed to the // message signer ArgumentMatcher<HttpParameters> hasParameters = new ArgumentMatcher<HttpParameters>() { public boolean matches(Object argument) { HttpParameters params = (HttpParameters) argument; assertEquals("http://mycallback", params.getFirst("oauth_callback")); assertEquals("1", params.getFirst("a")); return true; } }; verify(signer).sign(same(request), argThat(hasParameters)); }
public static OAuthConsumer initConsumer(String consumerKey, String consumerSecret) { OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); consumer.setMessageSigner(new PlainTextMessageSigner()); consumer.setTokenWithSecret("", ""); return consumer; }
tokenSecret); consumer.setTokenWithSecret("", ""); consumer.setMessageSigner(new HmacSha1MessageSigner()); consumer.setSendEmptyTokens(true);
returnedConsumer.setMessageSigner(new PlainTextMessageSigner()); returnedConsumer.setTokenWithSecret(consumer.getToken(), consumer.getTokenSecret()); return returnedConsumer;
protected OAuthConsumer getConsumer(OAuth1Adapter adapter) { OAuthConsumer consumer = adapter.getConsumer(); if (consumer == null) { consumer = new DefaultOAuthConsumer(adapter.getConsumerKey(), adapter.getConsumerSecret()); consumer.setMessageSigner(adapter.getMessageSigner()); consumer.setSigningStrategy(adapter.getSigningStrategy()); adapter.setConsumer(consumer); } return consumer; } }
oAuthConsumer.setMessageSigner(oAuthSigner); oAuthConsumer.setAdditionalParameters(params);