Data from signed request is propageted in Exception, when token is missing
authorKai Moritz <kai@juplo.de>
Fri, 28 Mar 2014 13:24:04 +0000 (14:24 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 6 Oct 2015 06:22:25 +0000 (08:22 +0200)
src/main/java/de/juplo/facebook/SignedRequestAwareAuthorizationCodeAccessTokenProvider.java
src/main/java/de/juplo/facebook/SignedRequestAwareUserRedirectRequiredException.java [new file with mode: 0644]

index 8d4c7ee..5df98b7 100644 (file)
@@ -142,34 +142,20 @@ public class SignedRequestAwareAuthorizationCodeAccessTokenProvider
         throw redirect;
       }
 
-      DefaultOAuth2AccessToken token = null;
+      /**
+       * Extract additional information and store it in the token
+       * See:
+       * https://developers.facebook.com/docs/reference/login/signed-request/
+       * TODO:
+       * - Attribute "code"
+       */
+      Map<String,Object> additionalInformation = new HashMap<>();
       try
       {
-        String value = json.get("oauth_token").asText();
-        if (value.isEmpty())
-        {
-          log.error("field \"oauth_token\" is missing: {}", data);
-          throw redirect;
-        }
-        token = new DefaultOAuth2AccessToken(value);
-        token.setExpiration(new Date(json.get("expires").getLongValue()*1000L));
-
-        /**
-         * Extract additional information and store it in the token
-         * See:
-         * https://developers.facebook.com/docs/reference/login/signed-request/
-         * TODO:
-         * - Attribute "code"
-         */
-        Map<String,Object> additionalInformation = new HashMap<>();
         additionalInformation.put(
             "issued_at",
             new Date(json.get("issued_at").getLongValue()*1000L)
             );
-        additionalInformation.put(
-            "user_id",
-            json.get("user_id").asText()
-            );
         Map<String,Object> user = new HashMap<>();
         user.put(
             "country",
@@ -199,6 +185,29 @@ public class SignedRequestAwareAuthorizationCodeAccessTokenProvider
           page.put("admin", json.get("page").get("admin").asBoolean());
           additionalInformation.put("page", page);
         }
+      }
+      catch (NullPointerException e)
+      {
+        log.warn("expected additional data is missing: {}", data);
+      }
+
+      DefaultOAuth2AccessToken token = null;
+      try
+      {
+        String value = json.get("oauth_token").asText();
+        if (value.isEmpty())
+        {
+          log.error("field \"oauth_token\" is missing: {}", data);
+          throw redirect;
+        }
+        token = new DefaultOAuth2AccessToken(value);
+        token.setExpiration(new Date(json.get("expires").getLongValue()*1000L));
+
+        additionalInformation.put(
+            "user_id",
+            json.get("user_id").asText()
+            );
+
         token.setAdditionalInformation(additionalInformation);
       }
       catch (NullPointerException e)
diff --git a/src/main/java/de/juplo/facebook/SignedRequestAwareUserRedirectRequiredException.java b/src/main/java/de/juplo/facebook/SignedRequestAwareUserRedirectRequiredException.java
new file mode 100644 (file)
index 0000000..7bad046
--- /dev/null
@@ -0,0 +1,35 @@
+package de.juplo.facebook;
+
+
+import java.util.Map;
+import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
+
+
+
+/**
+ *
+ * @author kai
+ */
+public class SignedRequestAwareUserRedirectRequiredException
+    extends
+      UserRedirectRequiredException
+{
+  private final Map<String,Object> signedRequestData;
+
+
+  public SignedRequestAwareUserRedirectRequiredException(
+      String redirectUri,
+      Map<String,String> requestParams,
+      Map<String,Object> signedRequestData
+      )
+  {
+    super(redirectUri, requestParams);
+    this.signedRequestData = signedRequestData;
+  }
+
+
+  public Map<String, Object> getSignedRequestData()
+  {
+    return signedRequestData;
+  }
+}