{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/comments/31057905","id":31057905,"diff_hunk":"@@ -150,6 +165,75 @@ module.exports = function(utils, env, nunjucksEnv, appName) {\n       app.get( '/slowparse/spec/errors.forbidjs.html', function( req, res ) {\n         res.render('/slowparse/spec/errors.forbidjs.html');\n       });\n+    },\n+\n+    oauth2Callback: function(req, res, next) {\n+      if (req.query.logout) {\n+        req.session = null;\n+        return res.redirect(301, '/');\n+      }\n+\n+      if (!req.query.code) {\n+        return next({ code: 401, message: \"OAUTH: Code required\" });\n+      }\n+\n+      if (!req.cookies.state || !req.query.state) {\n+        return next({ code: 401, message: \"OAUTH: State required\" });\n+      }\n+\n+      if (req.cookies.state !== req.query.state) {\n+        return next({ code: 401, message: \"OAUTH: Invalid state\" });\n+      }\n+\n+      if (req.query.client_id !== oauthClientId) {\n+        return next({ code: 401, message: \"OAUTH: Invalid client credentials\" });\n+      }\n+\n+      // First, fetch the token\n+      request.post({\n+        url: oauthUrl + '/login/oauth/access_token',\n+        form: {\n+          client_id: oauthClientId,\n+          client_secret: oauthClientSecret,\n+          grant_type: \"authorization_code\",\n+          code: req.query.code\n+        }\n+      }, function(err, response, body) {\n+        if (err) {\n+          console.log(\"Request error: \", err, \" Body: \", body);\n+          return next({ code: 500, message: \"Internal server error. See logs for details\" });\n+        }\n+\n+        if (response.statusCode !== 200) {\n+          console.log(\"Code \" + response.statusCode + \". Error getting access token: \", body);\n+          return next({ code: response.statusCode, message: body });\n+        }\n+\n+        body = JSON.parse(body);\n+\n+        req.session.token = cryptr.encrypt(body.access_token);\n+\n+        // Next, fetch user data\n+        request.get({\n+          url: oauthUrl + \"/user\",\n+          headers: {\n+            \"Authorization\": \"token \" + body.access_token\n+          }\n+        }, function(err, response, body) {\n+          if (err) {\n+            console.log(\"Request error: \", err, \" Body: \", body);\n+            return next({ code: 500, message: \"Internal server error. See logs for details\" });\n+          }\n+\n+          if (response.statusCode !== 200) {\n+            console.log(\"Code \" + response.statusCode + \". Error getting user data: \", body);\n+            return next({ code: response.statusCode, message: body });\n+          }\n+\n+          req.session.user = JSON.parse(body);\n+          res.redirect(301, '/');","path":"routes/index.js","position":156,"original_position":156,"commit_id":"298ac490b6d0bf3750c73de90d5794923a0f0c4f","original_commit_id":"298ac490b6d0bf3750c73de90d5794923a0f0c4f","user":{"login":"Pomax","id":177243,"avatar_url":"https://avatars.githubusercontent.com/u/177243?v=3","gravatar_id":"","url":"https://api.github.com/users/Pomax","html_url":"https://github.com/Pomax","followers_url":"https://api.github.com/users/Pomax/followers","following_url":"https://api.github.com/users/Pomax/following{/other_user}","gists_url":"https://api.github.com/users/Pomax/gists{/gist_id}","starred_url":"https://api.github.com/users/Pomax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Pomax/subscriptions","organizations_url":"https://api.github.com/users/Pomax/orgs","repos_url":"https://api.github.com/users/Pomax/repos","events_url":"https://api.github.com/users/Pomax/events{/privacy}","received_events_url":"https://api.github.com/users/Pomax/received_events","type":"User","site_admin":false},"body":"moved permanently? O_o","created_at":"2015-05-26T17:26:39Z","updated_at":"2015-05-26T17:26:39Z","html_url":"https://github.com/mozilla/thimble.webmaker.org/pull/518#discussion_r31057905","pull_request_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518","_links":{"self":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/comments/31057905"},"html":{"href":"https://github.com/mozilla/thimble.webmaker.org/pull/518#discussion_r31057905"},"pull_request":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518","id":35741679,"html_url":"https://github.com/mozilla/thimble.webmaker.org/pull/518","diff_url":"https://github.com/mozilla/thimble.webmaker.org/pull/518.diff","patch_url":"https://github.com/mozilla/thimble.webmaker.org/pull/518.patch","issue_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/518","number":518,"state":"open","locked":false,"title":"[WIP] Integrate ID into thimble","user":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"body":"This is a full implementation of Thimble using the [<kbd>id.webmaker.org</kbd>](https://github.com/mozilla/id.webmaker.org) oauth2 server for authentication. Very little UX was changed since a refresh is in the works.\r\n\r\n**Known issues:**\r\n\r\n1.  Logging in will lose all work in the editor. \r\n2.  Toolbar links to \"my profile\", \"my settings\" and \"my makes\" won't work since the rest of wm.o isn't configured to use `id`. \r\n3.  Thimble must completely load before signing in is possible.\r\n\r\n**Test instructions:**\r\n\r\n1. Clone, install, configure and run [<kbd>id.webmaker.org</kbd>](https://github.com/mozilla/id.webmaker.org). You'll need to install and run postgres to make this work, and you'll need to create and populate the `webmaker_oauth_test` database. Copy [the sql in this gist](http://pastebin.com/mqSS9bnu) over the `test-data.sql` file before running `test-data.js`. Also, unlike the other parts of webmaker, ID doesn't automatically manage the environment variables so a little more work is needed. (I can help walk people through this part)\r\n2. Configure and run thimble with the [new .env vars](https://github.com/mozilla/thimble.webmaker.org/pull/518/files#diff-45473cb0daa25d5231f105558b5b7226R131)\r\n3. Run the wm.o login server","created_at":"2015-05-19T15:19:57Z","updated_at":"2015-05-26T17:26:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6c94e201dc4144bee2b5365a8e5c56d0d72c7940","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518/commits","review_comments_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518/comments","review_comment_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/518/comments","statuses_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/statuses/298ac490b6d0bf3750c73de90d5794923a0f0c4f","head":{"label":"sedge:loginSwitch","ref":"loginSwitch","sha":"298ac490b6d0bf3750c73de90d5794923a0f0c4f","user":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"repo":{"id":9838309,"name":"thimble.webmaker.org","full_name":"sedge/thimble.webmaker.org","owner":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sedge/thimble.webmaker.org","description":"a minimal thimble on node.js POC","fork":true,"url":"https://api.github.com/repos/sedge/thimble.webmaker.org","forks_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/forks","keys_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/teams","hooks_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/hooks","issue_events_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/issues/events{/number}","events_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/events","assignees_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/assignees{/user}","branches_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/branches{/branch}","tags_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/tags","blobs_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/git/refs{/sha}","trees_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/statuses/{sha}","languages_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/languages","stargazers_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/stargazers","contributors_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/contributors","subscribers_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/subscribers","subscription_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/subscription","commits_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/commits{/sha}","git_commits_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/git/commits{/sha}","comments_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/comments{/number}","issue_comment_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/issues/comments{/number}","contents_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/contents/{+path}","compare_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/merges","archive_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/downloads","issues_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/issues{/number}","pulls_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/pulls{/number}","milestones_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/milestones{/number}","notifications_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/labels{/name}","releases_url":"https://api.github.com/repos/sedge/thimble.webmaker.org/releases{/id}","created_at":"2013-05-03T15:01:51Z","updated_at":"2015-05-06T18:25:59Z","pushed_at":"2015-05-26T16:14:05Z","git_url":"git://github.com/sedge/thimble.webmaker.org.git","ssh_url":"git@github.com:sedge/thimble.webmaker.org.git","clone_url":"https://github.com/sedge/thimble.webmaker.org.git","svn_url":"https://github.com/sedge/thimble.webmaker.org","homepage":null,"size":17078,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":0,"forks":4,"open_issues":0,"watchers":0,"default_branch":"bramble"}},"base":{"label":"mozilla:bramble","ref":"bramble","sha":"c9461e96e1a883b9a4e40da17abe163905d9fee9","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":9349840,"name":"thimble.webmaker.org","full_name":"mozilla/thimble.webmaker.org","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/thimble.webmaker.org","description":"Thimble coding editor","fork":false,"url":"https://api.github.com/repos/mozilla/thimble.webmaker.org","forks_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/forks","keys_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/teams","hooks_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/hooks","issue_events_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/events","assignees_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/tags","blobs_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/languages","stargazers_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/stargazers","contributors_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/contributors","subscribers_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/subscribers","subscription_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/subscription","commits_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/merges","archive_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/downloads","issues_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/thimble.webmaker.org/releases{/id}","created_at":"2013-04-10T15:54:34Z","updated_at":"2015-05-22T06:49:07Z","pushed_at":"2015-05-26T16:14:07Z","git_url":"git://github.com/mozilla/thimble.webmaker.org.git","ssh_url":"git@github.com:mozilla/thimble.webmaker.org.git","clone_url":"https://github.com/mozilla/thimble.webmaker.org.git","svn_url":"https://github.com/mozilla/thimble.webmaker.org","homepage":"thimble.webmaker.org","size":20593,"stargazers_count":66,"watchers_count":66,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":70,"mirror_url":null,"open_issues_count":42,"forks":70,"open_issues":42,"watchers":66,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518"},"html":{"href":"https://github.com/mozilla/thimble.webmaker.org/pull/518"},"issue":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/518"},"comments":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/issues/518/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/pulls/518/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla/thimble.webmaker.org/statuses/298ac490b6d0bf3750c73de90d5794923a0f0c4f"}}}}