{"action":"created","comment":{"url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/comments/23420682","id":23420682,"diff_hunk":"@@ -0,0 +1,221 @@\n+#!/usr/bin/python2.4\n+#\n+# Copyright 2015 Google Inc. All Rights Reserved.\n+\n+\"\"\"AppRTC GCM ID Registration\n+\n+This module implements user ID registration and verification.\n+\"\"\"\n+\n+import collections\n+import constants\n+import datetime\n+import json\n+import logging\n+import webapp2\n+\n+from google.appengine.ext import ndb\n+\n+def get_ancestor_key():\n+  return ndb.Key('GCMRecord', 'global')\n+\n+PARAM_USER_ID = 'userId'\n+PARAM_GCM_ID = 'gcmId'\n+PARAM_CODE = 'code'\n+PARAM_OLD_GCM_ID = 'oldGcmId'\n+PARAM_NEW_GCM_ID = 'newGcmId'\n+PARAM_USER_ID_LIST = 'userIdList'\n+\n+def has_msg_field(msg, field):\n+  return msg and field in msg and \\\n+      (not isinstance(msg[field], collections.Iterable) or \\\n+       len(msg[field]) > 0)\n+\n+def has_msg_fields(msg, fields):\n+  return reduce(lambda x, y: x and y,\n+                map(lambda x: has_msg_field(msg, x), fields))\n+\n+class BindPage(webapp2.RequestHandler):\n+  def get_message_from_json(self):\n+    try:\n+      message = json.loads(self.request.body)\n+      if isinstance(message, dict):\n+        return message\n+      logging.warning('Expected dictionary message'\n+          + ', request=' + self.request.body)\n+      return None\n+    except Exception as e:\n+      logging.warning('JSON load error from BindPage: error=' + str(e)\n+          + ', request=' + self.request.body)\n+      return None\n+\n+  def handle_new(self):\n+    msg = self.get_message_from_json()\n+    if has_msg_fields(msg, (PARAM_USER_ID, PARAM_GCM_ID)):\n+      # TODO(jiayl): validate the input, generate a random code, and send SMS.\n+      self.response.out.write(GCMRecord.add_or_update(\n+          msg[PARAM_USER_ID], msg[PARAM_GCM_ID], 'fake_code'))\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+  def handle_update(self):\n+    msg = self.get_message_from_json()\n+    if has_msg_fields(msg, (PARAM_USER_ID, PARAM_OLD_GCM_ID, PARAM_NEW_GCM_ID)):\n+      self.response.out.write(GCMRecord.update_gcm_id(\n+          msg[PARAM_USER_ID], msg[PARAM_OLD_GCM_ID], msg[PARAM_NEW_GCM_ID]))\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+  def handle_verify(self):\n+    msg = self.get_message_from_json()\n+    if has_msg_fields(msg, (PARAM_USER_ID, PARAM_GCM_ID, PARAM_CODE)):\n+      self.response.out.write(GCMRecord.verify(\n+          msg[PARAM_USER_ID], msg[PARAM_GCM_ID], msg[PARAM_CODE]))\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+  def handle_query(self):\n+    msg = self.get_message_from_json()\n+    if has_msg_field(msg, PARAM_USER_ID_LIST) and \\\n+        isinstance(msg[PARAM_USER_ID_LIST], list):\n+      result = []\n+      for id in msg[PARAM_USER_ID_LIST]:\n+        # TODO(jiayl): Only return the verified users when SMS verification is\n+        # added.\n+        if len(GCMRecord.get_by_user_id(id)) > 0:\n+          result.append(id)\n+      self.response.out.write(json.dumps(result))\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+  def handle_del(self):\n+    msg = self.get_message_from_json()\n+    if has_msg_fields(msg, [PARAM_USER_ID, PARAM_GCM_ID]):\n+      GCMRecord.remove(msg[PARAM_USER_ID], msg[PARAM_GCM_ID])\n+      self.response.out.write(constants.RESPONSE_SUCCESS)\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+  def post(self, cmd):\n+    if cmd == 'new':\n+      self.handle_new()\n+    elif cmd == 'update':\n+      self.handle_update()\n+    elif cmd == 'verify':\n+      self.handle_verify()\n+    elif cmd == 'del':\n+      self.handle_del()\n+    elif cmd == 'query':\n+      self.handle_query()\n+    else:\n+      self.response.out.write(constants.RESPONSE_INVALID_ARGUMENT)\n+\n+class GCMRecord(ndb.Model):\n+  \"\"\"Models a user ID to GCM ID binding record.\"\"\"\n+  user_id = ndb.StringProperty()\n+  gcm_id = ndb.StringProperty()\n+  code = ndb.StringProperty()\n+  code_sent_time = ndb.DateTimeProperty()\n+  verified = ndb.BooleanProperty()\n+  last_modified_time = ndb.DateTimeProperty()\n+\n+  @classmethod\n+  def get_by_user_id(cls, user_id):\n+    q = GCMRecord.query(ancestor=get_ancestor_key())\n+    return q.filter(GCMRecord.user_id == user_id).fetch()\n+\n+  @classmethod\n+  def get_by_gcm_id(cls, gcm_id):\n+    q = GCMRecord.query(ancestor=get_ancestor_key())\n+    return q.filter(GCMRecord.gcm_id == gcm_id).fetch()\n+\n+  @classmethod\n+  def get_by_ids(cls, user_id, gcm_id):\n+    q = GCMRecord.query(ancestor=get_ancestor_key()) \\\n+        .filter(GCMRecord.user_id == user_id).filter(GCMRecord.gcm_id == gcm_id)\n+    return q.fetch()\n+\n+  @classmethod\n+  @ndb.transactional(retries=100)\n+  def add_or_update(cls, user_id, gcm_id, code):\n+    records = GCMRecord.get_by_ids(user_id, gcm_id)\n+    now = datetime.datetime.utcnow()\n+    if len(records) == 0:\n+      record = GCMRecord(parent = get_ancestor_key(),\n+                         user_id = user_id,\n+                         gcm_id = gcm_id,\n+                         code = code,\n+                         verified = False,\n+                         code_sent_time = now,\n+                         last_modified_time = now)\n+      record.put()\n+      logging.info(\n+          'GCM binding added, user_id=%s, gcm_id=%s' % (user_id, gcm_id))\n+      return constants.RESPONSE_CODE_SENT\n+\n+    assert len(records) == 1\n+    if records[0].verified:\n+      logging.warning('Cannot update GCM binding code since already verified, '\\","path":"samples/web/content/apprtc/gcm_register.py","position":158,"original_position":158,"commit_id":"852b1d63800977238bd1d2c9a83799b200714715","original_commit_id":"852b1d63800977238bd1d2c9a83799b200714715","user":{"login":"juberti","id":1821693,"avatar_url":"https://avatars.githubusercontent.com/u/1821693?v=3","gravatar_id":"","url":"https://api.github.com/users/juberti","html_url":"https://github.com/juberti","followers_url":"https://api.github.com/users/juberti/followers","following_url":"https://api.github.com/users/juberti/following{/other_user}","gists_url":"https://api.github.com/users/juberti/gists{/gist_id}","starred_url":"https://api.github.com/users/juberti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juberti/subscriptions","organizations_url":"https://api.github.com/users/juberti/orgs","repos_url":"https://api.github.com/users/juberti/repos","events_url":"https://api.github.com/users/juberti/events{/privacy}","received_events_url":"https://api.github.com/users/juberti/received_events","type":"User","site_admin":false},"body":"Won't we still need to update the GCM ID post-verification? (e.g. on app upgrade)","created_at":"2015-01-22T23:36:30Z","updated_at":"2015-01-22T23:36:30Z","html_url":"https://github.com/GoogleChrome/webrtc/pull/406#discussion_r23420682","pull_request_url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406","_links":{"self":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/comments/23420682"},"html":{"href":"https://github.com/GoogleChrome/webrtc/pull/406#discussion_r23420682"},"pull_request":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406"}}},"pull_request":{"url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406","id":27785700,"html_url":"https://github.com/GoogleChrome/webrtc/pull/406","diff_url":"https://github.com/GoogleChrome/webrtc/pull/406.diff","patch_url":"https://github.com/GoogleChrome/webrtc/pull/406.patch","issue_url":"https://api.github.com/repos/GoogleChrome/webrtc/issues/406","number":406,"state":"open","locked":false,"title":"Add GCM register module","user":{"login":"tkchin","id":9094670,"avatar_url":"https://avatars.githubusercontent.com/u/9094670?v=3","gravatar_id":"","url":"https://api.github.com/users/tkchin","html_url":"https://github.com/tkchin","followers_url":"https://api.github.com/users/tkchin/followers","following_url":"https://api.github.com/users/tkchin/following{/other_user}","gists_url":"https://api.github.com/users/tkchin/gists{/gist_id}","starred_url":"https://api.github.com/users/tkchin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tkchin/subscriptions","organizations_url":"https://api.github.com/users/tkchin/orgs","repos_url":"https://api.github.com/users/tkchin/repos","events_url":"https://api.github.com/users/tkchin/events{/privacy}","received_events_url":"https://api.github.com/users/tkchin/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-21T19:08:04Z","updated_at":"2015-01-22T23:36:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3b9a5aa66875e2513ac5e5230ee351c6a36562b8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406/commits","review_comments_url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406/comments","review_comment_url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/comments/{number}","comments_url":"https://api.github.com/repos/GoogleChrome/webrtc/issues/406/comments","statuses_url":"https://api.github.com/repos/GoogleChrome/webrtc/statuses/852b1d63800977238bd1d2c9a83799b200714715","head":{"label":"tkchin:register","ref":"register","sha":"852b1d63800977238bd1d2c9a83799b200714715","user":{"login":"tkchin","id":9094670,"avatar_url":"https://avatars.githubusercontent.com/u/9094670?v=3","gravatar_id":"","url":"https://api.github.com/users/tkchin","html_url":"https://github.com/tkchin","followers_url":"https://api.github.com/users/tkchin/followers","following_url":"https://api.github.com/users/tkchin/following{/other_user}","gists_url":"https://api.github.com/users/tkchin/gists{/gist_id}","starred_url":"https://api.github.com/users/tkchin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tkchin/subscriptions","organizations_url":"https://api.github.com/users/tkchin/orgs","repos_url":"https://api.github.com/users/tkchin/repos","events_url":"https://api.github.com/users/tkchin/events{/privacy}","received_events_url":"https://api.github.com/users/tkchin/received_events","type":"User","site_admin":false},"repo":{"id":25009856,"name":"webrtc","full_name":"tkchin/webrtc","owner":{"login":"tkchin","id":9094670,"avatar_url":"https://avatars.githubusercontent.com/u/9094670?v=3","gravatar_id":"","url":"https://api.github.com/users/tkchin","html_url":"https://github.com/tkchin","followers_url":"https://api.github.com/users/tkchin/followers","following_url":"https://api.github.com/users/tkchin/following{/other_user}","gists_url":"https://api.github.com/users/tkchin/gists{/gist_id}","starred_url":"https://api.github.com/users/tkchin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tkchin/subscriptions","organizations_url":"https://api.github.com/users/tkchin/orgs","repos_url":"https://api.github.com/users/tkchin/repos","events_url":"https://api.github.com/users/tkchin/events{/privacy}","received_events_url":"https://api.github.com/users/tkchin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tkchin/webrtc","description":"WebRTC demos and samples","fork":true,"url":"https://api.github.com/repos/tkchin/webrtc","forks_url":"https://api.github.com/repos/tkchin/webrtc/forks","keys_url":"https://api.github.com/repos/tkchin/webrtc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tkchin/webrtc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tkchin/webrtc/teams","hooks_url":"https://api.github.com/repos/tkchin/webrtc/hooks","issue_events_url":"https://api.github.com/repos/tkchin/webrtc/issues/events{/number}","events_url":"https://api.github.com/repos/tkchin/webrtc/events","assignees_url":"https://api.github.com/repos/tkchin/webrtc/assignees{/user}","branches_url":"https://api.github.com/repos/tkchin/webrtc/branches{/branch}","tags_url":"https://api.github.com/repos/tkchin/webrtc/tags","blobs_url":"https://api.github.com/repos/tkchin/webrtc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tkchin/webrtc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tkchin/webrtc/git/refs{/sha}","trees_url":"https://api.github.com/repos/tkchin/webrtc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tkchin/webrtc/statuses/{sha}","languages_url":"https://api.github.com/repos/tkchin/webrtc/languages","stargazers_url":"https://api.github.com/repos/tkchin/webrtc/stargazers","contributors_url":"https://api.github.com/repos/tkchin/webrtc/contributors","subscribers_url":"https://api.github.com/repos/tkchin/webrtc/subscribers","subscription_url":"https://api.github.com/repos/tkchin/webrtc/subscription","commits_url":"https://api.github.com/repos/tkchin/webrtc/commits{/sha}","git_commits_url":"https://api.github.com/repos/tkchin/webrtc/git/commits{/sha}","comments_url":"https://api.github.com/repos/tkchin/webrtc/comments{/number}","issue_comment_url":"https://api.github.com/repos/tkchin/webrtc/issues/comments/{number}","contents_url":"https://api.github.com/repos/tkchin/webrtc/contents/{+path}","compare_url":"https://api.github.com/repos/tkchin/webrtc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tkchin/webrtc/merges","archive_url":"https://api.github.com/repos/tkchin/webrtc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tkchin/webrtc/downloads","issues_url":"https://api.github.com/repos/tkchin/webrtc/issues{/number}","pulls_url":"https://api.github.com/repos/tkchin/webrtc/pulls{/number}","milestones_url":"https://api.github.com/repos/tkchin/webrtc/milestones{/number}","notifications_url":"https://api.github.com/repos/tkchin/webrtc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tkchin/webrtc/labels{/name}","releases_url":"https://api.github.com/repos/tkchin/webrtc/releases{/id}","created_at":"2014-10-09T21:44:09Z","updated_at":"2015-01-22T19:12:10Z","pushed_at":"2015-01-22T22:17:35Z","git_url":"git://github.com/tkchin/webrtc.git","ssh_url":"git@github.com:tkchin/webrtc.git","clone_url":"https://github.com/tkchin/webrtc.git","svn_url":"https://github.com/tkchin/webrtc","homepage":"http://googlechrome.github.io/webrtc/","size":2859,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"GoogleChrome:apprtc-t","ref":"apprtc-t","sha":"ea723faefce3aae2542ab762112f1edf249c34c9","user":{"login":"GoogleChrome","id":1778935,"avatar_url":"https://avatars.githubusercontent.com/u/1778935?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleChrome","html_url":"https://github.com/GoogleChrome","followers_url":"https://api.github.com/users/GoogleChrome/followers","following_url":"https://api.github.com/users/GoogleChrome/following{/other_user}","gists_url":"https://api.github.com/users/GoogleChrome/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleChrome/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleChrome/subscriptions","organizations_url":"https://api.github.com/users/GoogleChrome/orgs","repos_url":"https://api.github.com/users/GoogleChrome/repos","events_url":"https://api.github.com/users/GoogleChrome/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleChrome/received_events","type":"Organization","site_admin":false},"repo":{"id":12970971,"name":"webrtc","full_name":"GoogleChrome/webrtc","owner":{"login":"GoogleChrome","id":1778935,"avatar_url":"https://avatars.githubusercontent.com/u/1778935?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleChrome","html_url":"https://github.com/GoogleChrome","followers_url":"https://api.github.com/users/GoogleChrome/followers","following_url":"https://api.github.com/users/GoogleChrome/following{/other_user}","gists_url":"https://api.github.com/users/GoogleChrome/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleChrome/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleChrome/subscriptions","organizations_url":"https://api.github.com/users/GoogleChrome/orgs","repos_url":"https://api.github.com/users/GoogleChrome/repos","events_url":"https://api.github.com/users/GoogleChrome/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleChrome/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/GoogleChrome/webrtc","description":"WebRTC demos and samples","fork":false,"url":"https://api.github.com/repos/GoogleChrome/webrtc","forks_url":"https://api.github.com/repos/GoogleChrome/webrtc/forks","keys_url":"https://api.github.com/repos/GoogleChrome/webrtc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/GoogleChrome/webrtc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/GoogleChrome/webrtc/teams","hooks_url":"https://api.github.com/repos/GoogleChrome/webrtc/hooks","issue_events_url":"https://api.github.com/repos/GoogleChrome/webrtc/issues/events{/number}","events_url":"https://api.github.com/repos/GoogleChrome/webrtc/events","assignees_url":"https://api.github.com/repos/GoogleChrome/webrtc/assignees{/user}","branches_url":"https://api.github.com/repos/GoogleChrome/webrtc/branches{/branch}","tags_url":"https://api.github.com/repos/GoogleChrome/webrtc/tags","blobs_url":"https://api.github.com/repos/GoogleChrome/webrtc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/GoogleChrome/webrtc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/GoogleChrome/webrtc/git/refs{/sha}","trees_url":"https://api.github.com/repos/GoogleChrome/webrtc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/GoogleChrome/webrtc/statuses/{sha}","languages_url":"https://api.github.com/repos/GoogleChrome/webrtc/languages","stargazers_url":"https://api.github.com/repos/GoogleChrome/webrtc/stargazers","contributors_url":"https://api.github.com/repos/GoogleChrome/webrtc/contributors","subscribers_url":"https://api.github.com/repos/GoogleChrome/webrtc/subscribers","subscription_url":"https://api.github.com/repos/GoogleChrome/webrtc/subscription","commits_url":"https://api.github.com/repos/GoogleChrome/webrtc/commits{/sha}","git_commits_url":"https://api.github.com/repos/GoogleChrome/webrtc/git/commits{/sha}","comments_url":"https://api.github.com/repos/GoogleChrome/webrtc/comments{/number}","issue_comment_url":"https://api.github.com/repos/GoogleChrome/webrtc/issues/comments/{number}","contents_url":"https://api.github.com/repos/GoogleChrome/webrtc/contents/{+path}","compare_url":"https://api.github.com/repos/GoogleChrome/webrtc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/GoogleChrome/webrtc/merges","archive_url":"https://api.github.com/repos/GoogleChrome/webrtc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/GoogleChrome/webrtc/downloads","issues_url":"https://api.github.com/repos/GoogleChrome/webrtc/issues{/number}","pulls_url":"https://api.github.com/repos/GoogleChrome/webrtc/pulls{/number}","milestones_url":"https://api.github.com/repos/GoogleChrome/webrtc/milestones{/number}","notifications_url":"https://api.github.com/repos/GoogleChrome/webrtc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/GoogleChrome/webrtc/labels{/name}","releases_url":"https://api.github.com/repos/GoogleChrome/webrtc/releases{/id}","created_at":"2013-09-20T10:40:23Z","updated_at":"2015-01-22T17:55:35Z","pushed_at":"2015-01-22T22:23:44Z","git_url":"git://github.com/GoogleChrome/webrtc.git","ssh_url":"git@github.com:GoogleChrome/webrtc.git","clone_url":"https://github.com/GoogleChrome/webrtc.git","svn_url":"https://github.com/GoogleChrome/webrtc","homepage":"http://googlechrome.github.io/webrtc/","size":10467,"stargazers_count":656,"watchers_count":656,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":386,"mirror_url":null,"open_issues_count":80,"forks":386,"open_issues":80,"watchers":656,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406"},"html":{"href":"https://github.com/GoogleChrome/webrtc/pull/406"},"issue":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/issues/406"},"comments":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/issues/406/comments"},"review_comments":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406/comments"},"review_comment":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/pulls/406/commits"},"statuses":{"href":"https://api.github.com/repos/GoogleChrome/webrtc/statuses/852b1d63800977238bd1d2c9a83799b200714715"}}}}