OAuthParameters params = new OAuthParameters(); params.readRequest(request); String consumerKey = params.getConsumerKey(); OAuthSecrets secrets = new OAuthSecrets(); secrets.setConsumerSecret(m_tokenStore.getToken(consumerKey)); String timestampStr = params.getTimestamp(); if (timestampStr == null) if(!OAuthSignature.verify(request, params, secrets))
OAuthParameters params = new OAuthParameters().consumerKey(input.oauthConsumerKey).signatureMethod("HMAC-SHA1").version("1.0"); OAuthSecrets secrets = new OAuthSecrets().consumerSecret(input.oauthConsumerSecret); client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
/** * Verifies the OAuth signature for a given request, parameters and * secrets. * * @param request the request to verify the signature from. * @param params the OAuth authorization parameters * @param secrets the secrets used to verify the OAuth signature. * @return true if the signature is verified. * @throws OAuthSignatureException if an error occurred generating the signature. */ public static boolean verify(OAuthRequest request, OAuthParameters params, OAuthSecrets secrets) throws OAuthSignatureException { return getSignatureMethod(params).verify(elements(request, params), secrets, params.getSignature()); }
/** * Retrieves an instance of a signature method that can be used to generate * or verify signatures for data. * * @return the retrieved signatured method. * @throws UnsupportedSignatureMethodException if signature method not supported. */ private static OAuthSignatureMethod getSignatureMethod(OAuthParameters params) throws UnsupportedSignatureMethodException { OAuthSignatureMethod method = Methods.getInstance(params.getSignatureMethod()); if (method == null) { throw new UnsupportedSignatureMethodException(params.getSignatureMethod()); } return method; }
/** * Generates and returns an OAuth signature for the given request, * parameters and secrets. * * @param request the request to generate signature for. * @param params the OAuth authorization parameters. * @param secrets the secrets used to generate the OAuth signature. * @return the OAuth digital signature. * @throws OAuthSignatureException if an error occurred generating the signature. */ public static String generate(OAuthRequest request, OAuthParameters params, OAuthSecrets secrets) throws OAuthSignatureException { return getSignatureMethod(params).sign(elements(request, params), secrets); }
/** * Builder pattern method to return {@link OAuthParameters} after setting * timestamp to the current time. * * @return this parameters object. */ public OAuthParameters timestamp() { setTimestamp(); return this; }
/** * Returns the consumer key. */ public String getConsumerKey() { return get(CONSUMER_KEY); }
/** * Builder pattern method to return {@link OAuthParameters} after setting * nonce to a randomly-generated UUID. * * @return this parameters object. */ public OAuthParameters nonce() { setNonce(); return this; }
/** * Builder pattern method to return {@link OAuthParameters} after setting * protection realm. * * @param realm the protection realm for the request. * @return this parameters object. */ public OAuthParameters realm(String realm) { setRealm(realm); return this; }
/** * Sets the nonce, a value that should be unique for a given timestamp. */ public void setNonce(String nonce) { put(NONCE, nonce); }
/** * Sets the protocol version to the default value of 1.0. */ public void setVersion() { setVersion("1.0"); }
/** * Builder pattern method to return {@link OAuthParameters} after setting * verifier code. * * @param verifier the verifier code. * @return this parameters object. */ public OAuthParameters verifier(String verifier) { setVerifier(verifier); return this; }
/** * Builder pattern method to return {@link Secrets} after setting * consumer secret. * * @param consumer the consumer secret. */ public OAuthSecrets tokenSecret(String tokenSecret) { setTokenSecret(tokenSecret); return this; } }
/** * Converts a byte array into a Base64 encoded string. * * @param data bytes to encode. * @return base64 encoding of data; 4 chars for every 3 bytes. */ public static String encode(byte[] data) { return encode(data, 0, data.length); }
/** * Verifies the OAuth signature for a given request, parameters and * secrets. * * @param request the request to verify the signature from. * @param params the OAuth authorization parameters * @param secrets the secrets used to verify the OAuth signature. * @return true if the signature is verified. * @throws OAuthSignatureException if an error occurred generating the signature. */ public static boolean verify(OAuthRequest request, OAuthParameters params, OAuthSecrets secrets) throws OAuthSignatureException { return getSignatureMethod(params).verify(elements(request, params), secrets, params.getSignature()); }
/** * Builder pattern method to return {@link Parameters} after setting * timestamp. * * @param timestamp positive integer, typically number of seconds since epoch. * @return this parameters object. */ public OAuthParameters timestamp(String timestamp) { setTimestamp(timestamp); return this; }
/** * Returns the signature for the request. */ public String getSignature() { return get(SIGNATURE); }
/** * Builder pattern method to return {@link Parameters} after setting * protection realm. * * @param realm the protection realm for the request. * @return this parameters object. */ public OAuthParameters realm(String realm) { setRealm(realm); return this; }
/** * Sets the consumer key. */ public void setConsumerKey(String consumerKey) { put(CONSUMER_KEY, consumerKey); }
/** * Returns the protocol version. */ public String getVersion() { return get(VERSION); }