{"action":"created","comment":{"url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/comments/199331633","pull_request_review_id":133435585,"id":199331633,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5OTMzMTYzMw==","diff_hunk":"@@ -2,14 +2,72 @@\n \n const fp = require('fastify-plugin')\n const oauth2Module = require('simple-oauth2')\n+const defaultState = require('crypto').randomBytes(10).toString('hex')\n+\n+function defaultGenerateStateFunction () {\n+  return defaultState\n+}\n+\n+function defaultCheckStateFunction (state, callback) {\n+  if (state === defaultState) {\n+    callback()\n+    return\n+  }\n+  callback(new Error('Invalid state'))\n+}\n \n const oauthPlugin = fp(function (fastify, options, next) {\n   if (!options.name) return next(new Error('options.name is required'))\n \n-  const oauth2 = oauth2Module.create(options.credentials)\n+  const name = options.name\n+  const credentials = options.credentials\n+  const callbackUri = options.callbackUri\n+  const scope = options.scope\n+  const generateStateFunction = options.generateStateFunction || defaultGenerateStateFunction\n+  const checkStateFunction = options.checkStateFunction || defaultCheckStateFunction\n+  const startRedirectPath = options.startRedirectPath\n+\n+  function startRedirectHandler (request, reply) {\n+    const state = generateStateFunction()\n+\n+    const authorizationUri = this[name].authorizationCode.authorizeURL({\n+      redirect_uri: callbackUri,\n+      scope: scope,\n+      state: state\n+    })\n+    reply.redirect(authorizationUri)\n+  }\n+\n+  const cbk = function (o, code, callback) {\n+    return o.authorizationCode.getToken({\n+      code: code,\n+      redirect_uri: callbackUri\n+    }, callback)\n+  }\n+\n+  function getAccessTokenFromAuthorizationCodeFlow (request, callback) {\n+    const code = request.query.code\n+    const state = request.query.state\n+    const fastify = this\n+\n+    checkStateFunction(state, function (err) {\n+      if (err) {\n+        callback(err)\n+        return\n+      }\n+      cbk(fastify[name], code, callback)\n+    })","path":"index.js","position":81,"original_position":59,"commit_id":"a611ad61f5b04ec38569a50d3b2763f0e386a4fd","original_commit_id":"3d1635287711b3be57ce44fa582692c01aaef768","user":{"login":"allevo","id":1054125,"node_id":"MDQ6VXNlcjEwNTQxMjU=","avatar_url":"https://avatars1.githubusercontent.com/u/1054125?v=4","gravatar_id":"","url":"https://api.github.com/users/allevo","html_url":"https://github.com/allevo","followers_url":"https://api.github.com/users/allevo/followers","following_url":"https://api.github.com/users/allevo/following{/other_user}","gists_url":"https://api.github.com/users/allevo/gists{/gist_id}","starred_url":"https://api.github.com/users/allevo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allevo/subscriptions","organizations_url":"https://api.github.com/users/allevo/orgs","repos_url":"https://api.github.com/users/allevo/repos","events_url":"https://api.github.com/users/allevo/events{/privacy}","received_events_url":"https://api.github.com/users/allevo/received_events","type":"User","site_admin":false},"body":"Done","created_at":"2018-06-30T20:31:00Z","updated_at":"2018-06-30T20:31:00Z","html_url":"https://github.com/fastify/fastify-oauth2/pull/7#discussion_r199331633","pull_request_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/comments/199331633"},"html":{"href":"https://github.com/fastify/fastify-oauth2/pull/7#discussion_r199331633"},"pull_request":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7"}},"in_reply_to_id":195922205},"pull_request":{"url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7","id":195354106,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk1MzU0MTA2","html_url":"https://github.com/fastify/fastify-oauth2/pull/7","diff_url":"https://github.com/fastify/fastify-oauth2/pull/7.diff","patch_url":"https://github.com/fastify/fastify-oauth2/pull/7.patch","issue_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/7","number":7,"state":"open","locked":false,"title":"Fix #2 - expose route","user":{"login":"allevo","id":1054125,"node_id":"MDQ6VXNlcjEwNTQxMjU=","avatar_url":"https://avatars1.githubusercontent.com/u/1054125?v=4","gravatar_id":"","url":"https://api.github.com/users/allevo","html_url":"https://github.com/allevo","followers_url":"https://api.github.com/users/allevo/followers","following_url":"https://api.github.com/users/allevo/following{/other_user}","gists_url":"https://api.github.com/users/allevo/gists{/gist_id}","starred_url":"https://api.github.com/users/allevo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allevo/subscriptions","organizations_url":"https://api.github.com/users/allevo/orgs","repos_url":"https://api.github.com/users/allevo/repos","events_url":"https://api.github.com/users/allevo/events{/privacy}","received_events_url":"https://api.github.com/users/allevo/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-06-16T23:47:31Z","updated_at":"2018-06-30T20:31:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fba0d060a6b6507f9a7d2e0b8d1bbf7265d2229b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7/commits","review_comments_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7/comments","review_comment_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/7/comments","statuses_url":"https://api.github.com/repos/fastify/fastify-oauth2/statuses/a611ad61f5b04ec38569a50d3b2763f0e386a4fd","head":{"label":"fastify:fix-2","ref":"fix-2","sha":"a611ad61f5b04ec38569a50d3b2763f0e386a4fd","user":{"login":"fastify","id":24939410,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI0OTM5NDEw","avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"repo":{"id":136843573,"node_id":"MDEwOlJlcG9zaXRvcnkxMzY4NDM1NzM=","name":"fastify-oauth2","full_name":"fastify/fastify-oauth2","owner":{"login":"fastify","id":24939410,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI0OTM5NDEw","avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fastify/fastify-oauth2","description":"Enable to perform login using oauth2 protocol","fork":false,"url":"https://api.github.com/repos/fastify/fastify-oauth2","forks_url":"https://api.github.com/repos/fastify/fastify-oauth2/forks","keys_url":"https://api.github.com/repos/fastify/fastify-oauth2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fastify/fastify-oauth2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fastify/fastify-oauth2/teams","hooks_url":"https://api.github.com/repos/fastify/fastify-oauth2/hooks","issue_events_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/events{/number}","events_url":"https://api.github.com/repos/fastify/fastify-oauth2/events","assignees_url":"https://api.github.com/repos/fastify/fastify-oauth2/assignees{/user}","branches_url":"https://api.github.com/repos/fastify/fastify-oauth2/branches{/branch}","tags_url":"https://api.github.com/repos/fastify/fastify-oauth2/tags","blobs_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/refs{/sha}","trees_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fastify/fastify-oauth2/statuses/{sha}","languages_url":"https://api.github.com/repos/fastify/fastify-oauth2/languages","stargazers_url":"https://api.github.com/repos/fastify/fastify-oauth2/stargazers","contributors_url":"https://api.github.com/repos/fastify/fastify-oauth2/contributors","subscribers_url":"https://api.github.com/repos/fastify/fastify-oauth2/subscribers","subscription_url":"https://api.github.com/repos/fastify/fastify-oauth2/subscription","commits_url":"https://api.github.com/repos/fastify/fastify-oauth2/commits{/sha}","git_commits_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/commits{/sha}","comments_url":"https://api.github.com/repos/fastify/fastify-oauth2/comments{/number}","issue_comment_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/comments{/number}","contents_url":"https://api.github.com/repos/fastify/fastify-oauth2/contents/{+path}","compare_url":"https://api.github.com/repos/fastify/fastify-oauth2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fastify/fastify-oauth2/merges","archive_url":"https://api.github.com/repos/fastify/fastify-oauth2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fastify/fastify-oauth2/downloads","issues_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues{/number}","pulls_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls{/number}","milestones_url":"https://api.github.com/repos/fastify/fastify-oauth2/milestones{/number}","notifications_url":"https://api.github.com/repos/fastify/fastify-oauth2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fastify/fastify-oauth2/labels{/name}","releases_url":"https://api.github.com/repos/fastify/fastify-oauth2/releases{/id}","deployments_url":"https://api.github.com/repos/fastify/fastify-oauth2/deployments","created_at":"2018-06-10T20:44:43Z","updated_at":"2018-06-23T02:50:51Z","pushed_at":"2018-06-17T22:17:16Z","git_url":"git://github.com/fastify/fastify-oauth2.git","ssh_url":"git@github.com:fastify/fastify-oauth2.git","clone_url":"https://github.com/fastify/fastify-oauth2.git","svn_url":"https://github.com/fastify/fastify-oauth2","homepage":null,"size":66,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":3,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":3,"watchers":4,"default_branch":"master"}},"base":{"label":"fastify:master","ref":"master","sha":"62563899f60e67e394c333cbc6fef0c1c7cc2313","user":{"login":"fastify","id":24939410,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI0OTM5NDEw","avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"repo":{"id":136843573,"node_id":"MDEwOlJlcG9zaXRvcnkxMzY4NDM1NzM=","name":"fastify-oauth2","full_name":"fastify/fastify-oauth2","owner":{"login":"fastify","id":24939410,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI0OTM5NDEw","avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fastify/fastify-oauth2","description":"Enable to perform login using oauth2 protocol","fork":false,"url":"https://api.github.com/repos/fastify/fastify-oauth2","forks_url":"https://api.github.com/repos/fastify/fastify-oauth2/forks","keys_url":"https://api.github.com/repos/fastify/fastify-oauth2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fastify/fastify-oauth2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fastify/fastify-oauth2/teams","hooks_url":"https://api.github.com/repos/fastify/fastify-oauth2/hooks","issue_events_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/events{/number}","events_url":"https://api.github.com/repos/fastify/fastify-oauth2/events","assignees_url":"https://api.github.com/repos/fastify/fastify-oauth2/assignees{/user}","branches_url":"https://api.github.com/repos/fastify/fastify-oauth2/branches{/branch}","tags_url":"https://api.github.com/repos/fastify/fastify-oauth2/tags","blobs_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/refs{/sha}","trees_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fastify/fastify-oauth2/statuses/{sha}","languages_url":"https://api.github.com/repos/fastify/fastify-oauth2/languages","stargazers_url":"https://api.github.com/repos/fastify/fastify-oauth2/stargazers","contributors_url":"https://api.github.com/repos/fastify/fastify-oauth2/contributors","subscribers_url":"https://api.github.com/repos/fastify/fastify-oauth2/subscribers","subscription_url":"https://api.github.com/repos/fastify/fastify-oauth2/subscription","commits_url":"https://api.github.com/repos/fastify/fastify-oauth2/commits{/sha}","git_commits_url":"https://api.github.com/repos/fastify/fastify-oauth2/git/commits{/sha}","comments_url":"https://api.github.com/repos/fastify/fastify-oauth2/comments{/number}","issue_comment_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues/comments{/number}","contents_url":"https://api.github.com/repos/fastify/fastify-oauth2/contents/{+path}","compare_url":"https://api.github.com/repos/fastify/fastify-oauth2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fastify/fastify-oauth2/merges","archive_url":"https://api.github.com/repos/fastify/fastify-oauth2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fastify/fastify-oauth2/downloads","issues_url":"https://api.github.com/repos/fastify/fastify-oauth2/issues{/number}","pulls_url":"https://api.github.com/repos/fastify/fastify-oauth2/pulls{/number}","milestones_url":"https://api.github.com/repos/fastify/fastify-oauth2/milestones{/number}","notifications_url":"https://api.github.com/repos/fastify/fastify-oauth2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fastify/fastify-oauth2/labels{/name}","releases_url":"https://api.github.com/repos/fastify/fastify-oauth2/releases{/id}","deployments_url":"https://api.github.com/repos/fastify/fastify-oauth2/deployments","created_at":"2018-06-10T20:44:43Z","updated_at":"2018-06-23T02:50:51Z","pushed_at":"2018-06-17T22:17:16Z","git_url":"git://github.com/fastify/fastify-oauth2.git","ssh_url":"git@github.com:fastify/fastify-oauth2.git","clone_url":"https://github.com/fastify/fastify-oauth2.git","svn_url":"https://github.com/fastify/fastify-oauth2","homepage":null,"size":66,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":3,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":3,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7"},"html":{"href":"https://github.com/fastify/fastify-oauth2/pull/7"},"issue":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/issues/7"},"comments":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/fastify/fastify-oauth2/statuses/a611ad61f5b04ec38569a50d3b2763f0e386a4fd"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"allevo"}}