From aa4ecf87c4bd2ff8c2ba73f789f7c9405214fdee Mon Sep 17 00:00:00 2001 From: Max Rasche Date: Tue, 11 Jan 2022 11:22:58 +0100 Subject: [PATCH] granular_scopes added to DebugTokenInfo --- src/main/java/com/restfb/FacebookClient.java | 50 +++++++++++++++++++ .../com/restfb/types/DebugTokenInfoTest.java | 18 +++++++ .../resources/json/debug-token-info-3.json | 25 ++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/test/resources/json/debug-token-info-3.json diff --git a/src/main/java/com/restfb/FacebookClient.java b/src/main/java/com/restfb/FacebookClient.java index e19721d7c..367ae1263 100644 --- a/src/main/java/com/restfb/FacebookClient.java +++ b/src/main/java/com/restfb/FacebookClient.java @@ -776,6 +776,12 @@ class DebugTokenInfo extends AbstractFacebookType { @Facebook private List scopes = new ArrayList<>(); + /** + * List of granular permissions that the user has granted for this app in this access token. + */ + @Facebook("granular_scopes") + private List granularScopes = new ArrayList<>(); + @Facebook private String type; @@ -851,6 +857,15 @@ public List getScopes() { return unmodifiableList(scopes); } + /** + * List of granular scopes the access token 'contains' + * + * @return list of granular scopes + */ + public List getGranularScopes() { + return unmodifiableList(granularScopes); + } + /** * General metadata associated with the access token. Can contain data like 'sso', 'auth_type', 'auth_nonce' * @@ -874,6 +889,41 @@ public String getType() { } } + class GranularScope extends AbstractFacebookType { + + private static final long serialVersionUID = 1L; + + /** + * The permission granted by the user. + */ + @Facebook + private String scope; + + /** + * The target ids of Pages, Groups, or business assets the user granted the above permission for. + */ + @Facebook("target_ids") + private List targetIds = new ArrayList<>(); + + /** + * The permission granted by the user. + * + * @return The permission granted by the user. + */ + public String getScope() { + return scope; + } + + /** + * The target ids of Pages, Groups, or business assets the user granted the above permission for. + * + * @return The target ids of Pages, Groups, or business assets the user granted the above permission for. + */ + public List getTargetIds() { + return unmodifiableList(targetIds); + } + } + class DebugTokenError extends AbstractFacebookType { private static final long serialVersionUID = 1L; diff --git a/src/test/java/com/restfb/types/DebugTokenInfoTest.java b/src/test/java/com/restfb/types/DebugTokenInfoTest.java index 5b8223b40..118d993fb 100644 --- a/src/test/java/com/restfb/types/DebugTokenInfoTest.java +++ b/src/test/java/com/restfb/types/DebugTokenInfoTest.java @@ -63,4 +63,22 @@ void withExpires() { assertEquals("1234567890", exampleDebugTokenInfo.getAppId()); assertEquals(1561330800000L, exampleDebugTokenInfo.getExpiresAt().getTime()); } + + @Test + void testJsonWithGranularScopes() { + FacebookClient.DebugTokenInfo exampleDebugTokenInfo = + createJsonMapper().toJavaObject(jsonFromClasspath("debug-token-info-3"), FacebookClient.DebugTokenInfo.class); + assertNotNull(exampleDebugTokenInfo); + assertNotNull(exampleDebugTokenInfo.getGranularScopes()); + assertEquals(2, exampleDebugTokenInfo.getGranularScopes().size()); + + assertEquals("pages_show_list", exampleDebugTokenInfo.getGranularScopes().get(0).getScope()); + assertNotNull(exampleDebugTokenInfo.getGranularScopes().get(0).getTargetIds()); + assertEquals(0, exampleDebugTokenInfo.getGranularScopes().get(0).getTargetIds().size()); + + assertEquals("instagram_basic", exampleDebugTokenInfo.getGranularScopes().get(1).getScope()); + assertNotNull(exampleDebugTokenInfo.getGranularScopes().get(1).getTargetIds()); + assertEquals(1, exampleDebugTokenInfo.getGranularScopes().get(1).getTargetIds().size()); + assertEquals("{connected-ig-user-id}", exampleDebugTokenInfo.getGranularScopes().get(1).getTargetIds().get(0)); + } } diff --git a/src/test/resources/json/debug-token-info-3.json b/src/test/resources/json/debug-token-info-3.json new file mode 100644 index 000000000..66a25347b --- /dev/null +++ b/src/test/resources/json/debug-token-info-3.json @@ -0,0 +1,25 @@ +{ + "app_id": "{app-id}", + "type": "USER", + "application": "{app-name}", + "data_access_expires_at": 1649504517, + "expires_at": 1641733200, + "is_valid": true, + "scopes": [ + "pages_show_list", + "instagram_basic", + "public_profile" + ], + "granular_scopes": [ + { + "scope": "pages_show_list" + }, + { + "scope": "instagram_basic", + "target_ids": [ + "{connected-ig-user-id}" + ] + } + ], + "user_id": "{user-id}" +}