{"action":"created","comment":{"url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/comments/24646300","id":24646300,"diff_hunk":"@@ -0,0 +1,161 @@\n+__author__ = 'root'\n+\n+import sys\n+import urllib2\n+sys.path.append(\"..\")\n+# -*- coding:utf8 -*-\n+# encoding = utf-8\n+from app.functions import get_remote, get_config, post_to_remote\n+from app.log import log\n+import json\n+from user_mgr import user_manager\n+from flask import redirect, url_for\n+\n+hackathon_api_url = get_config(\"hackathon-api/endpoint\")\n+hackathon_name = get_config(\"javascript/hackathon/name\")\n+\n+\n+\n+\n+class LoginBase():\n+\n+    def login2db(self,args):\n+\n+        access_token = args.get('access_token')\n+        provider = args.get('provider')\n+        # get user info\n+\n+        user_info_resp = get_remote(get_config('login/github/user_info_url') + access_token)\n+        # conn.request('GET',url,'',{'user-agent':'flask'})\n+        log.debug(\"get admin user info from \" + provider + \" : \"  + user_info_resp + '\\n' )\n+        # example:\n+        #\n+        # {\"login\":\"juniwang\",\"id\":8814383,\"avatar_url\":\"https://avatars.githubusercontent.com/u/8814383?v=3\",\"gravatar_id\":\"\",\n+        # \"url\":\"https://api.github.com/users/juniwang\",\"html_url\":\"https://github.com/juniwang\",\n+        # \"followers_url\":\"https://api.github.com/users/juniwang/followers\",\n+        # \"following_url\":\"https://api.github.com/users/juniwang/following{/other_user}\",\n+        # \"gists_url\":\"https://api.github.com/users/juniwang/gists{/gist_id}\",\n+        # \"starred_url\":\"https://api.github.com/users/juniwang/starred{/owner}{/repo}\",\n+        # \"subscriptions_url\":\"https://api.github.com/users/juniwang/subscriptions\",\n+        # \"organizations_url\":\"https://api.github.com/users/juniwang/orgs\",\"repos_url\":\"https://api.github.com/users/juniwang/repos\",\n+        # \"events_url\":\"https://api.github.com/users/juniwang/events{/privacy}\",\n+        # \"received_events_url\":\"https://api.github.com/users/juniwang/received_events\",\"type\":\"User\",\"site_admin\":false,\n+        # \"name\":\"Junbo Wang\",\"company\":\"\",\"blog\":\"\",\"location\":\"Shanghai China\",\n+        # \"email\":\"wangjunbo924@gmail.com\",\"hireable\":false,\"bio\":null,\"public_repos\":12,\"public_gists\":0,\"followers\":0,\n+        # \"following\":1,\"created_at\":\"2014-09-18T01:30:30Z\",\"updated_at\":\"2014-11-25T09:00:37Z\",\"private_gists\":0,\n+        # \"total_private_repos\":0,\"owned_private_repos\":0,\"disk_usage\":14179,\"collaborators\":0,\n+        # \"plan\":{\"name\":\"free\",\"space\":307200,\"collaborators\":0,\"private_repos\":0}}\n+        #\n+        user_info = json.loads(user_info_resp)\n+        name = user_info[\"login\"]\n+        nickname = user_info[\"name\"] if \"name\" in user_info else name\n+        openid = str(user_info[\"id\"])\n+        avatar = user_info[\"avatar_url\"]\n+        # get user primary email\n+        email_info_resp = get_remote(get_config('login/github/emails_info_url') + access_token)\n+        log.debug(\"get email from github:\" + email_info_resp + '\\n')\n+        # email_info include all user email provided by github\n+        # email is user's primary email\n+        email_info = json.loads(email_info_resp)\n+        admin_with_token = user_manager.db_login(openid,\n+                                                name=name,\n+                                                nickname=nickname,\n+                                                access_token=access_token,\n+                                                email_info=email_info,\n+                                                avatar_url=avatar)\n+        # login flask\n+        admin = admin_with_token[\"user\"]\n+        log.info(\"github user login successfully:\" + repr(admin))\n+        detail = user_manager.get_user_detail_info(user, hackathon_name=hackathon_name)\n+        detail[\"token\"] = admin_with_token[\"token\"].token\n+        return detail\n+\n+\n+\n+\n+\n+\n+class LoginUser:\n+    def __init__(self, **kwargs):\n+        self.id = kwargs[\"id\"]\n+        self.name = kwargs[\"name\"]\n+        self.nickname = kwargs[\"nickname\"]\n+        self.avatar_url = kwargs[\"avatar_url\"]\n+\n+    def is_authenticated(self):\n+        return True\n+\n+    def is_active(self):\n+        return True\n+\n+\n+    def is_anonymous(self):\n+        return False\n+\n+    def get_id(self):\n+        return unicode(self.id)\n+\n+\n+class QQLogin():\n+    def login(self, args):\n+        code = args.get('code')\n+        state = \"openhackathon\"\n+        # if state != QQ_OAUTH_STATE:\n+        # log.warn(\"STATE match fail. Potentially CSFR.\")\n+        # return \"UnAuthorized\", 401\n+\n+        # get access token\n+        token_resp = get_remote(get_config(\"login/qq/access_token_url\") + code + '&state=' + state)\n+        log.debug(\"get token from qq:\" + token_resp)\n+        start = token_resp.index('=')\n+        end = token_resp.index('&')\n+        access_token = token_resp[start + 1:end]\n+        # get user info\n+        data = {\"provider\": \"qq\", \"access_token\": access_token}\n+        # url = get_config(\"login/qq/user_info_url\") % (access_token, client_id, openid)\n+        return post_to_remote('%s/api/user/login' % hackathon_api_url, data)\n+\n+\n+class GithubLogin(LoginBase):\n+    def login(self, args):\n+        code = args.get('code')\n+\n+        # get access_token\n+        token_resp = get_remote(get_config('login/github/access_token_url') + code)\n+        log.debug(\"get token from github:\" + token_resp)\n+        start = token_resp.index('=')\n+        end = token_resp.index('&')\n+        access_token = token_resp[start + 1:end]\n+        # get user info\n+        # user_info_resp = get_remote(get_config('login/github/user_info_url') + access_token)\n+        # conn.request('GET',url,'',{'user-agent':'flask'})\n+        log.debug(\"get token info from github\")\n+        args = {\"provider\": \"github\",\"code\": code, \"access_token\": access_token, \"hackathon_name\": hackathon_name}\n+\n+        return self.login2db(args)\n+\n+\n+\n+class GitcafeLogin():\n+    def login(self, args):\n+        code = args.get('code')\n+        url = get_config('login/gitcafe/access_token_url') + code\n+        opener = urllib2.build_opener(urllib2.HTTPHandler)\n+        request = urllib2.Request(url, \"\")\n+        # req = requests.post(url, verify=True)\n+        resp = opener.open(request)\n+        # log.debug(\"get token from gitcafe:\" + resp.read())\n+        token_resp = json.loads(resp.read())\n+        # token_resp = json.loads(resp.read())\n+        # token_resp = req.content()\n+        data = {\"provider\": \"gitcafe\", \"access_token\": token_resp['access_token'], \"name\": hackathon_name}\n+        return post_to_remote('%s/api/user/login' % hackathon_api_url, data)","path":"open-hackathon-adminUI/src/app/admin/login.py","position":152,"original_position":152,"commit_id":"5e9401b7b1125364120601095cf75485ffbecc73","original_commit_id":"5e9401b7b1125364120601095cf75485ffbecc73","user":{"login":"juniwang","id":8814383,"avatar_url":"https://avatars.githubusercontent.com/u/8814383?v=3","gravatar_id":"","url":"https://api.github.com/users/juniwang","html_url":"https://github.com/juniwang","followers_url":"https://api.github.com/users/juniwang/followers","following_url":"https://api.github.com/users/juniwang/following{/other_user}","gists_url":"https://api.github.com/users/juniwang/gists{/gist_id}","starred_url":"https://api.github.com/users/juniwang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juniwang/subscriptions","organizations_url":"https://api.github.com/users/juniwang/orgs","repos_url":"https://api.github.com/users/juniwang/repos","events_url":"https://api.github.com/users/juniwang/events{/privacy}","received_events_url":"https://api.github.com/users/juniwang/received_events","type":"User","site_admin":false},"body":"similar with QQLogin","created_at":"2015-02-13T06:17:09Z","updated_at":"2015-02-13T06:17:09Z","html_url":"https://github.com/msopentechcn/open-hackathon/pull/149#discussion_r24646300","pull_request_url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149","_links":{"self":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/comments/24646300"},"html":{"href":"https://github.com/msopentechcn/open-hackathon/pull/149#discussion_r24646300"},"pull_request":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149"}}},"pull_request":{"url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149","id":29235997,"html_url":"https://github.com/msopentechcn/open-hackathon/pull/149","diff_url":"https://github.com/msopentechcn/open-hackathon/pull/149.diff","patch_url":"https://github.com/msopentechcn/open-hackathon/pull/149.patch","issue_url":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/149","number":149,"state":"open","locked":false,"title":"admin github login ","user":{"login":"mshubian","id":10021488,"avatar_url":"https://avatars.githubusercontent.com/u/10021488?v=3","gravatar_id":"","url":"https://api.github.com/users/mshubian","html_url":"https://github.com/mshubian","followers_url":"https://api.github.com/users/mshubian/followers","following_url":"https://api.github.com/users/mshubian/following{/other_user}","gists_url":"https://api.github.com/users/mshubian/gists{/gist_id}","starred_url":"https://api.github.com/users/mshubian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshubian/subscriptions","organizations_url":"https://api.github.com/users/mshubian/orgs","repos_url":"https://api.github.com/users/mshubian/repos","events_url":"https://api.github.com/users/mshubian/events{/privacy}","received_events_url":"https://api.github.com/users/mshubian/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-13T02:32:54Z","updated_at":"2015-02-13T06:17:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4bd12b5a2aefb132a63575f652989602cf9d987d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149/commits","review_comments_url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149/comments","review_comment_url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/comments/{number}","comments_url":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/149/comments","statuses_url":"https://api.github.com/repos/msopentechcn/open-hackathon/statuses/5e9401b7b1125364120601095cf75485ffbecc73","head":{"label":"mshubian:master","ref":"master","sha":"5e9401b7b1125364120601095cf75485ffbecc73","user":{"login":"mshubian","id":10021488,"avatar_url":"https://avatars.githubusercontent.com/u/10021488?v=3","gravatar_id":"","url":"https://api.github.com/users/mshubian","html_url":"https://github.com/mshubian","followers_url":"https://api.github.com/users/mshubian/followers","following_url":"https://api.github.com/users/mshubian/following{/other_user}","gists_url":"https://api.github.com/users/mshubian/gists{/gist_id}","starred_url":"https://api.github.com/users/mshubian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshubian/subscriptions","organizations_url":"https://api.github.com/users/mshubian/orgs","repos_url":"https://api.github.com/users/mshubian/repos","events_url":"https://api.github.com/users/mshubian/events{/privacy}","received_events_url":"https://api.github.com/users/mshubian/received_events","type":"User","site_admin":false},"repo":{"id":27369112,"name":"open-hackathon","full_name":"mshubian/open-hackathon","owner":{"login":"mshubian","id":10021488,"avatar_url":"https://avatars.githubusercontent.com/u/10021488?v=3","gravatar_id":"","url":"https://api.github.com/users/mshubian","html_url":"https://github.com/mshubian","followers_url":"https://api.github.com/users/mshubian/followers","following_url":"https://api.github.com/users/mshubian/following{/other_user}","gists_url":"https://api.github.com/users/mshubian/gists{/gist_id}","starred_url":"https://api.github.com/users/mshubian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshubian/subscriptions","organizations_url":"https://api.github.com/users/mshubian/orgs","repos_url":"https://api.github.com/users/mshubian/repos","events_url":"https://api.github.com/users/mshubian/events{/privacy}","received_events_url":"https://api.github.com/users/mshubian/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mshubian/open-hackathon","description":"Open Source Software Laboratory","fork":true,"url":"https://api.github.com/repos/mshubian/open-hackathon","forks_url":"https://api.github.com/repos/mshubian/open-hackathon/forks","keys_url":"https://api.github.com/repos/mshubian/open-hackathon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mshubian/open-hackathon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mshubian/open-hackathon/teams","hooks_url":"https://api.github.com/repos/mshubian/open-hackathon/hooks","issue_events_url":"https://api.github.com/repos/mshubian/open-hackathon/issues/events{/number}","events_url":"https://api.github.com/repos/mshubian/open-hackathon/events","assignees_url":"https://api.github.com/repos/mshubian/open-hackathon/assignees{/user}","branches_url":"https://api.github.com/repos/mshubian/open-hackathon/branches{/branch}","tags_url":"https://api.github.com/repos/mshubian/open-hackathon/tags","blobs_url":"https://api.github.com/repos/mshubian/open-hackathon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mshubian/open-hackathon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mshubian/open-hackathon/git/refs{/sha}","trees_url":"https://api.github.com/repos/mshubian/open-hackathon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mshubian/open-hackathon/statuses/{sha}","languages_url":"https://api.github.com/repos/mshubian/open-hackathon/languages","stargazers_url":"https://api.github.com/repos/mshubian/open-hackathon/stargazers","contributors_url":"https://api.github.com/repos/mshubian/open-hackathon/contributors","subscribers_url":"https://api.github.com/repos/mshubian/open-hackathon/subscribers","subscription_url":"https://api.github.com/repos/mshubian/open-hackathon/subscription","commits_url":"https://api.github.com/repos/mshubian/open-hackathon/commits{/sha}","git_commits_url":"https://api.github.com/repos/mshubian/open-hackathon/git/commits{/sha}","comments_url":"https://api.github.com/repos/mshubian/open-hackathon/comments{/number}","issue_comment_url":"https://api.github.com/repos/mshubian/open-hackathon/issues/comments/{number}","contents_url":"https://api.github.com/repos/mshubian/open-hackathon/contents/{+path}","compare_url":"https://api.github.com/repos/mshubian/open-hackathon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mshubian/open-hackathon/merges","archive_url":"https://api.github.com/repos/mshubian/open-hackathon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mshubian/open-hackathon/downloads","issues_url":"https://api.github.com/repos/mshubian/open-hackathon/issues{/number}","pulls_url":"https://api.github.com/repos/mshubian/open-hackathon/pulls{/number}","milestones_url":"https://api.github.com/repos/mshubian/open-hackathon/milestones{/number}","notifications_url":"https://api.github.com/repos/mshubian/open-hackathon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mshubian/open-hackathon/labels{/name}","releases_url":"https://api.github.com/repos/mshubian/open-hackathon/releases{/id}","created_at":"2014-12-01T08:07:40Z","updated_at":"2015-02-13T01:58:18Z","pushed_at":"2015-02-13T01:58:18Z","git_url":"git://github.com/mshubian/open-hackathon.git","ssh_url":"git@github.com:mshubian/open-hackathon.git","clone_url":"https://github.com/mshubian/open-hackathon.git","svn_url":"https://github.com/mshubian/open-hackathon","homepage":null,"size":6344,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"msopentechcn:master","ref":"master","sha":"c1115d3a97fdf112ad6830f373b2fb78a4593b5a","user":{"login":"msopentechcn","id":8188262,"avatar_url":"https://avatars.githubusercontent.com/u/8188262?v=3","gravatar_id":"","url":"https://api.github.com/users/msopentechcn","html_url":"https://github.com/msopentechcn","followers_url":"https://api.github.com/users/msopentechcn/followers","following_url":"https://api.github.com/users/msopentechcn/following{/other_user}","gists_url":"https://api.github.com/users/msopentechcn/gists{/gist_id}","starred_url":"https://api.github.com/users/msopentechcn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msopentechcn/subscriptions","organizations_url":"https://api.github.com/users/msopentechcn/orgs","repos_url":"https://api.github.com/users/msopentechcn/repos","events_url":"https://api.github.com/users/msopentechcn/events{/privacy}","received_events_url":"https://api.github.com/users/msopentechcn/received_events","type":"Organization","site_admin":false},"repo":{"id":21931750,"name":"open-hackathon","full_name":"msopentechcn/open-hackathon","owner":{"login":"msopentechcn","id":8188262,"avatar_url":"https://avatars.githubusercontent.com/u/8188262?v=3","gravatar_id":"","url":"https://api.github.com/users/msopentechcn","html_url":"https://github.com/msopentechcn","followers_url":"https://api.github.com/users/msopentechcn/followers","following_url":"https://api.github.com/users/msopentechcn/following{/other_user}","gists_url":"https://api.github.com/users/msopentechcn/gists{/gist_id}","starred_url":"https://api.github.com/users/msopentechcn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msopentechcn/subscriptions","organizations_url":"https://api.github.com/users/msopentechcn/orgs","repos_url":"https://api.github.com/users/msopentechcn/repos","events_url":"https://api.github.com/users/msopentechcn/events{/privacy}","received_events_url":"https://api.github.com/users/msopentechcn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/msopentechcn/open-hackathon","description":"Open Hackathon Platform","fork":false,"url":"https://api.github.com/repos/msopentechcn/open-hackathon","forks_url":"https://api.github.com/repos/msopentechcn/open-hackathon/forks","keys_url":"https://api.github.com/repos/msopentechcn/open-hackathon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/msopentechcn/open-hackathon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/msopentechcn/open-hackathon/teams","hooks_url":"https://api.github.com/repos/msopentechcn/open-hackathon/hooks","issue_events_url":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/events{/number}","events_url":"https://api.github.com/repos/msopentechcn/open-hackathon/events","assignees_url":"https://api.github.com/repos/msopentechcn/open-hackathon/assignees{/user}","branches_url":"https://api.github.com/repos/msopentechcn/open-hackathon/branches{/branch}","tags_url":"https://api.github.com/repos/msopentechcn/open-hackathon/tags","blobs_url":"https://api.github.com/repos/msopentechcn/open-hackathon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/msopentechcn/open-hackathon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/msopentechcn/open-hackathon/git/refs{/sha}","trees_url":"https://api.github.com/repos/msopentechcn/open-hackathon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/msopentechcn/open-hackathon/statuses/{sha}","languages_url":"https://api.github.com/repos/msopentechcn/open-hackathon/languages","stargazers_url":"https://api.github.com/repos/msopentechcn/open-hackathon/stargazers","contributors_url":"https://api.github.com/repos/msopentechcn/open-hackathon/contributors","subscribers_url":"https://api.github.com/repos/msopentechcn/open-hackathon/subscribers","subscription_url":"https://api.github.com/repos/msopentechcn/open-hackathon/subscription","commits_url":"https://api.github.com/repos/msopentechcn/open-hackathon/commits{/sha}","git_commits_url":"https://api.github.com/repos/msopentechcn/open-hackathon/git/commits{/sha}","comments_url":"https://api.github.com/repos/msopentechcn/open-hackathon/comments{/number}","issue_comment_url":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/comments/{number}","contents_url":"https://api.github.com/repos/msopentechcn/open-hackathon/contents/{+path}","compare_url":"https://api.github.com/repos/msopentechcn/open-hackathon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/msopentechcn/open-hackathon/merges","archive_url":"https://api.github.com/repos/msopentechcn/open-hackathon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/msopentechcn/open-hackathon/downloads","issues_url":"https://api.github.com/repos/msopentechcn/open-hackathon/issues{/number}","pulls_url":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls{/number}","milestones_url":"https://api.github.com/repos/msopentechcn/open-hackathon/milestones{/number}","notifications_url":"https://api.github.com/repos/msopentechcn/open-hackathon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/msopentechcn/open-hackathon/labels{/name}","releases_url":"https://api.github.com/repos/msopentechcn/open-hackathon/releases{/id}","created_at":"2014-07-17T06:53:26Z","updated_at":"2015-02-12T02:35:18Z","pushed_at":"2015-02-12T02:43:53Z","git_url":"git://github.com/msopentechcn/open-hackathon.git","ssh_url":"git@github.com:msopentechcn/open-hackathon.git","clone_url":"https://github.com/msopentechcn/open-hackathon.git","svn_url":"https://github.com/msopentechcn/open-hackathon","homepage":"","size":7521,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":2,"forks":10,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149"},"html":{"href":"https://github.com/msopentechcn/open-hackathon/pull/149"},"issue":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/149"},"comments":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/issues/149/comments"},"review_comments":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149/comments"},"review_comment":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/pulls/149/commits"},"statuses":{"href":"https://api.github.com/repos/msopentechcn/open-hackathon/statuses/5e9401b7b1125364120601095cf75485ffbecc73"}}}}