/** * Computes RFC 2104-compliant HMAC signature. * * @param data the data to be signed * @param token the token * @return signature * @see <a href="http://oauth.net/core/1.0a/#rfc.section.9.2.1">OAuth Core - 9.2.1. Generating * Signature</a> */ /* package */ String generateSignature(final String data, final OAuthToken token) { byte[] byteHMAC = null; try { final Mac mac = Mac.getInstance(HMAC_SHA1); SecretKeySpec spec; if (null == token) { final String oauthSignature = HttpParameter.encode(consumerSecret) + "&"; spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1); } else { spec = token.getSecretKeySpec(); if (null == spec) { final String oauthSignature = HttpParameter.encode(consumerSecret) + "&" + HttpParameter.encode(token.getTokenSecret()); spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1); token.setSecretKeySpec(spec); } } mac.init(spec); byteHMAC = mac.doFinal(data.getBytes()); } catch (final InvalidKeyException ike) { logger.error("Failed initialize \"Message Authentication Code\" (MAC)", ike); throw new AssertionError(ike); } catch (final NoSuchAlgorithmException nsae) { logger.error("Failed to get HmacSHA1 \"Message Authentication Code\" (MAC)", nsae); throw new AssertionError(nsae); } return BASE64Encoder.encode(byteHMAC); }
public List<HttpParameter> generateOAuthSignatureHttpParams( final String method, final String sign_url) { final long timestamp = System.currentTimeMillis() / 1000; final long nonce = timestamp + RAND.nextInt(); final List<HttpParameter> oauthHeaderParams = new ArrayList<HttpParameter>(5); oauthHeaderParams.add(new HttpParameter("oauth_consumer_key", consumerKey)); oauthHeaderParams.add(OAUTH_SIGNATURE_METHOD); oauthHeaderParams.add(new HttpParameter("oauth_timestamp", timestamp)); oauthHeaderParams.add(new HttpParameter("oauth_nonce", nonce)); oauthHeaderParams.add(new HttpParameter("oauth_version", "1.0")); if (oauthToken != null) { oauthHeaderParams.add(new HttpParameter("oauth_token", oauthToken.getToken())); } final List<HttpParameter> signatureBaseParams = new ArrayList<HttpParameter>(oauthHeaderParams.size()); signatureBaseParams.addAll(oauthHeaderParams); parseGetParameters(sign_url, signatureBaseParams); final StringBuffer base = new StringBuffer(method) .append("&") .append(HttpParameter.encode(constructRequestURL(sign_url))) .append("&"); base.append(HttpParameter.encode(normalizeRequestParameters(signatureBaseParams))); final String oauthBaseString = base.toString(); final String signature = generateSignature(oauthBaseString, oauthToken); oauthHeaderParams.add(new HttpParameter("oauth_signature", signature)); return oauthHeaderParams; }
@Override public int hashCode() { int result = consumerKey != null ? consumerKey.hashCode() : 0; result = 31 * result + (consumerSecret != null ? consumerSecret.hashCode() : 0); result = 31 * result + (oauthToken != null ? oauthToken.hashCode() : 0); return result; }
@Override public boolean equals(final Object o) { if (this == o) return true; if (!(o instanceof OAuthSupport)) return false; final OAuthAuthorization that = (OAuthAuthorization) o; if (consumerKey != null ? !consumerKey.equals(that.consumerKey) : that.consumerKey != null) return false; if (consumerSecret != null ? !consumerSecret.equals(that.consumerSecret) : that.consumerSecret != null) return false; if (oauthToken != null ? !oauthToken.equals(that.oauthToken) : that.oauthToken != null) return false; return true; }
/* package */ String generateAuthorizationHeader( final String method, final String sign_url, HttpParameter[] params, final String nonce, final String timestamp, final OAuthToken otoken) { if (null == params) { params = new HttpParameter[0]; } final List<HttpParameter> oauthHeaderParams = new ArrayList<HttpParameter>(5); oauthHeaderParams.add(new HttpParameter("oauth_consumer_key", consumerKey)); oauthHeaderParams.add(OAUTH_SIGNATURE_METHOD); oauthHeaderParams.add(new HttpParameter("oauth_timestamp", timestamp)); oauthHeaderParams.add(new HttpParameter("oauth_nonce", nonce)); oauthHeaderParams.add(new HttpParameter("oauth_version", "1.0")); if (otoken != null) { oauthHeaderParams.add(new HttpParameter("oauth_token", otoken.getToken())); } final List<HttpParameter> signatureBaseParams = new ArrayList<HttpParameter>(oauthHeaderParams.size() + params.length); signatureBaseParams.addAll(oauthHeaderParams); if (!HttpParameter.containsFile(params)) { signatureBaseParams.addAll(toParamList(params)); } parseGetParameters(sign_url, signatureBaseParams); final StringBuffer base = new StringBuffer(method) .append("&") .append(HttpParameter.encode(constructRequestURL(sign_url))) .append("&"); base.append(HttpParameter.encode(normalizeRequestParameters(signatureBaseParams))); final String oauthBaseString = base.toString(); logger.debug("OAuth base string: ", oauthBaseString); final String signature = generateSignature(oauthBaseString, otoken); logger.debug("OAuth signature: ", signature); oauthHeaderParams.add(new HttpParameter("oauth_signature", signature)); // http://oauth.net/core/1.0/#rfc.section.9.1.1 if (realm != null) { oauthHeaderParams.add(new HttpParameter("realm", realm)); } return "OAuth " + encodeParameters(oauthHeaderParams, ",", true); }