{"action":"created","comment":{"url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/comments/44091124","id":44091124,"diff_hunk":"@@ -21,47 +31,68 @@ def __init__(self):\n         super().__init__()\n         self.start_screen = Ui_startscreen()\n \n+    @asyncio.coroutine\n     def log_in(self):\n-        user_name = self.start_screen.emailEdit.text().strip()\n-        password = self.start_screen.passwordEdit.text().strip()\n-\n-        # assumption: logged in user properly.\n-        # assumption: all the fields needed for db are set\n-        full_name = user_name\n-        osf_id = user_name\n-        oauth_token = password\n-        # check if user already exists in db\n-\n+        logging.debug('attempting to log in')\n+        personal_access_token = self.start_screen.personalAccessTokenEdit.text().strip()\n+        url = settings.API_BASE + '/v2/users/me/'\n+        loop = asyncio.get_event_loop()\n+        osf = OSFQuery(loop, personal_access_token)\n+        logging.debug('url: {}'.format(url))\n         try:\n-            user = session.query(User).filter(User.osf_login == user_name).one()\n-            user.logged_in = True\n-            save(session, user)\n-            self.close()\n-        except MultipleResultsFound:\n-            logging.warning(\n-                'multiple users with same username. deleting all users with this username. restarting function.')\n-            for user in session.query(User):\n-                if user.osf_login == user_name:\n-                    session.delete(user)\n-                    save(session)\n-            self.log_in()\n-        except NoResultFound:\n-            logging.warning('user doesnt exist. Creating user. and logging them in.')\n-            user = User(\n-                full_name=full_name,\n-                osf_id=osf_id,\n-                osf_login=user_name,\n-                osf_local_folder_path='',\n-                oauth_token=oauth_token,\n-                osf_password=password\n+            resp = yield from osf.make_request(url)\n+        except (aiohttp.errors.ClientTimeoutError, aiohttp.errors.ClientConnectionError, concurrent.futures._base.TimeoutError):\n+            # No internet connection\n+            QMessageBox.warning(\n+                None,\n+                \"Log in Failed\",\n+                \"Unable to connect to server. Check your internet connection or try again later.\"\n+            )\n+        except aiohttp.errors.HttpBadRequest:\n+            # Invalid credentials\n+            QMessageBox.warning(\n+                None,\n+                \"Log in Failed\",\n+                \"Invalid login credentials.\"\n             )\n-            user.logged_in = True\n-            save(session, user)\n+        else:\n+            if resp.status == 200:\n+                json_resp = yield from resp.json()\n+                full_name = json_resp['data']['attributes']['full_name']     # Use one of these later to \"Welcome {user}\"\n+                given_name = json_resp['data']['attributes']['given_name']   # Use one of these later to \"Welcome {user}\"\n+                osf_id = json_resp['data']['id']\n+\n+                try:\n+                    user = session.query(User).filter(User.osf_id == osf_id).one()\n+                    user.logged_in = True\n+                    save(session, user)\n+                except MultipleResultsFound:\n+                    logging.warning('multiple users with same username. deleting all users with this username. restarting function.')\n+                    for user in session.query(User):\n+                        if user.osf_login == osf_id:\n+                            session.delete(user)\n+                            save(session)\n+                    self.log_in()\n+                except NoResultFound:\n+                    logging.debug('user doesnt exist. Creating user. and logging them in.')\n+                    user = User(\n+                        full_name=full_name,\n+                        osf_id=osf_id,\n+                        osf_login='',  # TODO: email goes here when more auth methods are added, not currently returned by APIv2\n+                        osf_local_folder_path='',\n+                        oauth_token=personal_access_token,\n+                    )\n+                    user.logged_in = True\n+                    save(session, user)","path":"osfoffline/views/start_screen.py","position":115,"original_position":115,"commit_id":"92540bc79ee30daa0e92b0cc23514663cef353b3","original_commit_id":"92540bc79ee30daa0e92b0cc23514663cef353b3","user":{"login":"abought","id":2957073,"avatar_url":"https://avatars.githubusercontent.com/u/2957073?v=3","gravatar_id":"","url":"https://api.github.com/users/abought","html_url":"https://github.com/abought","followers_url":"https://api.github.com/users/abought/followers","following_url":"https://api.github.com/users/abought/following{/other_user}","gists_url":"https://api.github.com/users/abought/gists{/gist_id}","starred_url":"https://api.github.com/users/abought/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abought/subscriptions","organizations_url":"https://api.github.com/users/abought/orgs","repos_url":"https://api.github.com/users/abought/repos","events_url":"https://api.github.com/users/abought/events{/privacy}","received_events_url":"https://api.github.com/users/abought/received_events","type":"User","site_admin":false},"body":"Should we add error handling in the case that `save` (re)raises an exception?","created_at":"2015-11-06T00:43:40Z","updated_at":"2015-11-06T00:43:40Z","html_url":"https://github.com/himanshuo/OSF-Offline/pull/13#discussion_r44091124","pull_request_url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13","_links":{"self":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/comments/44091124"},"html":{"href":"https://github.com/himanshuo/OSF-Offline/pull/13#discussion_r44091124"},"pull_request":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13","id":49856710,"html_url":"https://github.com/himanshuo/OSF-Offline/pull/13","diff_url":"https://github.com/himanshuo/OSF-Offline/pull/13.diff","patch_url":"https://github.com/himanshuo/OSF-Offline/pull/13.patch","issue_url":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/13","number":13,"state":"open","locked":false,"title":"[WIP][Feature] Authorization via Personal Access Tokens","user":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"body":"\r\nPurpose\r\n=======\r\nProvide a simpler way of authorizing than using cookies\r\n\r\nChanges\r\n=======\r\n* Start screen UI updated to accept only PAT, with instructions on PAT generation\r\n* Login method updated to use PATs\r\n\r\nSide Effects\r\n=========\r\n* better `[(<local>, <remote>), ...]` tuple list creation logic\r\n* better stop_loop logic\r\n* more logging added\r\n\r\nNotes\r\n=====\r\n* Tested to be functional on Mac and Windows\r\n* TODO: better error handling related to login timeouts\r\n\r\n[#OSF-5064]\r\n[#OSF-5096]","created_at":"2015-11-05T17:46:57Z","updated_at":"2015-11-06T00:43:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8b04d71b52db42a15c2e43b3a474bc6a86051c17","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13/commits","review_comments_url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13/comments","review_comment_url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/comments{/number}","comments_url":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/13/comments","statuses_url":"https://api.github.com/repos/himanshuo/OSF-Offline/statuses/92540bc79ee30daa0e92b0cc23514663cef353b3","head":{"label":"mfraezz:feature/PAT-auth","ref":"feature/PAT-auth","sha":"92540bc79ee30daa0e92b0cc23514663cef353b3","user":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"repo":{"id":45425839,"name":"OSF-Offline","full_name":"mfraezz/OSF-Offline","owner":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mfraezz/OSF-Offline","description":"Desktop Synchronization for OSF Projects ","fork":true,"url":"https://api.github.com/repos/mfraezz/OSF-Offline","forks_url":"https://api.github.com/repos/mfraezz/OSF-Offline/forks","keys_url":"https://api.github.com/repos/mfraezz/OSF-Offline/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mfraezz/OSF-Offline/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mfraezz/OSF-Offline/teams","hooks_url":"https://api.github.com/repos/mfraezz/OSF-Offline/hooks","issue_events_url":"https://api.github.com/repos/mfraezz/OSF-Offline/issues/events{/number}","events_url":"https://api.github.com/repos/mfraezz/OSF-Offline/events","assignees_url":"https://api.github.com/repos/mfraezz/OSF-Offline/assignees{/user}","branches_url":"https://api.github.com/repos/mfraezz/OSF-Offline/branches{/branch}","tags_url":"https://api.github.com/repos/mfraezz/OSF-Offline/tags","blobs_url":"https://api.github.com/repos/mfraezz/OSF-Offline/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mfraezz/OSF-Offline/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mfraezz/OSF-Offline/git/refs{/sha}","trees_url":"https://api.github.com/repos/mfraezz/OSF-Offline/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mfraezz/OSF-Offline/statuses/{sha}","languages_url":"https://api.github.com/repos/mfraezz/OSF-Offline/languages","stargazers_url":"https://api.github.com/repos/mfraezz/OSF-Offline/stargazers","contributors_url":"https://api.github.com/repos/mfraezz/OSF-Offline/contributors","subscribers_url":"https://api.github.com/repos/mfraezz/OSF-Offline/subscribers","subscription_url":"https://api.github.com/repos/mfraezz/OSF-Offline/subscription","commits_url":"https://api.github.com/repos/mfraezz/OSF-Offline/commits{/sha}","git_commits_url":"https://api.github.com/repos/mfraezz/OSF-Offline/git/commits{/sha}","comments_url":"https://api.github.com/repos/mfraezz/OSF-Offline/comments{/number}","issue_comment_url":"https://api.github.com/repos/mfraezz/OSF-Offline/issues/comments{/number}","contents_url":"https://api.github.com/repos/mfraezz/OSF-Offline/contents/{+path}","compare_url":"https://api.github.com/repos/mfraezz/OSF-Offline/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mfraezz/OSF-Offline/merges","archive_url":"https://api.github.com/repos/mfraezz/OSF-Offline/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mfraezz/OSF-Offline/downloads","issues_url":"https://api.github.com/repos/mfraezz/OSF-Offline/issues{/number}","pulls_url":"https://api.github.com/repos/mfraezz/OSF-Offline/pulls{/number}","milestones_url":"https://api.github.com/repos/mfraezz/OSF-Offline/milestones{/number}","notifications_url":"https://api.github.com/repos/mfraezz/OSF-Offline/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mfraezz/OSF-Offline/labels{/name}","releases_url":"https://api.github.com/repos/mfraezz/OSF-Offline/releases{/id}","created_at":"2015-11-02T22:19:28Z","updated_at":"2015-07-10T23:05:38Z","pushed_at":"2015-11-05T21:18:26Z","git_url":"git://github.com/mfraezz/OSF-Offline.git","ssh_url":"git@github.com:mfraezz/OSF-Offline.git","clone_url":"https://github.com/mfraezz/OSF-Offline.git","svn_url":"https://github.com/mfraezz/OSF-Offline","homepage":null,"size":1798,"stargazers_count":0,"watchers_count":0,"language":null,"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":"develop"}},"base":{"label":"himanshuo:develop","ref":"develop","sha":"ed52a38963ec60ccf6813e1ef5988015dbd9d135","user":{"login":"himanshuo","id":5808841,"avatar_url":"https://avatars.githubusercontent.com/u/5808841?v=3","gravatar_id":"","url":"https://api.github.com/users/himanshuo","html_url":"https://github.com/himanshuo","followers_url":"https://api.github.com/users/himanshuo/followers","following_url":"https://api.github.com/users/himanshuo/following{/other_user}","gists_url":"https://api.github.com/users/himanshuo/gists{/gist_id}","starred_url":"https://api.github.com/users/himanshuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/himanshuo/subscriptions","organizations_url":"https://api.github.com/users/himanshuo/orgs","repos_url":"https://api.github.com/users/himanshuo/repos","events_url":"https://api.github.com/users/himanshuo/events{/privacy}","received_events_url":"https://api.github.com/users/himanshuo/received_events","type":"User","site_admin":false},"repo":{"id":37069756,"name":"OSF-Offline","full_name":"himanshuo/OSF-Offline","owner":{"login":"himanshuo","id":5808841,"avatar_url":"https://avatars.githubusercontent.com/u/5808841?v=3","gravatar_id":"","url":"https://api.github.com/users/himanshuo","html_url":"https://github.com/himanshuo","followers_url":"https://api.github.com/users/himanshuo/followers","following_url":"https://api.github.com/users/himanshuo/following{/other_user}","gists_url":"https://api.github.com/users/himanshuo/gists{/gist_id}","starred_url":"https://api.github.com/users/himanshuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/himanshuo/subscriptions","organizations_url":"https://api.github.com/users/himanshuo/orgs","repos_url":"https://api.github.com/users/himanshuo/repos","events_url":"https://api.github.com/users/himanshuo/events{/privacy}","received_events_url":"https://api.github.com/users/himanshuo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/himanshuo/OSF-Offline","description":"Desktop Synchronization for OSF Projects ","fork":false,"url":"https://api.github.com/repos/himanshuo/OSF-Offline","forks_url":"https://api.github.com/repos/himanshuo/OSF-Offline/forks","keys_url":"https://api.github.com/repos/himanshuo/OSF-Offline/keys{/key_id}","collaborators_url":"https://api.github.com/repos/himanshuo/OSF-Offline/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/himanshuo/OSF-Offline/teams","hooks_url":"https://api.github.com/repos/himanshuo/OSF-Offline/hooks","issue_events_url":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/events{/number}","events_url":"https://api.github.com/repos/himanshuo/OSF-Offline/events","assignees_url":"https://api.github.com/repos/himanshuo/OSF-Offline/assignees{/user}","branches_url":"https://api.github.com/repos/himanshuo/OSF-Offline/branches{/branch}","tags_url":"https://api.github.com/repos/himanshuo/OSF-Offline/tags","blobs_url":"https://api.github.com/repos/himanshuo/OSF-Offline/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/himanshuo/OSF-Offline/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/himanshuo/OSF-Offline/git/refs{/sha}","trees_url":"https://api.github.com/repos/himanshuo/OSF-Offline/git/trees{/sha}","statuses_url":"https://api.github.com/repos/himanshuo/OSF-Offline/statuses/{sha}","languages_url":"https://api.github.com/repos/himanshuo/OSF-Offline/languages","stargazers_url":"https://api.github.com/repos/himanshuo/OSF-Offline/stargazers","contributors_url":"https://api.github.com/repos/himanshuo/OSF-Offline/contributors","subscribers_url":"https://api.github.com/repos/himanshuo/OSF-Offline/subscribers","subscription_url":"https://api.github.com/repos/himanshuo/OSF-Offline/subscription","commits_url":"https://api.github.com/repos/himanshuo/OSF-Offline/commits{/sha}","git_commits_url":"https://api.github.com/repos/himanshuo/OSF-Offline/git/commits{/sha}","comments_url":"https://api.github.com/repos/himanshuo/OSF-Offline/comments{/number}","issue_comment_url":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/comments{/number}","contents_url":"https://api.github.com/repos/himanshuo/OSF-Offline/contents/{+path}","compare_url":"https://api.github.com/repos/himanshuo/OSF-Offline/compare/{base}...{head}","merges_url":"https://api.github.com/repos/himanshuo/OSF-Offline/merges","archive_url":"https://api.github.com/repos/himanshuo/OSF-Offline/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/himanshuo/OSF-Offline/downloads","issues_url":"https://api.github.com/repos/himanshuo/OSF-Offline/issues{/number}","pulls_url":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls{/number}","milestones_url":"https://api.github.com/repos/himanshuo/OSF-Offline/milestones{/number}","notifications_url":"https://api.github.com/repos/himanshuo/OSF-Offline/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/himanshuo/OSF-Offline/labels{/name}","releases_url":"https://api.github.com/repos/himanshuo/OSF-Offline/releases{/id}","created_at":"2015-06-08T13:50:50Z","updated_at":"2015-07-10T23:05:38Z","pushed_at":"2015-11-05T23:25:27Z","git_url":"git://github.com/himanshuo/OSF-Offline.git","ssh_url":"git@github.com:himanshuo/OSF-Offline.git","clone_url":"https://github.com/himanshuo/OSF-Offline.git","svn_url":"https://github.com/himanshuo/OSF-Offline","homepage":null,"size":2137,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6,"mirror_url":null,"open_issues_count":1,"forks":6,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13"},"html":{"href":"https://github.com/himanshuo/OSF-Offline/pull/13"},"issue":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/13"},"comments":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/himanshuo/OSF-Offline/statuses/92540bc79ee30daa0e92b0cc23514663cef353b3"}}}}