/** Verify test case from Appendix A.5.1 of OAuth spec http://oauth.net/core/1.0a/#anchor12 */ public void testSignatureBaseString() throws MalformedURLException { OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.GET); och.setRequestUrl(new URL("http://photos.example.net/photos")); och.setConsumerKey("dpf43f3p2l4k3l03"); och.setToken("nnch734d00sl2jdk"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 1191242096L; och.setTimestamp(timestamp); String nonce = "kllo9940pd9333jh"; och.setNonce(nonce); List<Pair<String, String>> extraParams = new ArrayList<Pair<String, String>>(); extraParams.add(new Pair<String, String>("file", "vacation.jpg")); extraParams.add(new Pair<String, String>("size", "original")); och.setExtraAuthorizationHeaderParams(extraParams); String expectedSignatureBaseString = "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); // parameters in the query string get included in the signature och.setRequestUrl(new URL("http://photos.example.net/photos?file=vacation.jpg&size=original")); och.setExtraAuthorizationHeaderParams(null); assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); }
/** * Test based on the example at * http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/ entitled "Non * URL-Safe Parameter" */ public void testOAuthCallWithNonUrlSafeParams() throws MalformedURLException { // http://PHOTOS.example.net:8001/Photos with the parameters photo size=300%, title=Back of $100 // Dollars Bill OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.GET); och.setRequestUrl( new URL( "http://PHOTOS.example.net:8001/Photos?" + OAuthUtils.urlEncode("photo size") + "=" + OAuthUtils.urlEncode("300%") + "&title=" + OAuthUtils.urlEncode("Back of $100 Dollars Bill"))); och.setConsumerKey("dpf43f3++p+#2l4k3l03"); och.setConsumerSecret("kd9@4h%%4f93k423kf44"); och.setToken("nnch734d(0)0sl2jdk"); och.setTokenSecret("pfkkd#hi9_sl-3r=4s00"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 1191242096L; och.setTimestamp(timestamp); String nonce = "kllo~9940~pd9333jh"; och.setNonce(nonce); String expectedSignatureBaseString = "GET&http%3A%2F%2Fphotos.example.net%3A8001%2FPhotos&oauth_consumer_key%3Ddpf43f3%252B%252Bp%252B%25232l4k3l03%26oauth_nonce%3Dkllo~9940~pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d%25280%25290sl2jdk%26oauth_version%3D1.0%26photo%2520size%3D300%2525%26title%3DBack%2520of%2520%2524100%2520Dollars%2520Bill"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); String expectedAuthorizationHeader = "OAuth oauth_consumer_key=\"dpf43f3%2B%2Bp%2B%232l4k3l03\", oauth_token=\"nnch734d%280%290sl2jdk\", oauth_nonce=\"kllo~9940~pd9333jh\", oauth_timestamp=\"1191242096\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"tTFyqivhutHiglPvmyilZlHm5Uk%3D\""; assertEqualAuthorizationHeaders(expectedAuthorizationHeader, och.generateAuthorizationHeader()); }
/** * This tests that query params are being properly encoded. There was previously a bug in this * library that manifested itself when query param values needed escaping, such as the @ in the * name query param value. The correct behavior was verified with the OAuth calculator here: * http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/ */ public void testOAuthCallWithEmailParam() throws MalformedURLException { OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.GET); och.setRequestUrl( new URL( "https://cheetah.dhcp.pgdev.sap.corp/oauth/test_consumer_request?name=" + OAuthUtils.urlEncode("*****@*****.**"))); och.setConsumerKey("1VMzOctCAidMaahS9yJU"); och.setConsumerSecret("JinGkf4bjzFQhOVUbhxpL3eU1esKgO8qTAGfCXy7"); och.setToken("kkk9d7dh3k39sjv7"); och.setTokenSecret("fYtRQv54NygyJXUTzebgCopW3a5RTaBruvByh92g"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 137131201L; och.setTimestamp(timestamp); String nonce = "7d8f3e4a"; och.setNonce(nonce); String expectedSignatureBaseString = "GET&https%3A%2F%2Fcheetah.dhcp.pgdev.sap.corp%2Foauth%2Ftest_consumer_request&name%3Dfred%2540gmail.com%26oauth_consumer_key%3D1VMzOctCAidMaahS9yJU%26oauth_nonce%3D7d8f3e4a%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk9d7dh3k39sjv7%26oauth_version%3D1.0"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); String expectedAuthorizationHeader = "OAuth oauth_consumer_key=\"1VMzOctCAidMaahS9yJU\", oauth_token=\"kkk9d7dh3k39sjv7\", oauth_nonce=\"7d8f3e4a\", oauth_timestamp=\"137131201\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"G3tfgEHevQfQ7jNlsWZ4O%2Fo92Xw%3D\""; assertEqualAuthorizationHeaders(expectedAuthorizationHeader, och.generateAuthorizationHeader()); }
/** Verify test case from the OAuth spec http://tools.ietf.org/html/rfc5849 section-3.4.1.1. */ public void testSignatureBaseString2() throws MalformedURLException { OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.POST); och.setRequestUrl(new URL("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b")); och.setRealm("Example"); och.setConsumerKey("9djdj82h48djs9d2"); och.setToken("kkk9d7dh3k39sjv7"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 137131201L; och.setTimestamp(timestamp); String nonce = "7d8f3e4a"; och.setNonce(nonce); och.omitOAuthVersion(); List<Pair<String, String>> requestBodyParams = new ArrayList<Pair<String, String>>(); requestBodyParams.add(new Pair<String, String>("c2", null)); requestBodyParams.add( new Pair<String, String>( "a3", "2 q")); // 2+q in the spec, since it is shown in the form of a HTTP request, where it // is encoded. och.setExtraRequestBodyParams(requestBodyParams); String expectedSignatureBaseString = "POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q" + "%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_" + "key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m" + "ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk" + "9d7dh3k39sjv7"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); }
/** * Test based on the example at * http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/ entitled "Non * English Parameters" */ public void testOAuthCallWithNonEnglishParams() throws MalformedURLException { // http://PHOTOS.example.net:8001/Photos with the type and scenario parameters having unicode // parameters OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.GET); // Used the online tool http://www.snible.org/java2/uni2java.html to convert the unicode strings // to Java String literals. String typeValue = "\u00D7\u0090\u00D7\u2022\u00D7\u02DC\u00D7\u2022\u00D7\u2018\u00D7\u2022\u00D7\u00A1"; // Note \u00A0 which encodes as UTF8 as C2A0 is the non-breaking space character. // http://stackoverflow.com/questions/2774471/what-is-c2-a0-in-mime-encoded-quoted-printable-text // If you just cut and paste from the web page, it will convert to a regular space, so be // careful! String scenarioValue = "\u00D7\u00AA\u00D7\u0090\u00D7\u2022\u00D7\u00A0\u00D7\u201D"; och.setRequestUrl( new URL( "http://PHOTOS.example.net:8001/Photos?type=" + OAuthUtils.urlEncode(typeValue) + "&scenario=" + OAuthUtils.urlEncode(scenarioValue))); och.setConsumerKey("dpf43f3++p+#2l4k3l03"); och.setConsumerSecret("kd9@4h%%4f93k423kf44"); och.setToken("nnch734d(0)0sl2jdk"); och.setTokenSecret("pfkkd#hi9_sl-3r=4s00"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 1191242096L; och.setTimestamp(timestamp); String nonce = "kllo~9940~pd9333jh"; och.setNonce(nonce); String expectedSignatureBaseString = "GET&http%3A%2F%2Fphotos.example.net%3A8001%2FPhotos&oauth_consumer_key%3Ddpf43f3%252B%252Bp%252B%25232l4k3l03%26oauth_nonce%3Dkllo~9940~pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d%25280%25290sl2jdk%26oauth_version%3D1.0%26scenario%3D%25C3%2597%25C2%25AA%25C3%2597%25C2%2590%25C3%2597%25E2%2580%25A2%25C3%2597%25C2%25A0%25C3%2597%25E2%2580%259D%26type%3D%25C3%2597%25C2%2590%25C3%2597%25E2%2580%25A2%25C3%2597%25CB%259C%25C3%2597%25E2%2580%25A2%25C3%2597%25E2%2580%2598%25C3%2597%25E2%2580%25A2%25C3%2597%25C2%25A1"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); String expectedAuthorizationHeader = "OAuth oauth_consumer_key=\"dpf43f3%2B%2Bp%2B%232l4k3l03\", oauth_token=\"nnch734d%280%290sl2jdk\", oauth_nonce=\"kllo~9940~pd9333jh\", oauth_timestamp=\"1191242096\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"MH9NDodF4I%2FV6GjYYVChGaKCtnk%3D\""; assertEqualAuthorizationHeaders(expectedAuthorizationHeader, och.generateAuthorizationHeader()); }
/** * Test based on a case where OAuth sort is not the same as lexicographic sort of the 'key=value' * Strings. For example, a1=3, a=2 sorts in OAuth order as a=2, a1=3, but as Strings it is a1=3, * a=2 since '1' < '='. The expected values were computed independently with the calculator at * http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/ * * @throws MalformedURLException */ public void testOAuthCallWithEdgeCasePairParamSortOrder() throws MalformedURLException { OAuthClientHelper och = new OAuthClientHelper(); och.setHttpMethod(HttpMethod.GET); och.setRequestUrl(new URL("http://PHOTOS.example.net:8001/Photos?a1=3&a=2")); och.setConsumerKey("dpf43f3++p+#2l4k3l03"); och.setConsumerSecret("kd9@4h%%4f93k423kf44"); och.setToken("nnch734d(0)0sl2jdk"); och.setTokenSecret("pfkkd#hi9_sl-3r=4s00"); och.setSignatureMethod(SignatureMethod.HMAC_SHA1); long timestamp = 1191242096L; och.setTimestamp(timestamp); String nonce = "kllo~9940~pd9333jh"; och.setNonce(nonce); String expectedSignatureBaseString = "GET&http%3A%2F%2Fphotos.example.net%3A8001%2FPhotos&a%3D2%26a1%3D3%26oauth_consumer_key%3Ddpf43f3%252B%252Bp%252B%25232l4k3l03%26oauth_nonce%3Dkllo~9940~pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d%25280%25290sl2jdk%26oauth_version%3D1.0"; assertEquals(expectedSignatureBaseString, och.generateSignatureBaseString(nonce, timestamp)); String expectedAuthorizationHeader = "OAuth oauth_consumer_key=\"dpf43f3%2B%2Bp%2B%232l4k3l03\", oauth_token=\"nnch734d%280%290sl2jdk\", oauth_nonce=\"kllo~9940~pd9333jh\", oauth_timestamp=\"1191242096\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"XvQXwVym27PgKSIWiElVVSdGIq8%3D\""; assertEqualAuthorizationHeaders(expectedAuthorizationHeader, och.generateAuthorizationHeader()); }