/** * Creates an OAuth 1.0 protected resource request interceptor. * @param accessToken the access token and secret */ public OAuth1RequestInterceptor(OAuth1Credentials oauth1Credentials) { this.oauth1Credentials = oauth1Credentials; this.signingUtils = new SigningSupport(); }
private String buildAuthorizationHeaderValue(URI tokenUrl, Map<String, String> tokenParameters, MultiValueMap<String, String> additionalParameters, String tokenSecret) { Map<String, String> oauthParameters = signingUtils.commonOAuthParameters(consumerKey); oauthParameters.putAll(tokenParameters); if (additionalParameters == null) { additionalParameters = EmptyMultiValueMap.instance(); } return signingUtils.buildAuthorizationHeaderValue(HttpMethod.POST, tokenUrl, oauthParameters, additionalParameters, consumerSecret, tokenSecret); }
private static String oauthEncode(String param) { try { // See http://tools.ietf.org/html/rfc5849#section-3.6 byte[] bytes = encode(param.getBytes(UTF8_CHARSET_NAME), UNRESERVED); return new String(bytes, "US-ASCII"); } catch (Exception shouldntHappen) { throw new IllegalStateException(shouldntHappen); } }
/** * Builds an authorization header from a request. * Expects that the request's query parameters are form-encoded. */ public String buildAuthorizationHeaderValue(HttpRequest request, byte[] body, OAuth1Credentials oauth1Credentials) { Map<String, String> oauthParameters = commonOAuthParameters(oauth1Credentials.getConsumerKey()); oauthParameters.put("oauth_token", oauth1Credentials.getAccessToken()); MultiValueMap<String, String> additionalParameters = union(readFormParameters(request.getHeaders().getContentType(), body), parseFormParameters(request.getURI().getRawQuery())); return buildAuthorizationHeaderValue(request.getMethod(), request.getURI(), oauthParameters, additionalParameters, oauth1Credentials.getConsumerSecret(), oauth1Credentials.getAccessTokenSecret()); }
/** * Builds the authorization header. * The elements in additionalParameters are expected to not be encoded. */ public String buildAuthorizationHeaderValue(HttpMethod method, URI targetUrl, Map<String, String> oauthParameters, MultiValueMap<String, String> additionalParameters, String consumerSecret, String tokenSecret) { StringBuilder header = new StringBuilder(); header.append("OAuth "); for (Entry<String, String> entry : oauthParameters.entrySet()) { header.append(oauthEncode(entry.getKey())).append("=\"").append(oauthEncode(entry.getValue())).append("\", "); } MultiValueMap<String, String> collectedParameters = new LinkedMultiValueMap<String, String>((int) ((oauthParameters.size() + additionalParameters.size()) / .75 + 1)); collectedParameters.setAll(oauthParameters); collectedParameters.putAll(additionalParameters); String baseString = buildBaseString(method, getBaseStringUri(targetUrl), collectedParameters); String signature = calculateSignature(baseString, consumerSecret, tokenSecret); header.append(oauthEncode("oauth_signature")).append("=\"").append(oauthEncode(signature)).append("\""); return header.toString(); }
String buildBaseString(HttpMethod method, String targetUrl, MultiValueMap<String, String> collectedParameters) { StringBuilder builder = new StringBuilder(); builder.append(method.name()).append('&').append(oauthEncode(targetUrl)).append('&'); builder.append(oauthEncode(normalizeParameters(collectedParameters))); return builder.toString(); }
private String getBaseStringUri(URI uri) { try { // see: http://tools.ietf.org/html/rfc5849#section-3.4.1.2 return new URI(uri.getScheme(), null, uri.getHost(), getPort(uri), uri.getPath(), null, null).toString(); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
private String getAuthorizationHeaderValue(HttpRequest request, byte[] body) { return signingUtils.buildAuthorizationHeaderValue(request, body, oauth1Credentials); }
private MultiValueMap<String, String> parseFormParameters(String parameterString) { if (parameterString == null || parameterString.length() == 0) { return EmptyMultiValueMap.instance(); } String[] pairs = StringUtils.tokenizeToStringArray(parameterString, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(formDecode(pair), ""); } else { String name = formDecode(pair.substring(0, idx)); String value = formDecode(pair.substring(idx + 1)); result.add(name, value); } } return result; }
/** * Builds an authorization header from a request. * Expects that the request's query parameters are form-encoded. */ public String buildAuthorizationHeaderValue(HttpRequest request, byte[] body, OAuth1Credentials oauth1Credentials) { Map<String, String> oauthParameters = commonOAuthParameters(oauth1Credentials.getConsumerKey()); oauthParameters.put("oauth_token", oauth1Credentials.getAccessToken()); MultiValueMap<String, String> additionalParameters = union(readFormParameters(request.getHeaders().getContentType(), body), parseFormParameters(request.getURI().getRawQuery())); return buildAuthorizationHeaderValue(request.getMethod(), request.getURI(), oauthParameters, additionalParameters, oauth1Credentials.getConsumerSecret(), oauth1Credentials.getAccessTokenSecret()); }
/** * Builds the authorization header. * The elements in additionalParameters are expected to not be encoded. */ public String buildAuthorizationHeaderValue(HttpMethod method, URI targetUrl, Map<String, String> oauthParameters, MultiValueMap<String, String> additionalParameters, String consumerSecret, String tokenSecret) { StringBuilder header = new StringBuilder(); header.append("OAuth "); for (Entry<String, String> entry : oauthParameters.entrySet()) { header.append(oauthEncode(entry.getKey())).append("=\"").append(oauthEncode(entry.getValue())).append("\", "); } MultiValueMap<String, String> collectedParameters = new LinkedMultiValueMap<String, String>((int) ((oauthParameters.size() + additionalParameters.size()) / .75 + 1)); collectedParameters.setAll(oauthParameters); collectedParameters.putAll(additionalParameters); String baseString = buildBaseString(method, getBaseStringUri(targetUrl), collectedParameters); String signature = calculateSignature(baseString, consumerSecret, tokenSecret); header.append(oauthEncode("oauth_signature")).append("=\"").append(oauthEncode(signature)).append("\""); return header.toString(); }
String buildBaseString(HttpMethod method, String targetUrl, MultiValueMap<String, String> collectedParameters) { StringBuilder builder = new StringBuilder(); builder.append(method.name()).append('&').append(oauthEncode(targetUrl)).append('&'); builder.append(oauthEncode(normalizeParameters(collectedParameters))); return builder.toString(); }
private String getBaseStringUri(URI uri) { try { // see: http://tools.ietf.org/html/rfc5849#section-3.4.1.2 return new URI(uri.getScheme(), null, uri.getHost(), getPort(uri), uri.getPath(), null, null).toString(); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
private String getAuthorizationHeaderValue(HttpRequest request, byte[] body) { return signingUtils.buildAuthorizationHeaderValue(request, body, oauth1Credentials); }
private MultiValueMap<String, String> parseFormParameters(String parameterString) { if (parameterString == null || parameterString.length() == 0) { return EmptyMultiValueMap.instance(); } String[] pairs = StringUtils.tokenizeToStringArray(parameterString, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(formDecode(pair), ""); } else { String name = formDecode(pair.substring(0, idx)); String value = formDecode(pair.substring(idx + 1)); result.add(name, value); } } return result; }
private String buildAuthorizationHeaderValue(URI tokenUrl, Map<String, String> tokenParameters, MultiValueMap<String, String> additionalParameters, String tokenSecret) { Map<String, String> oauthParameters = signingUtils.commonOAuthParameters(consumerKey); oauthParameters.putAll(tokenParameters); if (additionalParameters == null) { additionalParameters = EmptyMultiValueMap.instance(); } return signingUtils.buildAuthorizationHeaderValue(HttpMethod.POST, tokenUrl, oauthParameters, additionalParameters, consumerSecret, tokenSecret); }
/** * Creates an OAuth 1.0 protected resource request interceptor. * @param accessToken the access token and secret */ public OAuth1RequestInterceptor(OAuth1Credentials oauth1Credentials) { this.oauth1Credentials = oauth1Credentials; this.signingUtils = new SigningSupport(); }
private static String oauthEncode(String param) { try { // See http://tools.ietf.org/html/rfc5849#section-3.6 byte[] bytes = encode(param.getBytes(UTF8_CHARSET_NAME), UNRESERVED); return new String(bytes, "US-ASCII"); } catch (Exception shouldntHappen) { throw new IllegalStateException(shouldntHappen); } }
public OAuth1Template(String consumerKey, String consumerSecret, String requestTokenUrl, String authorizeUrl, String authenticateUrl, String accessTokenUrl, OAuth1Version version) { Assert.notNull(consumerKey, "The consumerKey property cannot be null"); Assert.notNull(consumerSecret, "The consumerSecret property cannot be null"); Assert.notNull(requestTokenUrl, "The requestTokenUrl property cannot be null"); Assert.notNull(authorizeUrl, "The authorizeUrl property cannot be null"); Assert.notNull(accessTokenUrl, "The accessTokenUrl property cannot be null"); this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; this.requestTokenUrl = encodeTokenUri(requestTokenUrl); this.authorizeUrl = authorizeUrl; this.authenticateUrl = authenticateUrl; this.accessTokenUrl = encodeTokenUri(accessTokenUrl); this.version = version; this.restTemplate = createRestTemplate(); this.signingUtils = new SigningSupport(); }
public OAuth1Template(String consumerKey, String consumerSecret, String requestTokenUrl, String authorizeUrl, String authenticateUrl, String accessTokenUrl, OAuth1Version version) { Assert.notNull(consumerKey, "The consumerKey property cannot be null"); Assert.notNull(consumerSecret, "The consumerSecret property cannot be null"); Assert.notNull(requestTokenUrl, "The requestTokenUrl property cannot be null"); Assert.notNull(authorizeUrl, "The authorizeUrl property cannot be null"); Assert.notNull(accessTokenUrl, "The accessTokenUrl property cannot be null"); this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; this.requestTokenUrl = encodeTokenUri(requestTokenUrl); this.authorizeUrl = authorizeUrl; this.authenticateUrl = authenticateUrl; this.accessTokenUrl = encodeTokenUri(accessTokenUrl); this.version = version; this.restTemplate = createRestTemplate(); this.signingUtils = new SigningSupport(); }