From 2dedc05085f743cc4f8c312b3aa0b7846f28d357 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 28 Mar 2014 14:24:04 +0100 Subject: [PATCH] Data from signed request is propageted in Exception, when token is missing --- ...eAuthorizationCodeAccessTokenProvider.java | 53 +++++++++++-------- ...estAwareUserRedirectRequiredException.java | 35 ++++++++++++ 2 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 src/main/java/de/juplo/facebook/SignedRequestAwareUserRedirectRequiredException.java diff --git a/src/main/java/de/juplo/facebook/SignedRequestAwareAuthorizationCodeAccessTokenProvider.java b/src/main/java/de/juplo/facebook/SignedRequestAwareAuthorizationCodeAccessTokenProvider.java index 8d4c7ee..5df98b7 100644 --- a/src/main/java/de/juplo/facebook/SignedRequestAwareAuthorizationCodeAccessTokenProvider.java +++ b/src/main/java/de/juplo/facebook/SignedRequestAwareAuthorizationCodeAccessTokenProvider.java @@ -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 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 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 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 index 0000000..7bad046 --- /dev/null +++ b/src/main/java/de/juplo/facebook/SignedRequestAwareUserRedirectRequiredException.java @@ -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 signedRequestData; + + + public SignedRequestAwareUserRedirectRequiredException( + String redirectUri, + Map requestParams, + Map signedRequestData + ) + { + super(redirectUri, requestParams); + this.signedRequestData = signedRequestData; + } + + + public Map getSignedRequestData() + { + return signedRequestData; + } +} -- 2.20.1