Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: guywithnose/facebook-java-sdk
base: 543fba2140
...
head fork: guywithnose/facebook-java-sdk
compare: 082164ab3c
Checking mergeability… Don't worry, you can still create the pull request.
  • 10 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
38 src/facebook/BaseFacebook.java
@@ -4,8 +4,10 @@
*/
package facebook;
+import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.SocketTimeoutException;
+import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -88,7 +90,7 @@
/**
* A CSRF state variable to assist in the defense against CSRF attacks.
*/
- protected String state;
+ protected String state = null;
/**
* The OAuth access token received in exchange for a valid authorization code.
@@ -466,9 +468,9 @@ public String getLoginUrl(HashMap<String, String> params)
{
establishCSRFTokenState();
String currentUrl = getCurrentUrl();
-
params.put("client_id", getAppId());
- params.put("redirect_uri", currentUrl);
+ if (!params.containsKey("redirect_uri"))
+ params.put("redirect_uri", currentUrl);
params.put("state", state);
return getUrl("www", "dialog/oauth", params);
@@ -497,12 +499,9 @@ public String getLogoutUrl()
*/
public String getLogoutUrl(HashMap<String, String> params)
{
- /*
- * TODO Translate return getUrl( 'www', 'logout.php', array_merge(array(
- * 'next' => getCurrentUrl(), 'access_token' => getAccessToken(), ),
- * $params) );
- */
- return null;
+ params.put("next",getCurrentUrl());
+ params.put("access_token",getAccessToken());
+ return getUrl( "www", "logout.php", params );
}
/**
@@ -534,10 +533,12 @@ public String getLoginStatusUrl(HashMap<String, String> params)
{
params.put("api_key", getAppId());
params.put("no_user", getCurrentUrl());
- params.put("no_session", getCurrentUrl());
- params.put("ok_session", getCurrentUrl());
+ if (!params.containsKey("no_session"))
+ params.put("no_session", getCurrentUrl());
+ if (!params.containsKey("ok_session"))
+ params.put("ok_session", getCurrentUrl());
params.put("session_version", "3");
- return getUrl( "www", "extern/login_status.php", params);
+ return getUrl("www", "extern/login_status.php", params);
}
/**
@@ -1282,7 +1283,13 @@ private String http_build_query(HashMap<String, String> params)
{
query.append(key);
query.append("=");
- query.append(params.get(key));
+ try
+ {
+ query.append(URLEncoder.encode(params.get(key), "ISO-8859-1"));
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
query.append("&");
}
query.deleteCharAt(query.length() - 1);
@@ -1300,7 +1307,8 @@ protected String getCurrentUrl()
String currentUrl = req.getRequestURL().toString();
String query = req.getQueryString();
if (query != null)
- { // drop known fb params
+ {
+ // drop known fb params
String[] params = query.split("&");
ArrayList<String> retained_params = new ArrayList<String>();
for (String param : params)
@@ -1311,6 +1319,7 @@ protected String getCurrentUrl()
}
}
+ query = null;
if (retained_params.size() > 0)
{
query = "?";
@@ -1324,7 +1333,6 @@ protected String getCurrentUrl()
query += queryBuilder.toString();
}
}
-
// use port if non default
int port = req.getServerPort();
if ((port == 80 && "http".equals(req.getProtocol()))
View
461 testsrc/facebook/tests/facebookTest.java
@@ -2,6 +2,9 @@
import static org.junit.Assert.*;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
@@ -18,6 +21,7 @@
import facebook.tests.helpers.FBCode;
import facebook.tests.helpers.FBGetCurrentURLFacebook;
import facebook.tests.helpers.FBGetSignedRequestCookieFacebook;
+import facebook.tests.helpers.FBPublic;
import facebook.tests.helpers.HttpServletRequestMock;
import facebook.tests.helpers.TransientFacebook;
@@ -619,201 +623,237 @@ public void testGraphAPI_OnlyParams() throws FacebookApiException
assertFalse("User\"s verification status should only be "
+ "available with a valid access token.", response.has("verified"));
}
-
- /**
- * Tests the loginURL method using defaults.
- */
- @Test
- public void testLoginURL_Defaults() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $encodedUrl = rawurlencode("http://fbrell.com/examples");
- assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
- "Expect the current url to exist.");
- */
- }
-
- /**
- * Tests the loginURL method using defaults drop state query param.
- */
- @Test
- public void testLoginURL_DefaultsDropStateQueryParam() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples?state=xx42xx";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $expectEncodedUrl = rawurlencode("http://fbrell.com/examples");
- assertTrue(strpos($facebook->getLoginUrl(), $expectEncodedUrl) > -1,
- "Expect the current url to exist.");
- assertFalse(strpos($facebook->getLoginUrl(), "xx42xx"),
- "Expect the session param to be dropped.");
- */
- }
-
- /**
- * Tests the loginURL method using defaults drop code query param.
- */
- @Test
- public void testLoginURL_DefaultsDropCodeQueryParam() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples?code=xx42xx";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $expectEncodedUrl = rawurlencode("http://fbrell.com/examples");
- assertTrue(strpos($facebook->getLoginUrl(), $expectEncodedUrl) > -1,
- "Expect the current url to exist.");
- assertFalse(strpos($facebook->getLoginUrl(), "xx42xx"),
- "Expect the session param to be dropped.");
- */
- }
-
- /**
- * Tests the loginURL method using defaults drop signed request param but
- * not others.
- */
- @Test
- public void testLoginURL_DefaultsDropSignedRequestParamButNotOthers() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] =
- "/examples?signed_request=xx42xx&do_not_drop=xx43xx";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $expectEncodedUrl = rawurlencode("http://fbrell.com/examples");
- assertFalse(strpos($facebook->getLoginUrl(), "xx42xx"),
- "Expect the session param to be dropped.");
- assertTrue(strpos($facebook->getLoginUrl(), "xx43xx") > -1,
- "Expect the do_not_drop param to exist.");
- */
- }
-
- /**
- * Tests the loginURL method using custom next.
- */
- @Test
- public void testLoginURL_CustomNext() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $next = "http://fbrell.com/custom";
- $loginUrl = $facebook->getLoginUrl(array(
- "redirect_uri" => $next,
- "cancel_url" => $next
- ));
- $currentEncodedUrl = rawurlencode("http://fbrell.com/examples");
- $expectedEncodedUrl = rawurlencode($next);
- assertNotNull(strpos($loginUrl, $expectedEncodedUrl),
- "Expect the custom url to exist.");
- assertFalse(strpos($loginUrl, $currentEncodedUrl),
- "Expect the current url to not exist.");
- */
- }
-
- /**
- * Tests the logoutURL method using defaults.
- */
- @Test
- public void testLogoutURL_Defaults() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $encodedUrl = rawurlencode("http://fbrell.com/examples");
- assertNotNull(strpos($facebook->getLogoutUrl(), $encodedUrl),
- "Expect the current url to exist.");
- */
- }
-
- /**
- * Tests the loginStatusURL method using defaults.
- */
- @Test
- public void testLoginStatusURL_Defaults() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $encodedUrl = rawurlencode("http://fbrell.com/examples");
- assertNotNull(strpos($facebook->getLoginStatusUrl(), $encodedUrl),
- "Expect the current url to exist.");
- */
- }
-
- /**
- * Tests the loginStatusURL method using custom.
- */
- @Test
- public void testLoginStatusURL_Custom() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $encodedUrl1 = rawurlencode("http://fbrell.com/examples");
- $okUrl = "http://fbrell.com/here1";
- $encodedUrl2 = rawurlencode($okUrl);
- $loginStatusUrl = $facebook->getLoginStatusUrl(array(
- "ok_session" => $okUrl,
- ));
- assertNotNull(strpos($loginStatusUrl, $encodedUrl1),
- "Expect the current url to exist.");
- assertNotNull(strpos($loginStatusUrl, $encodedUrl2),
- "Expect the custom url to exist.");
- */
- }
-
- /**
- * Tests the getLoginUrl method using non default port.
- */
- @Test
- public void testGetLoginUrl_NonDefaultPort() {
- fail("Not implemented.");
- /* TODO Translate
- $_SERVER["HTTP_HOST"] = "fbrell.com:8080";
- $_SERVER["REQUEST_URI"] = "/examples";
- $facebook = new TransientFacebook(array(
- "appId" => self::APP_ID,
- "secret" => self::SECRET,
- ));
- $encodedUrl = rawurlencode("http://fbrell.com:8080/examples");
- assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
- "Expect the current url to exist.");
- */
+
+ /**
+ * Tests the loginURL method using defaults.
+ */
+ @Test
+ public void testLoginURL_Defaults()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples");
+ String encodedUrl = "";
+ try
+ {
+ encodedUrl = URLEncoder.encode("http://fbrell.com/examples", "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertTrue("Expect the current url to exist.", facebook.getLoginUrl()
+ .indexOf(encodedUrl) != -1);
+ }
+
+ /**
+ * Tests the loginURL method using defaults drop state query param.
+ */
+ @Test
+ public void testLoginURL_DefaultsDropStateQueryParam()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples?state=xx42xx");
+ String expectEncodedUrl = "";
+ try
+ {
+ expectEncodedUrl = URLEncoder.encode("http://fbrell.com/examples",
+ "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook.getLoginUrl()
+ .indexOf(expectEncodedUrl) == -1);
+ assertTrue("Expect the session param to be dropped.", facebook
+ .getLoginUrl().indexOf("xx42xx") == -1);
+ }
+
+ /**
+ * Tests the loginURL method using defaults drop code query param.
+ */
+ @Test
+ public void testLoginURL_DefaultsDropCodeQueryParam()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples?code=xx42xx");
+ String expectEncodedUrl = "";
+ try
+ {
+ expectEncodedUrl = URLEncoder.encode("http://fbrell.com/examples",
+ "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook.getLoginUrl()
+ .indexOf(expectEncodedUrl) == -1);
+ assertTrue("Expect the session param to be dropped.", facebook
+ .getLoginUrl().indexOf("xx42xx") == -1);
+ }
+
+ /**
+ * Tests the loginURL method using defaults drop signed request param but not
+ * others.
+ */
+ @Test
+ public void testLoginURL_DefaultsDropSignedRequestParamButNotOthers()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples?signed_request=xx42xx&do_not_drop=xx43xx");
+ String expectEncodedUrl = "";
+ try
+ {
+ expectEncodedUrl = URLEncoder.encode(
+ "http://fbrell.com/examples?do_not_drop=xx43xx", "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook.getLoginUrl()
+ .indexOf(expectEncodedUrl) == -1);
+ assertTrue("Expect the session param to be dropped.", facebook
+ .getLoginUrl().indexOf("xx42xx") == -1);
+ assertFalse("Expect the session param to be dropped.", facebook
+ .getLoginUrl().indexOf("xx43xx") == -1);
+ }
+
+ /**
+ * Tests the loginURL method using custom next.
+ */
+ @Test
+ public void testLoginURL_CustomNext()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples");
+ String next = "http://fbrell.com/custom";
+ String loginUrl = facebook.getLoginUrl(new HashMap<String, String>()
+ {
+ {
+ put("redirect_uri", "http://fbrell.com/custom");
+ put("cancel_url", "http://fbrell.com/custom");
}
-
+ });
+ String currentEncodedUrl = "";
+ String expectedEncodedUrl = "";
+ try
+ {
+ currentEncodedUrl = URLEncoder.encode("http://fbrell.com/examples",
+ "ISO-8859-1");
+ expectedEncodedUrl = URLEncoder.encode(next, "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the custom url to exist.",
+ loginUrl.indexOf(expectedEncodedUrl) == -1);
+ assertTrue("Expect the current url to not exist.",
+ loginUrl.indexOf(currentEncodedUrl) == -1);
+ }
+
+ /**
+ * Tests the logoutURL method using defaults.
+ */
+ @Test
+ public void testLogoutURL_Defaults()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples");
+ String encodedUrl = "";
+ try
+ {
+ encodedUrl = URLEncoder
+ .encode("http://fbrell.com/examples", "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook.getLogoutUrl()
+ .indexOf(encodedUrl) == -1);
+ }
+
+ /**
+ * Tests the loginStatusURL method using defaults.
+ */
+ @Test
+ public void testLoginStatusURL_Defaults()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples");
+ String encodedUrl = "";
+ try
+ {
+ encodedUrl = URLEncoder
+ .encode("http://fbrell.com/examples", "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook
+ .getLoginStatusUrl().indexOf(encodedUrl) == -1);
+ }
+
+ /**
+ * Tests the loginStatusURL method using custom.
+ */
+ @Test
+ public void testLoginStatusURL_Custom()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com/examples");
+ String encodedUrl1 = "";
+ String encodedUrl2 = "";
+ String okUrl = "http://fbrell.com/here1";
+ try
+ {
+ encodedUrl1 = URLEncoder.encode("http://fbrell.com/examples",
+ "ISO-8859-1");
+ encodedUrl2 = URLEncoder.encode(okUrl, "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ String loginStatusUrl = facebook
+ .getLoginStatusUrl(new HashMap<String, String>()
+ {
+ {
+ put("ok_session", "http://fbrell.com/here1");
+ }
+ });
+ assertFalse("Expect the current url to exist.",
+ loginStatusUrl.indexOf(encodedUrl1) == -1);
+ assertFalse("Expect the custom url to exist.",
+ loginStatusUrl.indexOf(encodedUrl2) == -1);
+ }
+
+ /**
+ * Tests the getLoginUrl method using non default port.
+ */
+ @Test
+ public void testGetLoginUrl_NonDefaultPort()
+ {
+ HttpServletRequestMock req = new HttpServletRequestMock();
+ TransientFacebook facebook = new TransientFacebook(config, req);
+ req.setRequestString("http://fbrell.com:8080/examples");
+ String encodedUrl = "";
+ try
+ {
+ encodedUrl = URLEncoder.encode("http://fbrell.com:8080/examples",
+ "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+ assertFalse("Expect the current url to exist.", facebook.getLoginUrl()
+ .indexOf(encodedUrl) == 1);
+ }
+
/**
* Tests the getLoginUrl method using secure current url.
*/
@@ -883,21 +923,19 @@ public void testAppSecretCall() {
}
*/
}
-
- /**
- * Tests the base64UrlEncode method.
- */
- @Test
- public void testBase64UrlEncode() {
- fail("Not implemented.");
- /* TODO Translate
- $input = "Facebook rocks";
- $output = "RmFjZWJvb2sgcm9ja3M";
- assertEquals(FBPublic::publicBase64UrlDecode($output), $input);
- */
- }
-
+ /**
+ * Tests the base64UrlEncode method.
+ */
+ @Test
+ public void testBase64UrlEncode()
+ {
+ String input = "Facebook rocks";
+ String output = "RmFjZWJvb2sgcm9ja3M";
+
+ assertEquals(input, FBPublic.publicBase64UrlDecode(output));
+ }
+
/**
* Tests the signedToken method.
*/
@@ -1140,6 +1178,13 @@ protected void assertIsSubset(HashMap<String, String> correct, HashMap<String, S
*/
protected HashMap<String, String> parse_str(String query)
{
+ try
+ {
+ query = URLDecoder.decode(query, "ISO-8859-1");
+ } catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
HashMap<String, String> params = new HashMap<String, String>();
for(String param : query.split("&"))
{
View
7 testsrc/facebook/tests/helpers/HttpServletRequestMock.java
@@ -479,12 +479,7 @@ public int getServerPort()
int colonIndex = url.indexOf(":", start);
if(colonIndex < slashIndex && colonIndex != -1)
{
- System.out.println(start);
- System.out.println(slashIndex);
- System.out.println(colonIndex);
- System.out.println(url);
- System.out.println(url.substring(start+colonIndex, slashIndex-colonIndex));
- return Integer.valueOf(url.substring(start+colonIndex, slashIndex-colonIndex));
+ return Integer.valueOf(url.substring(colonIndex+1, slashIndex));
}
if("https".equals(getProtocol()))
{
View
2  testsrc/facebook/tests/helpers/TransientFacebook.java
@@ -50,7 +50,7 @@ protected void setPersistentData(String key, String value)
@Override
protected String getPersistentData(String key)
{
- return getPersistentData(key, "false");
+ return getPersistentData(key, null);
}
/**

No commit comments for this range

Something went wrong with that request. Please try again.