OAuth1SigningInterceptor implements Interceptor { Request signRequest(Request request) throws IOException { byte[] nonce = new byte[NUANCE_BYTES]; random.nextBytes(nonce); String oauthNonce = CHARACTER_PATTERN.matcher(ByteString.of(nonce).base64()).replaceAll(""); String oauthTimestamp = clock.millis(); String consumerKeyValue = escape(consumerKey); String accessTokenValue = escape(accessToken); SortedMap<String, String> parameters = new TreeMap<>(); parameters.put(OAUTH_CONSUMER_KEY, consumerKeyValue); parameters.put(OAUTH_ACCESS_TOKEN, accessTokenValue); parameters.put(OAUTH_NONCE, oauthNonce); parameters.put(OAUTH_TIMESTAMP, oauthTimestamp); parameters.put(OAUTH_SIGNATURE_METHOD, OAUTH_SIGNATURE_METHOD_VALUE); parameters.put(OAUTH_VERSION, OAUTH_VERSION_VALUE); HttpUrl url = request.url(); for (int i = 0; i < url.querySize(); i++) { parameters.put(escape(url.queryParameterName(i)), escape(url.queryParameterValue(i))); } Buffer body = new Buffer(); RequestBody requestBody = request.body(); if (requestBody != null && FORM_ENCODED_CONTENT_TYPE.equals(requestBody.contentType())) { requestBody.writeTo(body); } while (!body.exhausted()) { long keyEnd = body.indexOf((byte) '='); if (keyEnd == -1) throw new IllegalStateException("Key with no value: " + body.readUtf8()); String key = body.readUtf8(keyEnd); body.skip(1); long valueEnd = body.indexOf((byte) '&'); String value = valueEnd == -1 ? body.readUtf8() : body.readUtf8(valueEnd); if (valueEnd != -1) body.skip(1); parameters.put(key, value); } Buffer base = new Buffer(); String method = request.method(); base.writeUtf8(method); base.writeByte('&'); base.writeUtf8(escape(request.url() .newBuilder() .query(null) .build() .toString())); base.writeByte('&'); boolean first = true; for (Entry<String, String> entry : parameters.entrySet()) { if (!first) base.writeUtf8(escape("&")); first = false; base.writeUtf8(escape(entry.getKey())); base.writeUtf8(escape("=")); base.writeUtf8(escape(entry.getValue())); } String signingKey = escape(consumerSecret) + '&' + escape(accessSecret); String signature = base.hmacSha1(ByteString.encodeUtf8(signingKey)).base64(); String authorization = "OAuth " + OAUTH_CONSUMER_KEY + "=\"" + consumerKeyValue + "\", " + OAUTH_NONCE + "=\"" + oauthNonce + "\", " + OAUTH_SIGNATURE + "=\"" + escape(signature) + "\", " + OAUTH_SIGNATURE_METHOD + "=\"" + OAUTH_SIGNATURE_METHOD_VALUE + "\", " + OAUTH_TIMESTAMP + "=\"" + oauthTimestamp + "\", " + OAUTH_ACCESS_TOKEN + "=\"" + accessTokenValue + "\", " + OAUTH_VERSION + "=\"" + OAUTH_VERSION_VALUE + '"'; return request.newBuilder().addHeader("Authorization", authorization).build(); } OAuth1SigningInterceptor(String consumerKey, String consumerSecret, String accessToken, String accessSecret,
          Random random, Clock clock); @Override Response intercept(Chain chain);  }
@Test public void withFormBody() throws Exception { RequestBody body = new FormBody.Builder() .add("status", "Hello Ladies + Gentlemen, a signed OAuth request!") .build(); Request request = new Request.Builder() .url("https: .post(body) .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "tnnArxj06cWHq44gCs1OSKk%2FjLY%3D"); }
@Test public void withJsonBody() throws Exception { RequestBody body = RequestBody.create(MediaType.parse("application/json"), "{\"key\":\"value\"}"); Request request = new Request.Builder() .url("https: .post(body) .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "PvXMVlSUE38kti5PXeIU6kNtg4U%3D"); }
@Test public void noBody() throws Exception { Request request = new Request.Builder() .url("https: .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "hn5jxegoQxNM6SXvQgVhK15yQL8%3D"); }
@Test public void urlSameQueryParams() throws Exception { Request request = new Request.Builder() .url("https: .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "R8m%2BYY%2FZG5GJ%2F%2F3zCrE65DkTdCk%3D"); }
@Test public void urlQueryParams() throws Exception { Request request = new Request.Builder() .url("https: .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "kGp3zke%2Fv6IfGi%2B6eVmORqej4Wg%3D"); }
@Test public void urlQueryParamsWithSpace() throws Exception { Request request = new Request.Builder() .url("https: .build(); Request signed = oauth1.signRequest(request); assertAuthHeader(signed, "Z3saYRlzqfBzE%2BWTEzZtolIhJkc%3D"); }