{"action":"created","comment":{"url":"https://api.github.com/repos/rethinkdb/horizon/pulls/comments/63813036","id":63813036,"diff_hunk":"@@ -60,97 +64,77 @@ class Auth {\n   }\n \n   // TODO: maybe we should write something into the user data to track open sessions/tokens\n-  generate_jwt(provider, info, cb) {\n-    const key = auth_key(provider, info);\n-    let query = r.db(this._parent._reql_conn.metadata()._internal_db).table('users_auth').get(key);\n+  generate(provider, info) {\n+    const key = this.auth_key(provider, info);\n+    const internal_db = r.db(this._parent._reql_conn.metadata()._internal_db);\n+\n+    function insert (table, row) {\n+      return internal_db.table(table)\n+                        .insert(row, { conflict: 'error', returnChanges: 'always' })\n+                        .bracket('changes')(0)('new_val')\n+    }\n+\n+    let query = internal_db.table('users')\n+                           .get(internal_db.table('users_auth').get(key)('user_id'))\n+                           .default(r.error('User not found and new user creation is disabled.'));\n \n     if (this._create_new_users) {\n-      query = query.default(r.uuid().do((user_id) =>\n-                r.db(this._parent._reql_conn.metadata()._internal_db)\n-                 .table('users_auth').insert({ id: key, user_id }, { returnChanges: true })\n-                 .do((res) =>\n-                   r.branch(res('inserted').eq(1),\n-                     r.db(this._parent._reql_conn.metadata()._internal_db).table('users')\n-                      .insert(this.new_user_row(user_id))\n-                      .do((res2) =>\n-                        r.branch(res2('inserted').eq(1),\n-                          res2('changes')(0)('new_val'),\n-                          r.error(res2('first_error'))\n-                        )),\n-                     r.error(res('first_error'))\n-                   )\n-                 )\n-             ));\n-    } else {\n-      query = query.default(r.error('User not found and new user creation is disabled.'));\n+      query = insert('users_auth', { id: key, user_id: r.uuid() })\n+        .do(auth_user => insert('users', this.new_user_row(auth_user('user_id'))));\n     }\n \n-    this.reql_call(query).then((res) => {\n-      cb(null, this._jwt_from_user(res.id, provider));\n-    }).catch((err) => {\n+    query = query.merge({ provider })\n+\n+    return this.reql_call(query)\n+    .catch(err => {\n       // TODO: if we got a `Duplicate primary key` error, it was likely a race condition\n       // and we should succeed if we try again.\n       logger.debug(`Failed user lookup or creation: ${err}`);\n-      cb(new Error('User lookup or creation in database failed.'));\n-    });\n+      throw new Error('User lookup or creation in database failed.');\n+    })\n+    .then(user => this._jwt.sign(user));\n   }\n \n-  generate_anon_jwt(cb) {\n-    if (!this._allow_anonymous) {\n-      cb(new Error('Anonymous connections are not allowed.'));\n+  reql_call(query) {\n+    if (!this._parent._reql_conn.ready()) {\n+      return Promise.reject('Connection to database is down, cannot perform authentication.');\n     }\n-\n-    const query = r.db(this._parent._reql_conn.metadata()._internal_db).table('users')\n-                   .insert(this.new_user_row(r.uuid()),\n-                           { returnChanges: 'always' })\n-                   .bracket('changes')(0)\n-                   .do((res) =>\n-                     r.branch(res('new_val').eq(null),\n-                              r.error(res('error')),\n-                              res('new_val')));\n-\n-    this.reql_call(query).then((res) => {\n-      this.verify_jwt(this._jwt_from_user(res.id, null), cb);\n-    }).catch((err) => {\n-      logger.error(`Failed anonymous user creation: ${err.stack}`);\n-      cb(new Error('Anonymous user creation in database failed.'));\n-    });\n+    return query.run(this._parent._reql_conn.connection());\n   }\n+}\n \n-  generate_unauth_jwt(cb) {\n-    if (!this._allow_unauthenticated) {\n-      cb(new Error('Unauthenticated connections are not allowed.'));\n+\n+class JWT {\n+  constructor(options) {\n+    this.duration = options.duration;\n+    this.algorithm = 'HS512';\n+\n+    if (options.token_secret != null) {\n+      this.secret = new Buffer(options.token_secret, 'base64');\n     } else {\n-      this.verify_jwt(this._jwt_from_user(null, null), cb);\n+      throw new Error(\n+        'No token_secret set! Try setting it in .hz/config.toml' +","path":"server/src/auth.js","position":181,"original_position":181,"commit_id":"57efe17805ad62e9c6361572af22cef840f5e1dd","original_commit_id":"57efe17805ad62e9c6361572af22cef840f5e1dd","user":{"login":"Tryneus","id":552910,"avatar_url":"https://avatars.githubusercontent.com/u/552910?v=3","gravatar_id":"","url":"https://api.github.com/users/Tryneus","html_url":"https://github.com/Tryneus","followers_url":"https://api.github.com/users/Tryneus/followers","following_url":"https://api.github.com/users/Tryneus/following{/other_user}","gists_url":"https://api.github.com/users/Tryneus/gists{/gist_id}","starred_url":"https://api.github.com/users/Tryneus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tryneus/subscriptions","organizations_url":"https://api.github.com/users/Tryneus/orgs","repos_url":"https://api.github.com/users/Tryneus/repos","events_url":"https://api.github.com/users/Tryneus/events{/privacy}","received_events_url":"https://api.github.com/users/Tryneus/received_events","type":"User","site_admin":false},"body":"Needs a space after `config.toml`","created_at":"2016-05-19T02:02:46Z","updated_at":"2016-05-19T02:02:46Z","html_url":"https://github.com/rethinkdb/horizon/pull/419#discussion_r63813036","pull_request_url":"https://api.github.com/repos/rethinkdb/horizon/pulls/419","_links":{"self":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/comments/63813036"},"html":{"href":"https://github.com/rethinkdb/horizon/pull/419#discussion_r63813036"},"pull_request":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/419"}}},"pull_request":{"url":"https://api.github.com/repos/rethinkdb/horizon/pulls/419","id":70603059,"html_url":"https://github.com/rethinkdb/horizon/pull/419","diff_url":"https://github.com/rethinkdb/horizon/pull/419.diff","patch_url":"https://github.com/rethinkdb/horizon/pull/419.patch","issue_url":"https://api.github.com/repos/rethinkdb/horizon/issues/419","number":419,"state":"open","locked":false,"title":"Refactor auth to use promises, expose user profile in handshake","user":{"login":"marshall007","id":877936,"avatar_url":"https://avatars.githubusercontent.com/u/877936?v=3","gravatar_id":"","url":"https://api.github.com/users/marshall007","html_url":"https://github.com/marshall007","followers_url":"https://api.github.com/users/marshall007/followers","following_url":"https://api.github.com/users/marshall007/following{/other_user}","gists_url":"https://api.github.com/users/marshall007/gists{/gist_id}","starred_url":"https://api.github.com/users/marshall007/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marshall007/subscriptions","organizations_url":"https://api.github.com/users/marshall007/orgs","repos_url":"https://api.github.com/users/marshall007/repos","events_url":"https://api.github.com/users/marshall007/events{/privacy}","received_events_url":"https://api.github.com/users/marshall007/received_events","type":"User","site_admin":false},"body":"Supersedes #353. Rebased on top of the 1.0 updates, taking into account @Tryneus's feedback on the previous PR.\n\n<!-- Reviewable:start -->\n---\nThis change is [<img src=\"https://reviewable.io/review_button.svg\" height=\"35\" align=\"absmiddle\" alt=\"Reviewable\"/>](https://reviewable.io/reviews/rethinkdb/horizon/419)\n<!-- Reviewable:end -->\n","created_at":"2016-05-18T21:32:48Z","updated_at":"2016-05-19T02:02:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e331a7428bc71e2f6339ba74874b77a29d3e0e42","assignee":{"login":"Tryneus","id":552910,"avatar_url":"https://avatars.githubusercontent.com/u/552910?v=3","gravatar_id":"","url":"https://api.github.com/users/Tryneus","html_url":"https://github.com/Tryneus","followers_url":"https://api.github.com/users/Tryneus/followers","following_url":"https://api.github.com/users/Tryneus/following{/other_user}","gists_url":"https://api.github.com/users/Tryneus/gists{/gist_id}","starred_url":"https://api.github.com/users/Tryneus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tryneus/subscriptions","organizations_url":"https://api.github.com/users/Tryneus/orgs","repos_url":"https://api.github.com/users/Tryneus/repos","events_url":"https://api.github.com/users/Tryneus/events{/privacy}","received_events_url":"https://api.github.com/users/Tryneus/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/rethinkdb/horizon/milestones/6","html_url":"https://github.com/rethinkdb/horizon/milestones/Release%201.x.x","labels_url":"https://api.github.com/repos/rethinkdb/horizon/milestones/6/labels","id":1761314,"number":6,"title":"Release 1.x.x","description":"Release after 1.0","creator":{"login":"deontologician","id":1366,"avatar_url":"https://avatars.githubusercontent.com/u/1366?v=3","gravatar_id":"","url":"https://api.github.com/users/deontologician","html_url":"https://github.com/deontologician","followers_url":"https://api.github.com/users/deontologician/followers","following_url":"https://api.github.com/users/deontologician/following{/other_user}","gists_url":"https://api.github.com/users/deontologician/gists{/gist_id}","starred_url":"https://api.github.com/users/deontologician/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deontologician/subscriptions","organizations_url":"https://api.github.com/users/deontologician/orgs","repos_url":"https://api.github.com/users/deontologician/repos","events_url":"https://api.github.com/users/deontologician/events{/privacy}","received_events_url":"https://api.github.com/users/deontologician/received_events","type":"User","site_admin":false},"open_issues":28,"closed_issues":6,"state":"open","created_at":"2016-05-12T00:05:51Z","updated_at":"2016-05-19T01:50:58Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/rethinkdb/horizon/pulls/419/commits","review_comments_url":"https://api.github.com/repos/rethinkdb/horizon/pulls/419/comments","review_comment_url":"https://api.github.com/repos/rethinkdb/horizon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rethinkdb/horizon/issues/419/comments","statuses_url":"https://api.github.com/repos/rethinkdb/horizon/statuses/57efe17805ad62e9c6361572af22cef840f5e1dd","head":{"label":"marshall007:marshall_auth_2","ref":"marshall_auth_2","sha":"57efe17805ad62e9c6361572af22cef840f5e1dd","user":{"login":"marshall007","id":877936,"avatar_url":"https://avatars.githubusercontent.com/u/877936?v=3","gravatar_id":"","url":"https://api.github.com/users/marshall007","html_url":"https://github.com/marshall007","followers_url":"https://api.github.com/users/marshall007/followers","following_url":"https://api.github.com/users/marshall007/following{/other_user}","gists_url":"https://api.github.com/users/marshall007/gists{/gist_id}","starred_url":"https://api.github.com/users/marshall007/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marshall007/subscriptions","organizations_url":"https://api.github.com/users/marshall007/orgs","repos_url":"https://api.github.com/users/marshall007/repos","events_url":"https://api.github.com/users/marshall007/events{/privacy}","received_events_url":"https://api.github.com/users/marshall007/received_events","type":"User","site_admin":false},"repo":{"id":59152550,"name":"horizon","full_name":"marshall007/horizon","owner":{"login":"marshall007","id":877936,"avatar_url":"https://avatars.githubusercontent.com/u/877936?v=3","gravatar_id":"","url":"https://api.github.com/users/marshall007","html_url":"https://github.com/marshall007","followers_url":"https://api.github.com/users/marshall007/followers","following_url":"https://api.github.com/users/marshall007/following{/other_user}","gists_url":"https://api.github.com/users/marshall007/gists{/gist_id}","starred_url":"https://api.github.com/users/marshall007/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marshall007/subscriptions","organizations_url":"https://api.github.com/users/marshall007/orgs","repos_url":"https://api.github.com/users/marshall007/repos","events_url":"https://api.github.com/users/marshall007/events{/privacy}","received_events_url":"https://api.github.com/users/marshall007/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/marshall007/horizon","description":"Horizon is a realtime, open-source backend for JavaScript apps.","fork":true,"url":"https://api.github.com/repos/marshall007/horizon","forks_url":"https://api.github.com/repos/marshall007/horizon/forks","keys_url":"https://api.github.com/repos/marshall007/horizon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/marshall007/horizon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/marshall007/horizon/teams","hooks_url":"https://api.github.com/repos/marshall007/horizon/hooks","issue_events_url":"https://api.github.com/repos/marshall007/horizon/issues/events{/number}","events_url":"https://api.github.com/repos/marshall007/horizon/events","assignees_url":"https://api.github.com/repos/marshall007/horizon/assignees{/user}","branches_url":"https://api.github.com/repos/marshall007/horizon/branches{/branch}","tags_url":"https://api.github.com/repos/marshall007/horizon/tags","blobs_url":"https://api.github.com/repos/marshall007/horizon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/marshall007/horizon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/marshall007/horizon/git/refs{/sha}","trees_url":"https://api.github.com/repos/marshall007/horizon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/marshall007/horizon/statuses/{sha}","languages_url":"https://api.github.com/repos/marshall007/horizon/languages","stargazers_url":"https://api.github.com/repos/marshall007/horizon/stargazers","contributors_url":"https://api.github.com/repos/marshall007/horizon/contributors","subscribers_url":"https://api.github.com/repos/marshall007/horizon/subscribers","subscription_url":"https://api.github.com/repos/marshall007/horizon/subscription","commits_url":"https://api.github.com/repos/marshall007/horizon/commits{/sha}","git_commits_url":"https://api.github.com/repos/marshall007/horizon/git/commits{/sha}","comments_url":"https://api.github.com/repos/marshall007/horizon/comments{/number}","issue_comment_url":"https://api.github.com/repos/marshall007/horizon/issues/comments{/number}","contents_url":"https://api.github.com/repos/marshall007/horizon/contents/{+path}","compare_url":"https://api.github.com/repos/marshall007/horizon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/marshall007/horizon/merges","archive_url":"https://api.github.com/repos/marshall007/horizon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/marshall007/horizon/downloads","issues_url":"https://api.github.com/repos/marshall007/horizon/issues{/number}","pulls_url":"https://api.github.com/repos/marshall007/horizon/pulls{/number}","milestones_url":"https://api.github.com/repos/marshall007/horizon/milestones{/number}","notifications_url":"https://api.github.com/repos/marshall007/horizon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/marshall007/horizon/labels{/name}","releases_url":"https://api.github.com/repos/marshall007/horizon/releases{/id}","deployments_url":"https://api.github.com/repos/marshall007/horizon/deployments","created_at":"2016-05-18T21:28:39Z","updated_at":"2016-05-18T21:28:40Z","pushed_at":"2016-05-18T21:28:53Z","git_url":"git://github.com/marshall007/horizon.git","ssh_url":"git@github.com:marshall007/horizon.git","clone_url":"https://github.com/marshall007/horizon.git","svn_url":"https://github.com/marshall007/horizon","homepage":"https://horizon.io","size":6237,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"next"}},"base":{"label":"rethinkdb:next","ref":"next","sha":"d584324dee2533919a934a424a9e92924e5f1530","user":{"login":"rethinkdb","id":1229647,"avatar_url":"https://avatars.githubusercontent.com/u/1229647?v=3","gravatar_id":"","url":"https://api.github.com/users/rethinkdb","html_url":"https://github.com/rethinkdb","followers_url":"https://api.github.com/users/rethinkdb/followers","following_url":"https://api.github.com/users/rethinkdb/following{/other_user}","gists_url":"https://api.github.com/users/rethinkdb/gists{/gist_id}","starred_url":"https://api.github.com/users/rethinkdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rethinkdb/subscriptions","organizations_url":"https://api.github.com/users/rethinkdb/orgs","repos_url":"https://api.github.com/users/rethinkdb/repos","events_url":"https://api.github.com/users/rethinkdb/events{/privacy}","received_events_url":"https://api.github.com/users/rethinkdb/received_events","type":"Organization","site_admin":false},"repo":{"id":45091124,"name":"horizon","full_name":"rethinkdb/horizon","owner":{"login":"rethinkdb","id":1229647,"avatar_url":"https://avatars.githubusercontent.com/u/1229647?v=3","gravatar_id":"","url":"https://api.github.com/users/rethinkdb","html_url":"https://github.com/rethinkdb","followers_url":"https://api.github.com/users/rethinkdb/followers","following_url":"https://api.github.com/users/rethinkdb/following{/other_user}","gists_url":"https://api.github.com/users/rethinkdb/gists{/gist_id}","starred_url":"https://api.github.com/users/rethinkdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rethinkdb/subscriptions","organizations_url":"https://api.github.com/users/rethinkdb/orgs","repos_url":"https://api.github.com/users/rethinkdb/repos","events_url":"https://api.github.com/users/rethinkdb/events{/privacy}","received_events_url":"https://api.github.com/users/rethinkdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rethinkdb/horizon","description":"Horizon is a realtime, open-source backend for JavaScript apps.","fork":false,"url":"https://api.github.com/repos/rethinkdb/horizon","forks_url":"https://api.github.com/repos/rethinkdb/horizon/forks","keys_url":"https://api.github.com/repos/rethinkdb/horizon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rethinkdb/horizon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rethinkdb/horizon/teams","hooks_url":"https://api.github.com/repos/rethinkdb/horizon/hooks","issue_events_url":"https://api.github.com/repos/rethinkdb/horizon/issues/events{/number}","events_url":"https://api.github.com/repos/rethinkdb/horizon/events","assignees_url":"https://api.github.com/repos/rethinkdb/horizon/assignees{/user}","branches_url":"https://api.github.com/repos/rethinkdb/horizon/branches{/branch}","tags_url":"https://api.github.com/repos/rethinkdb/horizon/tags","blobs_url":"https://api.github.com/repos/rethinkdb/horizon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rethinkdb/horizon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rethinkdb/horizon/git/refs{/sha}","trees_url":"https://api.github.com/repos/rethinkdb/horizon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rethinkdb/horizon/statuses/{sha}","languages_url":"https://api.github.com/repos/rethinkdb/horizon/languages","stargazers_url":"https://api.github.com/repos/rethinkdb/horizon/stargazers","contributors_url":"https://api.github.com/repos/rethinkdb/horizon/contributors","subscribers_url":"https://api.github.com/repos/rethinkdb/horizon/subscribers","subscription_url":"https://api.github.com/repos/rethinkdb/horizon/subscription","commits_url":"https://api.github.com/repos/rethinkdb/horizon/commits{/sha}","git_commits_url":"https://api.github.com/repos/rethinkdb/horizon/git/commits{/sha}","comments_url":"https://api.github.com/repos/rethinkdb/horizon/comments{/number}","issue_comment_url":"https://api.github.com/repos/rethinkdb/horizon/issues/comments{/number}","contents_url":"https://api.github.com/repos/rethinkdb/horizon/contents/{+path}","compare_url":"https://api.github.com/repos/rethinkdb/horizon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rethinkdb/horizon/merges","archive_url":"https://api.github.com/repos/rethinkdb/horizon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rethinkdb/horizon/downloads","issues_url":"https://api.github.com/repos/rethinkdb/horizon/issues{/number}","pulls_url":"https://api.github.com/repos/rethinkdb/horizon/pulls{/number}","milestones_url":"https://api.github.com/repos/rethinkdb/horizon/milestones{/number}","notifications_url":"https://api.github.com/repos/rethinkdb/horizon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rethinkdb/horizon/labels{/name}","releases_url":"https://api.github.com/repos/rethinkdb/horizon/releases{/id}","deployments_url":"https://api.github.com/repos/rethinkdb/horizon/deployments","created_at":"2015-10-28T05:33:14Z","updated_at":"2016-05-19T02:00:04Z","pushed_at":"2016-05-19T01:50:50Z","git_url":"git://github.com/rethinkdb/horizon.git","ssh_url":"git@github.com:rethinkdb/horizon.git","clone_url":"https://github.com/rethinkdb/horizon.git","svn_url":"https://github.com/rethinkdb/horizon","homepage":"https://horizon.io","size":6436,"stargazers_count":2094,"watchers_count":2094,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":31,"mirror_url":null,"open_issues_count":117,"forks":31,"open_issues":117,"watchers":2094,"default_branch":"next"}},"_links":{"self":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/419"},"html":{"href":"https://github.com/rethinkdb/horizon/pull/419"},"issue":{"href":"https://api.github.com/repos/rethinkdb/horizon/issues/419"},"comments":{"href":"https://api.github.com/repos/rethinkdb/horizon/issues/419/comments"},"review_comments":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/419/comments"},"review_comment":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rethinkdb/horizon/pulls/419/commits"},"statuses":{"href":"https://api.github.com/repos/rethinkdb/horizon/statuses/57efe17805ad62e9c6361572af22cef840f5e1dd"}}}}