{"action":"created","comment":{"url":"https://api.github.com/repos/kaaproject/kaa/pulls/comments/24079195","id":24079195,"diff_hunk":"@@ -0,0 +1,152 @@\n+package org.kaaproject.kaa.server.verifiers.facebook.verifier;\n+\n+import org.codehaus.jackson.map.ObjectMapper;\n+import org.kaaproject.kaa.server.common.verifier.AbstractKaaUserVerifier;\n+import org.kaaproject.kaa.server.common.verifier.UserVerifierCallback;\n+import org.kaaproject.kaa.server.common.verifier.UserVerifierContext;\n+import org.kaaproject.kaa.server.verifiers.facebook.config.gen.FacebookAvroConfig;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.io.BufferedReader;\n+import java.io.IOException;\n+import java.io.InputStreamReader;\n+import java.net.HttpURLConnection;\n+import java.net.MalformedURLException;\n+import java.net.URL;\n+import java.util.HashMap;\n+import java.util.concurrent.*;\n+\n+public class FacebookUserVerifier extends AbstractKaaUserVerifier<FacebookAvroConfig> {\n+    private static final Logger LOG = LoggerFactory.getLogger(FacebookUserVerifier.class);\n+    private static final String FACEBOOK_URL_PREFIX = \"https://graph.facebook.com/debug_token\";\n+    private FacebookAvroConfig configuration;\n+    private ExecutorService tokenVerifiersPool;\n+    private static ObjectMapper responseMapper;\n+\n+    @Override\n+    public void init(UserVerifierContext context, FacebookAvroConfig configuration) {\n+        LOG.info(\"Initializing user verifier with context {} and configuration {}\", context, configuration);\n+        this.configuration = configuration;\n+        tokenVerifiersPool = new ThreadPoolExecutor(0, configuration.getMaxParallelConnections(),\n+                            60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());\n+    }\n+\n+    @Override\n+    @SuppressWarnings(\"unchecked\")\n+    public void checkAccessToken(String userExternalId, String userAccessToken, UserVerifierCallback callback) {\n+        LOG.trace(\"Received user verification request for user {} and input token {}\", userExternalId, userAccessToken);\n+\n+        tokenVerifiersPool.submit(new TokenVerifier(userExternalId, userAccessToken, callback, configuration));\n+    }\n+\n+    private class TokenVerifier implements Runnable {\n+        String userExternalId;\n+        String userAccessToken;\n+        final UserVerifierCallback callback;\n+        final FacebookAvroConfig config;\n+\n+        public TokenVerifier(String userExternalId, String userAccessToken,\n+                             UserVerifierCallback callback, FacebookAvroConfig config) {\n+            this.userExternalId = userExternalId;\n+            this.userAccessToken = userAccessToken;\n+            this.callback = callback;\n+            this.config = config;\n+        }\n+\n+        @Override\n+        @SuppressWarnings(\"unchecked\")\n+        public void run() {\n+            String accessToken = config.getAppId() + \"|\" + config.getAppSecret();\n+            HttpURLConnection connection = null;\n+            BufferedReader reader = null;\n+\n+            try {\n+                connection = establishConnection(userAccessToken, accessToken);\n+\n+                responseMapper = new ObjectMapper();\n+\n+                // no data field means that token is invalid\n+                if (connection.getResponseCode() == 400) {\n+                    LOG.trace(\"400 Bad request\");\n+                    callback.onVerificationFailure(\"400 Bad request\");\n+                } else if (connection.getResponseCode() == 200) {\n+                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));\n+\n+                    // we always get a map\n+                    HashMap<String, Object> responseMap =\n+                            responseMapper.readValue(reader.readLine(), HashMap.class);\n+\n+                    HashMap<String, Object> dataMap = (HashMap) responseMap.get(\"data\");\n+                    String receivedUserId = (String) dataMap.get(\"user_id\");\n+                    if (receivedUserId == null) {\n+                        HashMap<String, Object> errorMap = (HashMap) dataMap.get(\"error\");\n+                        LOG.trace(\"Bad input token: {}, errcode = \", errorMap.get(\"message\"), errorMap.get(\"code\"));\n+                        callback.onVerificationFailure(\"Bad input token: \" + errorMap.get(\"message\") +\n+                                \", errcode = \" + errorMap.get(\"code\"));\n+                    } else if (!receivedUserId.equals(userExternalId)) {\n+                        LOG.trace(\"Input token {} doesn't belong to the user with {} id\", userAccessToken, userExternalId);\n+\n+                        callback.onVerificationFailure(\"User access token \" + userAccessToken + \" doesn't belong to the user\");\n+                    } else {\n+                        LOG.trace(\"Input token {} is confirmed and belongs to the user with {} id\",\n+                                userAccessToken, userExternalId);\n+\n+                        callback.onSuccess();\n+                    }\n+                } else {\n+                    LOG.trace(\"Server response code: {}, no data can be retrieved\" + connection.getResponseCode());\n+\n+                    callback.onVerificationFailure(\"Server response code:\" + connection.getResponseCode()\n+                            + \", no data can be retrieved\");\n+                }\n+            } catch (MalformedURLException e) {\n+                LOG.debug(e.toString());\n+\n+                // should be unreachable, as URL is correct\n+                callback.onVerificationFailure(\"Internal error: malformed url\");\n+            } catch (IOException e) {\n+                LOG.debug(e.toString());","path":"server/verifiers/facebook-verifier/verifier/src/main/java/org/kaaproject/kaa/server/verifiers/facebook/verifier/FacebookUserVerifier.java","position":109,"original_position":109,"commit_id":"09b5556e1fb306733d9b846d65b4bda25daa9f3d","original_commit_id":"09b5556e1fb306733d9b846d65b4bda25daa9f3d","user":{"login":"ashvayka","id":5016373,"avatar_url":"https://avatars.githubusercontent.com/u/5016373?v=3","gravatar_id":"","url":"https://api.github.com/users/ashvayka","html_url":"https://github.com/ashvayka","followers_url":"https://api.github.com/users/ashvayka/followers","following_url":"https://api.github.com/users/ashvayka/following{/other_user}","gists_url":"https://api.github.com/users/ashvayka/gists{/gist_id}","starred_url":"https://api.github.com/users/ashvayka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashvayka/subscriptions","organizations_url":"https://api.github.com/users/ashvayka/orgs","repos_url":"https://api.github.com/users/ashvayka/repos","events_url":"https://api.github.com/users/ashvayka/events{/privacy}","received_events_url":"https://api.github.com/users/ashvayka/received_events","type":"User","site_admin":false},"body":"LOG.debug(\"message\", e);","created_at":"2015-02-04T12:20:05Z","updated_at":"2015-02-04T12:20:05Z","html_url":"https://github.com/kaaproject/kaa/pull/83#discussion_r24079195","pull_request_url":"https://api.github.com/repos/kaaproject/kaa/pulls/83","_links":{"self":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/comments/24079195"},"html":{"href":"https://github.com/kaaproject/kaa/pull/83#discussion_r24079195"},"pull_request":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/83"}}},"pull_request":{"url":"https://api.github.com/repos/kaaproject/kaa/pulls/83","id":28576319,"html_url":"https://github.com/kaaproject/kaa/pull/83","diff_url":"https://github.com/kaaproject/kaa/pull/83.diff","patch_url":"https://github.com/kaaproject/kaa/pull/83.patch","issue_url":"https://api.github.com/repos/kaaproject/kaa/issues/83","number":83,"state":"open","locked":false,"title":"KAA-331: Implement facebook user verifier","user":{"login":"batytskyy","id":5807866,"avatar_url":"https://avatars.githubusercontent.com/u/5807866?v=3","gravatar_id":"","url":"https://api.github.com/users/batytskyy","html_url":"https://github.com/batytskyy","followers_url":"https://api.github.com/users/batytskyy/followers","following_url":"https://api.github.com/users/batytskyy/following{/other_user}","gists_url":"https://api.github.com/users/batytskyy/gists{/gist_id}","starred_url":"https://api.github.com/users/batytskyy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/batytskyy/subscriptions","organizations_url":"https://api.github.com/users/batytskyy/orgs","repos_url":"https://api.github.com/users/batytskyy/repos","events_url":"https://api.github.com/users/batytskyy/events{/privacy}","received_events_url":"https://api.github.com/users/batytskyy/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-03T18:29:05Z","updated_at":"2015-02-04T12:20:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c15d969c761f892f32ea9e46792f1b0435e4283c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/kaaproject/kaa/pulls/83/commits","review_comments_url":"https://api.github.com/repos/kaaproject/kaa/pulls/83/comments","review_comment_url":"https://api.github.com/repos/kaaproject/kaa/pulls/comments/{number}","comments_url":"https://api.github.com/repos/kaaproject/kaa/issues/83/comments","statuses_url":"https://api.github.com/repos/kaaproject/kaa/statuses/09b5556e1fb306733d9b846d65b4bda25daa9f3d","head":{"label":"kaaproject:feature/KAA-331","ref":"feature/KAA-331","sha":"09b5556e1fb306733d9b846d65b4bda25daa9f3d","user":{"login":"kaaproject","id":6984613,"avatar_url":"https://avatars.githubusercontent.com/u/6984613?v=3","gravatar_id":"","url":"https://api.github.com/users/kaaproject","html_url":"https://github.com/kaaproject","followers_url":"https://api.github.com/users/kaaproject/followers","following_url":"https://api.github.com/users/kaaproject/following{/other_user}","gists_url":"https://api.github.com/users/kaaproject/gists{/gist_id}","starred_url":"https://api.github.com/users/kaaproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaaproject/subscriptions","organizations_url":"https://api.github.com/users/kaaproject/orgs","repos_url":"https://api.github.com/users/kaaproject/repos","events_url":"https://api.github.com/users/kaaproject/events{/privacy}","received_events_url":"https://api.github.com/users/kaaproject/received_events","type":"Organization","site_admin":false},"repo":{"id":17839346,"name":"kaa","full_name":"kaaproject/kaa","owner":{"login":"kaaproject","id":6984613,"avatar_url":"https://avatars.githubusercontent.com/u/6984613?v=3","gravatar_id":"","url":"https://api.github.com/users/kaaproject","html_url":"https://github.com/kaaproject","followers_url":"https://api.github.com/users/kaaproject/followers","following_url":"https://api.github.com/users/kaaproject/following{/other_user}","gists_url":"https://api.github.com/users/kaaproject/gists{/gist_id}","starred_url":"https://api.github.com/users/kaaproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaaproject/subscriptions","organizations_url":"https://api.github.com/users/kaaproject/orgs","repos_url":"https://api.github.com/users/kaaproject/repos","events_url":"https://api.github.com/users/kaaproject/events{/privacy}","received_events_url":"https://api.github.com/users/kaaproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/kaaproject/kaa","description":"Kaa open-source middleware platform for building, managing, and integrating connected products with the Internet of Everything.","fork":false,"url":"https://api.github.com/repos/kaaproject/kaa","forks_url":"https://api.github.com/repos/kaaproject/kaa/forks","keys_url":"https://api.github.com/repos/kaaproject/kaa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kaaproject/kaa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kaaproject/kaa/teams","hooks_url":"https://api.github.com/repos/kaaproject/kaa/hooks","issue_events_url":"https://api.github.com/repos/kaaproject/kaa/issues/events{/number}","events_url":"https://api.github.com/repos/kaaproject/kaa/events","assignees_url":"https://api.github.com/repos/kaaproject/kaa/assignees{/user}","branches_url":"https://api.github.com/repos/kaaproject/kaa/branches{/branch}","tags_url":"https://api.github.com/repos/kaaproject/kaa/tags","blobs_url":"https://api.github.com/repos/kaaproject/kaa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kaaproject/kaa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kaaproject/kaa/git/refs{/sha}","trees_url":"https://api.github.com/repos/kaaproject/kaa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kaaproject/kaa/statuses/{sha}","languages_url":"https://api.github.com/repos/kaaproject/kaa/languages","stargazers_url":"https://api.github.com/repos/kaaproject/kaa/stargazers","contributors_url":"https://api.github.com/repos/kaaproject/kaa/contributors","subscribers_url":"https://api.github.com/repos/kaaproject/kaa/subscribers","subscription_url":"https://api.github.com/repos/kaaproject/kaa/subscription","commits_url":"https://api.github.com/repos/kaaproject/kaa/commits{/sha}","git_commits_url":"https://api.github.com/repos/kaaproject/kaa/git/commits{/sha}","comments_url":"https://api.github.com/repos/kaaproject/kaa/comments{/number}","issue_comment_url":"https://api.github.com/repos/kaaproject/kaa/issues/comments/{number}","contents_url":"https://api.github.com/repos/kaaproject/kaa/contents/{+path}","compare_url":"https://api.github.com/repos/kaaproject/kaa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kaaproject/kaa/merges","archive_url":"https://api.github.com/repos/kaaproject/kaa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kaaproject/kaa/downloads","issues_url":"https://api.github.com/repos/kaaproject/kaa/issues{/number}","pulls_url":"https://api.github.com/repos/kaaproject/kaa/pulls{/number}","milestones_url":"https://api.github.com/repos/kaaproject/kaa/milestones{/number}","notifications_url":"https://api.github.com/repos/kaaproject/kaa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kaaproject/kaa/labels{/name}","releases_url":"https://api.github.com/repos/kaaproject/kaa/releases{/id}","created_at":"2014-03-17T18:48:31Z","updated_at":"2015-02-03T18:43:40Z","pushed_at":"2015-02-04T12:05:08Z","git_url":"git://github.com/kaaproject/kaa.git","ssh_url":"git@github.com:kaaproject/kaa.git","clone_url":"https://github.com/kaaproject/kaa.git","svn_url":"https://github.com/kaaproject/kaa","homepage":"http://www.kaaproject.org","size":57532,"stargazers_count":73,"watchers_count":73,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":6,"mirror_url":null,"open_issues_count":4,"forks":6,"open_issues":4,"watchers":73,"default_branch":"master"}},"base":{"label":"kaaproject:feature/KAA-244","ref":"feature/KAA-244","sha":"2c2ca67e57ae6e8071549ec1076d92e37b760bff","user":{"login":"kaaproject","id":6984613,"avatar_url":"https://avatars.githubusercontent.com/u/6984613?v=3","gravatar_id":"","url":"https://api.github.com/users/kaaproject","html_url":"https://github.com/kaaproject","followers_url":"https://api.github.com/users/kaaproject/followers","following_url":"https://api.github.com/users/kaaproject/following{/other_user}","gists_url":"https://api.github.com/users/kaaproject/gists{/gist_id}","starred_url":"https://api.github.com/users/kaaproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaaproject/subscriptions","organizations_url":"https://api.github.com/users/kaaproject/orgs","repos_url":"https://api.github.com/users/kaaproject/repos","events_url":"https://api.github.com/users/kaaproject/events{/privacy}","received_events_url":"https://api.github.com/users/kaaproject/received_events","type":"Organization","site_admin":false},"repo":{"id":17839346,"name":"kaa","full_name":"kaaproject/kaa","owner":{"login":"kaaproject","id":6984613,"avatar_url":"https://avatars.githubusercontent.com/u/6984613?v=3","gravatar_id":"","url":"https://api.github.com/users/kaaproject","html_url":"https://github.com/kaaproject","followers_url":"https://api.github.com/users/kaaproject/followers","following_url":"https://api.github.com/users/kaaproject/following{/other_user}","gists_url":"https://api.github.com/users/kaaproject/gists{/gist_id}","starred_url":"https://api.github.com/users/kaaproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaaproject/subscriptions","organizations_url":"https://api.github.com/users/kaaproject/orgs","repos_url":"https://api.github.com/users/kaaproject/repos","events_url":"https://api.github.com/users/kaaproject/events{/privacy}","received_events_url":"https://api.github.com/users/kaaproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/kaaproject/kaa","description":"Kaa open-source middleware platform for building, managing, and integrating connected products with the Internet of Everything.","fork":false,"url":"https://api.github.com/repos/kaaproject/kaa","forks_url":"https://api.github.com/repos/kaaproject/kaa/forks","keys_url":"https://api.github.com/repos/kaaproject/kaa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kaaproject/kaa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kaaproject/kaa/teams","hooks_url":"https://api.github.com/repos/kaaproject/kaa/hooks","issue_events_url":"https://api.github.com/repos/kaaproject/kaa/issues/events{/number}","events_url":"https://api.github.com/repos/kaaproject/kaa/events","assignees_url":"https://api.github.com/repos/kaaproject/kaa/assignees{/user}","branches_url":"https://api.github.com/repos/kaaproject/kaa/branches{/branch}","tags_url":"https://api.github.com/repos/kaaproject/kaa/tags","blobs_url":"https://api.github.com/repos/kaaproject/kaa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kaaproject/kaa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kaaproject/kaa/git/refs{/sha}","trees_url":"https://api.github.com/repos/kaaproject/kaa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kaaproject/kaa/statuses/{sha}","languages_url":"https://api.github.com/repos/kaaproject/kaa/languages","stargazers_url":"https://api.github.com/repos/kaaproject/kaa/stargazers","contributors_url":"https://api.github.com/repos/kaaproject/kaa/contributors","subscribers_url":"https://api.github.com/repos/kaaproject/kaa/subscribers","subscription_url":"https://api.github.com/repos/kaaproject/kaa/subscription","commits_url":"https://api.github.com/repos/kaaproject/kaa/commits{/sha}","git_commits_url":"https://api.github.com/repos/kaaproject/kaa/git/commits{/sha}","comments_url":"https://api.github.com/repos/kaaproject/kaa/comments{/number}","issue_comment_url":"https://api.github.com/repos/kaaproject/kaa/issues/comments/{number}","contents_url":"https://api.github.com/repos/kaaproject/kaa/contents/{+path}","compare_url":"https://api.github.com/repos/kaaproject/kaa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kaaproject/kaa/merges","archive_url":"https://api.github.com/repos/kaaproject/kaa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kaaproject/kaa/downloads","issues_url":"https://api.github.com/repos/kaaproject/kaa/issues{/number}","pulls_url":"https://api.github.com/repos/kaaproject/kaa/pulls{/number}","milestones_url":"https://api.github.com/repos/kaaproject/kaa/milestones{/number}","notifications_url":"https://api.github.com/repos/kaaproject/kaa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kaaproject/kaa/labels{/name}","releases_url":"https://api.github.com/repos/kaaproject/kaa/releases{/id}","created_at":"2014-03-17T18:48:31Z","updated_at":"2015-02-03T18:43:40Z","pushed_at":"2015-02-04T12:05:08Z","git_url":"git://github.com/kaaproject/kaa.git","ssh_url":"git@github.com:kaaproject/kaa.git","clone_url":"https://github.com/kaaproject/kaa.git","svn_url":"https://github.com/kaaproject/kaa","homepage":"http://www.kaaproject.org","size":57532,"stargazers_count":73,"watchers_count":73,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":6,"mirror_url":null,"open_issues_count":4,"forks":6,"open_issues":4,"watchers":73,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/83"},"html":{"href":"https://github.com/kaaproject/kaa/pull/83"},"issue":{"href":"https://api.github.com/repos/kaaproject/kaa/issues/83"},"comments":{"href":"https://api.github.com/repos/kaaproject/kaa/issues/83/comments"},"review_comments":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/83/comments"},"review_comment":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/kaaproject/kaa/pulls/83/commits"},"statuses":{"href":"https://api.github.com/repos/kaaproject/kaa/statuses/09b5556e1fb306733d9b846d65b4bda25daa9f3d"}}}}