|
| 1 | +/* |
| 2 | + * Copyright 2010 Ning, Inc. |
| 3 | + * |
| 4 | + * Ning licenses this file to you under the Apache License, version 2.0 |
| 5 | + * (the "License"); you may not use this file except in compliance with the |
| 6 | + * License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.ning.http.client.oauth; |
| 17 | + |
| 18 | +import static org.testng.Assert.assertEquals; |
| 19 | +import static org.testng.Assert.fail; |
| 20 | + |
| 21 | +import java.io.UnsupportedEncodingException; |
| 22 | +import java.net.URLDecoder; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import com.ning.http.client.Param; |
| 29 | +import com.ning.http.client.Request; |
| 30 | +import com.ning.http.client.RequestBuilder; |
| 31 | +import com.ning.http.client.uri.Uri; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests the OAuth signature behavior. |
| 36 | + * |
| 37 | + * See <a href="https://oauth.googlecode.com/svn/code/javascript/example/signature.html">Signature Tester</a> for an |
| 38 | + * online oauth signature checker. |
| 39 | + * |
| 40 | + */ |
| 41 | +public class OAuthSignatureCalculatorTest { |
| 42 | + private static final String CONSUMER_KEY = "dpf43f3p2l4k3l03"; |
| 43 | + |
| 44 | + private static final String CONSUMER_SECRET = "kd94hf93k423kf44"; |
| 45 | + |
| 46 | + public static final String TOKEN_KEY = "nnch734d00sl2jdk"; |
| 47 | + |
| 48 | + public static final String TOKEN_SECRET = "pfkkdhi9sl3r4s00"; |
| 49 | + |
| 50 | + public static final String NONCE = "kllo9940pd9333jh"; |
| 51 | + |
| 52 | + final static long TIMESTAMP = 1191242096; |
| 53 | + |
| 54 | + private static class StaticOAuthSignatureCalculator extends OAuthSignatureCalculator { |
| 55 | + |
| 56 | + private final long timestamp; |
| 57 | + private final String nonce; |
| 58 | + |
| 59 | + public StaticOAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth, long timestamp, String nonce) { |
| 60 | + super(consumerAuth, userAuth); |
| 61 | + this.timestamp = timestamp; |
| 62 | + this.nonce = nonce; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected long generateTimestamp() { |
| 67 | + return timestamp; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected String generateNonce() { |
| 72 | + return nonce; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // based on the reference test case from |
| 77 | + // http://oauth.pbwiki.com/TestCases |
| 78 | + @Test(groups = "fast") |
| 79 | + public void testGetCalculateSignature() { |
| 80 | + ConsumerKey consumer = new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET); |
| 81 | + RequestToken user = new RequestToken(TOKEN_KEY, TOKEN_SECRET); |
| 82 | + OAuthSignatureCalculator calc = new OAuthSignatureCalculator(consumer, user); |
| 83 | + List<Param> queryParams = new ArrayList<>(); |
| 84 | + queryParams.add(new Param("file", "vacation.jpg")); |
| 85 | + queryParams.add(new Param("size", "original")); |
| 86 | + String url = "http://photos.example.net/photos"; |
| 87 | + String sig = calc.calculateSignature("GET", Uri.create(url), TIMESTAMP, NONCE, null, queryParams); |
| 88 | + |
| 89 | + assertEquals(sig, "tR3+Ty81lMeYAr/Fid0kMTYa/WM="); |
| 90 | + } |
| 91 | + |
| 92 | + @Test(groups = "fast") |
| 93 | + public void testPostCalculateSignature() { |
| 94 | + ConsumerKey consumer = new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET); |
| 95 | + RequestToken user = new RequestToken(TOKEN_KEY, TOKEN_SECRET); |
| 96 | + OAuthSignatureCalculator calc = new StaticOAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE); |
| 97 | + |
| 98 | + List<Param> formParams = new ArrayList<Param>(); |
| 99 | + formParams.add(new Param("file", "vacation.jpg")); |
| 100 | + formParams.add(new Param("size", "original")); |
| 101 | + String url = "http://photos.example.net/photos"; |
| 102 | + final Request req = new RequestBuilder("POST") |
| 103 | + .setUri(Uri.create(url)) |
| 104 | + .setFormParams(formParams) |
| 105 | + .setSignatureCalculator(calc).build(); |
| 106 | + |
| 107 | + // From the signature tester, POST should look like: |
| 108 | + // normalized parameters: file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original |
| 109 | + // signature base string: POST&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 |
| 110 | + // signature: wPkvxykrw+BTdCcGqKr+3I+PsiM= |
| 111 | + // header: OAuth realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%3D" |
| 112 | + |
| 113 | + String authHeader = req.getHeaders().get("Authorization").get(0); |
| 114 | + Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader); |
| 115 | + assertEquals(m.find(), true); |
| 116 | + String encodedSig = m.group(1); |
| 117 | + String sig = null; |
| 118 | + try { |
| 119 | + sig = URLDecoder.decode(encodedSig, "UTF-8"); |
| 120 | + } catch (UnsupportedEncodingException e) { |
| 121 | + fail("bad encoding", e); |
| 122 | + } |
| 123 | + |
| 124 | + assertEquals(sig, "wPkvxykrw+BTdCcGqKr+3I+PsiM="); |
| 125 | + } |
| 126 | + |
| 127 | + @Test(groups = "fast") |
| 128 | + public void testGetWithRequestBuilder() { |
| 129 | + ConsumerKey consumer = new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET); |
| 130 | + RequestToken user = new RequestToken(TOKEN_KEY, TOKEN_SECRET); |
| 131 | + OAuthSignatureCalculator calc = new StaticOAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE); |
| 132 | + |
| 133 | + List<Param> queryParams = new ArrayList<Param>(); |
| 134 | + queryParams.add(new Param("file", "vacation.jpg")); |
| 135 | + queryParams.add(new Param("size", "original")); |
| 136 | + String url = "http://photos.example.net/photos"; |
| 137 | + |
| 138 | + final Request req = new RequestBuilder("GET") |
| 139 | + .setUri(Uri.create(url)) |
| 140 | + .setQueryParams(queryParams) |
| 141 | + .setSignatureCalculator(calc).build(); |
| 142 | + |
| 143 | + final List<Param> params = req.getQueryParams(); |
| 144 | + assertEquals(params.size(), 2); |
| 145 | + |
| 146 | + // From the signature tester, the URL should look like: |
| 147 | + //normalized parameters: file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original |
| 148 | + //signature base string: 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 |
| 149 | + //signature: tR3+Ty81lMeYAr/Fid0kMTYa/WM= |
| 150 | + //Authorization header: OAuth realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D" |
| 151 | + |
| 152 | + String authHeader = req.getHeaders().get("Authorization").get(0); |
| 153 | + Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader); |
| 154 | + assertEquals(m.find(), true); |
| 155 | + String encodedSig = m.group(1); |
| 156 | + String sig = null; |
| 157 | + try { |
| 158 | + sig = URLDecoder.decode(encodedSig, "UTF-8"); |
| 159 | + } catch (UnsupportedEncodingException e) { |
| 160 | + fail("bad encoding", e); |
| 161 | + } |
| 162 | + |
| 163 | + assertEquals(sig, "tR3+Ty81lMeYAr/Fid0kMTYa/WM="); |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments