11 Commits
11 changed files with 85 additions and 31 deletions

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2010 Pablo Fenandez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<packaging>jar</packaging>
<version>1.0.1</version>
<version>1.0.5</version>
<name>OAuth Library</name>
<dependencies>
@@ -5,12 +5,12 @@ public class GoogleApi extends DefaultApi10a
@Override
public String getAccessTokenEndpoint()
{
return "https://www.google.com/accounts/OAuthGetRequestToken";
return "https://www.google.com/accounts/OAuthGetAccessToken";
}
@Override
public String getRequestTokenEndpoint()
{
return "https://www.google.com/accounts/OAuthGetAccessToken";
return "https://www.google.com/accounts/OAuthGetRequestToken";
}
}
@@ -0,0 +1,16 @@
package org.scribe.builder.api;
public class VimeoApi extends DefaultApi10a
{
@Override
public String getAccessTokenEndpoint()
{
return "http://vimeo.com/oauth/access_token";
}
@Override
public String getRequestTokenEndpoint()
{
return "http://vimeo.com/oauth/request_token";
}
}
@@ -8,20 +8,24 @@ import org.scribe.utils.*;
public class HeaderExtractorImpl implements HeaderExtractor
{
private static final String PARAM_SEPARATOR = ", ";
private static final String PREAMBLE = "OAuth ";
public String extract(OAuthRequest request)
{
checkPreconditions(request);
Map<String, String> parameters = request.getOauthParameters();
StringBuffer header = new StringBuffer();
StringBuffer header = new StringBuffer(parameters.size() * 20);
header.append(PREAMBLE);
for (String key : parameters.keySet())
{
addToHeader(header, key, parameters.get(key));
if(header.length() > PREAMBLE.length())
{
header.append(PARAM_SEPARATOR);
}
header.append(String.format("%s=\"%s\"", key, URLUtils.percentEncode(parameters.get(key))));
}
return removeTrail(header);
return header.toString();
}
private void checkPreconditions(OAuthRequest request)
@@ -34,13 +38,4 @@ public class HeaderExtractorImpl implements HeaderExtractor
}
}
private void addToHeader(StringBuffer header, String name, String value)
{
header.append(String.format("%s=\"%s\", ", name, URLUtils.percentEncode(value)));
}
private String removeTrail(StringBuffer header)
{
return header.toString().substring(0, header.length() - 2);
}
}
@@ -1,10 +1,13 @@
package org.scribe.utils;
import java.util.regex.Pattern;
import org.scribe.model.OAuthConstants;
public class Preconditions
{
private static final String DEFAULT_MESSAGE = "Received an invalid parameter";
private static final Pattern URL_PATTERN = Pattern.compile("[a-zA-Z_-]+://\\S+");
public static void checkNotNull(Object object, String errorMsg)
{
@@ -33,12 +36,12 @@ public class Preconditions
static boolean isUrl(String url)
{
return url.startsWith("http://") || url.startsWith("https://");
return URL_PATTERN.matcher(url).matches();
}
private static void check(boolean requirements, String error)
{
String message = (error == null || error.trim().isEmpty()) ? DEFAULT_MESSAGE : error;
String message = (error == null || error.trim().length() <= 0) ? DEFAULT_MESSAGE : error;
if (!requirements)
{
throw new IllegalArgumentException(message);
+10 -10
View File
@@ -24,17 +24,16 @@ public class URLUtils
private static String doFormUrlEncode(Map<String, String> map)
{
StringBuffer encodedString = new StringBuffer();
StringBuffer encodedString = new StringBuffer(map.size() * 20);
for (String key : map.keySet())
{
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key))).append(PARAM_SEPARATOR);
if(encodedString.length() > 0)
{
encodedString.append(PARAM_SEPARATOR);
}
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
}
return removeTrailingSeparator(encodedString);
}
private static String removeTrailingSeparator(StringBuffer buffer)
{
return buffer.toString().substring(0, buffer.length() - 1);
return encodedString.toString();
}
public static String percentEncode(String string)
@@ -42,8 +41,9 @@ public class URLUtils
Preconditions.checkNotNull(string, "Cannot encode null string");
try
{
return URLEncoder.encode(string, UTF_8);
} catch (UnsupportedEncodingException uee)
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20");
}
catch (UnsupportedEncodingException uee)
{
throw new OAuthException(ERROR_MSG, uee);
}
@@ -41,4 +41,12 @@ public class BaseStringExtractorTest
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
extractor.extract(request);
}
@Test
public void shouldProperlyEncodeSpaces()
{
String expected = "GET&http%3A%2F%2Fexample.com&body%3Dthis%2520param%2520has%2520whitespace%26oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E*%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
request.addBodyParameter("body", "this param has whitespace");
assertEquals(expected, extractor.extract(request));
}
}
@@ -52,7 +52,7 @@ public class RequestTest
postRequest.addBodyParameter("param", "value");
postRequest.addBodyParameter("param two", "value with spaces");
postRequest.send();
assertEquals("param+two=value+with+spaces&param=value", postRequest.getBodyContents());
assertEquals("param%20two=value%20with%20spaces&param=value", postRequest.getBodyContents());
assertTrue(connection.getHeaders().containsKey("Content-Length"));
}
@@ -53,6 +53,17 @@ public class PreconditionsTest
Preconditions.checkValidUrl("https://www.example.com", ERROR_MSG);
}
public void shouldAllowNonStandarProtocolsForAndroid()
{
Preconditions.checkValidUrl("x-url-custom://www.example.com", ERROR_MSG);
}
@Test(expected = IllegalArgumentException.class)
public void shouldNotAllowStrangeProtocolNames()
{
Preconditions.checkValidUrl("$weird*://www.example.com", ERROR_MSG);
}
public void shouldAllowOutOfBandAsValidCallbackValue()
{
Preconditions.checkValidOAuthCallback("oob", ERROR_MSG);
@@ -23,7 +23,7 @@ public class URLUtilsTest
params.put("key with spaces", "value with spaces");
params.put("&symbols!", "#!");
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@@ -39,7 +39,7 @@ public class URLUtilsTest
public void shouldPercentEncodeString()
{
String toEncode = "this is a test &^";
String expected = "this+is+a+test+%26%5E";
String expected = "this%20is%20a%20test%20%26%5E";
assertEquals(expected, URLUtils.percentEncode(toEncode));
}