{"action":"created","comment":{"url":"https://api.github.com/repos/juju/juju-gui/pulls/comments/40677296","id":40677296,"diff_hunk":"@@ -0,0 +1,339 @@\n+/*\n+This file is part of the Juju GUI, which lets users view and manage Juju\n+environments within a graphical interface (https://launchpad.net/juju-gui).\n+Copyright (C) 2012-2015 Canonical Ltd.\n+\n+This program is free software: you can redistribute it and/or modify it under\n+the terms of the GNU Affero General Public License version 3, as published by\n+the Free Software Foundation.\n+\n+This program is distributed in the hope that it will be useful, but WITHOUT\n+ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,\n+SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero\n+General Public License for more details.\n+\n+You should have received a copy of the GNU Affero General Public License along\n+with this program.  If not, see <http://www.gnu.org/licenses/>.\n+*/\n+\n+'use strict';\n+\n+/**\n+ * Bakery holds the context for making HTTP requests\n+ * that automatically acquire and discharge macaroons.\n+ *\n+ * @module env\n+ * @submodule env.bakery\n+ */\n+\n+YUI.add('juju-env-bakery', function(Y) {\n+\n+  var module = Y.namespace('juju.environments.web');\n+  var macaroon = Y.namespace('macaroon');\n+\n+  /**\n+   * Bakery client inspired by the equivalent GO code.\n+   *\n+   * This object exposes the ability to perform requests\n+   * that automatically acquire and discharge macaroons\n+   *\n+   * @class Bakery\n+   */\n+\n+  var Bakery = Y.Base.create('Bakery',\n+      Y.Base, [], {\n+\n+      /**\n+       Initialize.\n+\n+       @method initializer\n+       @return {undefined} Nothing.\n+       */\n+      initializer: function () {\n+        this.webhandler = new Y.juju.environments.web.WebHandler();\n+      },\n+\n+      /**\n+       Send get request.\n+\n+       @param {String} The path to make the api request to.\n+       @param {String} The path to make a request to set the cookie.\n+       @param {Function} successCallback Called when the api request completes\n+       successfully.\n+       @param {Function} failureCallback Called when the api request fails\n+       with a response of >= 400 except 401/407 where it does authentication.\n+       */\n+      sendGetRequest: function (path, setCookiePath,\n+                                successCallback, failureCallback) {\n+        var macaroons = Y.Cookie.get('Macaroons');\n+        var headers = {'Bakery-Protocol-Version': 1};\n+        if (macaroons !== null) {\n+          headers['Macaroons'] = macaroons;\n+        }\n+\n+        this.webhandler.sendGetRequest(\n+          path, headers, null, null, false, null,\n+          this._requestHandlerWithInteraction.bind(\n+            this,\n+            path,\n+            setCookiePath,\n+            successCallback,\n+            failureCallback\n+          )\n+        );\n+      },\n+\n+      /**\n+       Handles the request response from the _makeRequest method, calling the\n+       supplied failure callback if the response status was >= 400 or passing\n+       the response object to the supplied success callback. For 407/401\n+       response it will request authentication through the macaroon provided in\n+       the 401/407 response.\n+\n+       @method _requestHandlerWithInteraction\n+       @param {String} The path to make the api request to.\n+       @param {String} The path to make a request to set the cookie.\n+       @param {Function} successCallback Called when the api request completes\n+       successfully.\n+       @param {Function} failureCallback Called when the api request fails\n+       with a response of >= 400 (except 401/407).\n+       @param {Object} response The XHR response object.\n+       */\n+      _requestHandlerWithInteraction: function (path,\n+                                                setCookiePath,\n+                                                successCallback,\n+                                                failureCallback,\n+                                                response) {\n+        var target = response.target;\n+        if (target.status === 401 &&\n+          target.getResponseHeader('Www-Authenticate') === 'Macaroon') {\n+          var jsonResponse = JSON.parse(target.responseText);\n+          this._authenticate(\n+            jsonResponse.Info.Macaroon,\n+            setCookiePath,\n+            this._sendOriginalRequest.bind(\n+              this, path, successCallback, failureCallback\n+            ),\n+            failureCallback\n+          );\n+        } else {\n+          this._requestHandler(successCallback, failureCallback, response);\n+        }\n+      },\n+\n+      /**\n+       Used to resend the original request without any interaction this time..\n+\n+       @method _sendOriginalRequest\n+       @param {String} The path to make the api request to.\n+       @param {Function} successCallback Called when the api request completes\n+       successfully.\n+       @param {Function} failureCallback Called when the api request fails\n+       with a response of >= 400 (except 401/407).\n+       */\n+      _sendOriginalRequest: function(path, successCallback, failureCallback) {\n+          var macaroons = Y.Cookie.get('Macaroons');\n+          var headers = {'Bakery-Protocol-Version': 1};\n+          if (macaroons !== null) {\n+            headers['Macaroons'] = macaroons;\n+          }\n+          this.webhandler.sendGetRequest(\n+            path, headers, null, null, false, null,\n+            this._requestHandler.bind(\n+              this, successCallback, failureCallback\n+            )\n+          );\n+      },\n+\n+      /**\n+       Handles the request response from the _makeRequest method, calling the\n+       supplied failure callback if the response status was >= 400 or passing\n+       the response object to the supplied success callback.\n+\n+       @method _requestHandler\n+       @param {Function} successCallback Called when the api request completes\n+       successfully.\n+       @param {Function} failureCallback Called when the api request fails\n+       with a response of >= 400.\n+       @param {Object} response The XHR response object.\n+       */\n+      _requestHandler: function (successCallback, failureCallback, response) {\n+        var target = response.target;\n+        if (target.status >= 400) {\n+          failureCallback(response);\n+          return;\n+        }\n+        successCallback(response);\n+      },\n+\n+      /**\n+       Authenticate  by discharging the macaroon and","path":"jujugui/static/gui/src/app/store/env/bakery.js","position":170,"original_position":170,"commit_id":"91a7356933efb0008944aab822d787a394e58e09","original_commit_id":"91a7356933efb0008944aab822d787a394e58e09","user":{"login":"hatched","id":532033,"avatar_url":"https://avatars.githubusercontent.com/u/532033?v=3","gravatar_id":"","url":"https://api.github.com/users/hatched","html_url":"https://github.com/hatched","followers_url":"https://api.github.com/users/hatched/followers","following_url":"https://api.github.com/users/hatched/following{/other_user}","gists_url":"https://api.github.com/users/hatched/gists{/gist_id}","starred_url":"https://api.github.com/users/hatched/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hatched/subscriptions","organizations_url":"https://api.github.com/users/hatched/orgs","repos_url":"https://api.github.com/users/hatched/repos","events_url":"https://api.github.com/users/hatched/events{/privacy}","received_events_url":"https://api.github.com/users/hatched/received_events","type":"User","site_admin":false},"body":"extra space","created_at":"2015-09-29T14:24:03Z","updated_at":"2015-09-29T14:24:03Z","html_url":"https://github.com/juju/juju-gui/pull/830#discussion_r40677296","pull_request_url":"https://api.github.com/repos/juju/juju-gui/pulls/830","_links":{"self":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/comments/40677296"},"html":{"href":"https://github.com/juju/juju-gui/pull/830#discussion_r40677296"},"pull_request":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/830"}}},"pull_request":{"url":"https://api.github.com/repos/juju/juju-gui/pulls/830","id":44783503,"html_url":"https://github.com/juju/juju-gui/pull/830","diff_url":"https://github.com/juju/juju-gui/pull/830.diff","patch_url":"https://github.com/juju/juju-gui/pull/830.patch","issue_url":"https://api.github.com/repos/juju/juju-gui/issues/830","number":830,"state":"open","locked":false,"title":"Support for JS MAcaroon and bakery port from GO.","user":{"login":"fabricematrat","id":1395842,"avatar_url":"https://avatars.githubusercontent.com/u/1395842?v=3","gravatar_id":"","url":"https://api.github.com/users/fabricematrat","html_url":"https://github.com/fabricematrat","followers_url":"https://api.github.com/users/fabricematrat/followers","following_url":"https://api.github.com/users/fabricematrat/following{/other_user}","gists_url":"https://api.github.com/users/fabricematrat/gists{/gist_id}","starred_url":"https://api.github.com/users/fabricematrat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabricematrat/subscriptions","organizations_url":"https://api.github.com/users/fabricematrat/orgs","repos_url":"https://api.github.com/users/fabricematrat/repos","events_url":"https://api.github.com/users/fabricematrat/events{/privacy}","received_events_url":"https://api.github.com/users/fabricematrat/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-11T13:42:20Z","updated_at":"2015-09-29T14:24:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1d6c6c816d3e5e74f12b6e6c77a0ec57076b0661","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/juju/juju-gui/pulls/830/commits","review_comments_url":"https://api.github.com/repos/juju/juju-gui/pulls/830/comments","review_comment_url":"https://api.github.com/repos/juju/juju-gui/pulls/comments{/number}","comments_url":"https://api.github.com/repos/juju/juju-gui/issues/830/comments","statuses_url":"https://api.github.com/repos/juju/juju-gui/statuses/91a7356933efb0008944aab822d787a394e58e09","head":{"label":"fabricematrat:rendez-vous","ref":"rendez-vous","sha":"91a7356933efb0008944aab822d787a394e58e09","user":{"login":"fabricematrat","id":1395842,"avatar_url":"https://avatars.githubusercontent.com/u/1395842?v=3","gravatar_id":"","url":"https://api.github.com/users/fabricematrat","html_url":"https://github.com/fabricematrat","followers_url":"https://api.github.com/users/fabricematrat/followers","following_url":"https://api.github.com/users/fabricematrat/following{/other_user}","gists_url":"https://api.github.com/users/fabricematrat/gists{/gist_id}","starred_url":"https://api.github.com/users/fabricematrat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabricematrat/subscriptions","organizations_url":"https://api.github.com/users/fabricematrat/orgs","repos_url":"https://api.github.com/users/fabricematrat/repos","events_url":"https://api.github.com/users/fabricematrat/events{/privacy}","received_events_url":"https://api.github.com/users/fabricematrat/received_events","type":"User","site_admin":false},"repo":{"id":41755836,"name":"juju-gui","full_name":"fabricematrat/juju-gui","owner":{"login":"fabricematrat","id":1395842,"avatar_url":"https://avatars.githubusercontent.com/u/1395842?v=3","gravatar_id":"","url":"https://api.github.com/users/fabricematrat","html_url":"https://github.com/fabricematrat","followers_url":"https://api.github.com/users/fabricematrat/followers","following_url":"https://api.github.com/users/fabricematrat/following{/other_user}","gists_url":"https://api.github.com/users/fabricematrat/gists{/gist_id}","starred_url":"https://api.github.com/users/fabricematrat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabricematrat/subscriptions","organizations_url":"https://api.github.com/users/fabricematrat/orgs","repos_url":"https://api.github.com/users/fabricematrat/repos","events_url":"https://api.github.com/users/fabricematrat/events{/privacy}","received_events_url":"https://api.github.com/users/fabricematrat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/fabricematrat/juju-gui","description":"Juju-GUI is a web-based GUI for Juju <https://juju.ubuntu.com/>.","fork":true,"url":"https://api.github.com/repos/fabricematrat/juju-gui","forks_url":"https://api.github.com/repos/fabricematrat/juju-gui/forks","keys_url":"https://api.github.com/repos/fabricematrat/juju-gui/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fabricematrat/juju-gui/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fabricematrat/juju-gui/teams","hooks_url":"https://api.github.com/repos/fabricematrat/juju-gui/hooks","issue_events_url":"https://api.github.com/repos/fabricematrat/juju-gui/issues/events{/number}","events_url":"https://api.github.com/repos/fabricematrat/juju-gui/events","assignees_url":"https://api.github.com/repos/fabricematrat/juju-gui/assignees{/user}","branches_url":"https://api.github.com/repos/fabricematrat/juju-gui/branches{/branch}","tags_url":"https://api.github.com/repos/fabricematrat/juju-gui/tags","blobs_url":"https://api.github.com/repos/fabricematrat/juju-gui/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fabricematrat/juju-gui/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fabricematrat/juju-gui/git/refs{/sha}","trees_url":"https://api.github.com/repos/fabricematrat/juju-gui/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fabricematrat/juju-gui/statuses/{sha}","languages_url":"https://api.github.com/repos/fabricematrat/juju-gui/languages","stargazers_url":"https://api.github.com/repos/fabricematrat/juju-gui/stargazers","contributors_url":"https://api.github.com/repos/fabricematrat/juju-gui/contributors","subscribers_url":"https://api.github.com/repos/fabricematrat/juju-gui/subscribers","subscription_url":"https://api.github.com/repos/fabricematrat/juju-gui/subscription","commits_url":"https://api.github.com/repos/fabricematrat/juju-gui/commits{/sha}","git_commits_url":"https://api.github.com/repos/fabricematrat/juju-gui/git/commits{/sha}","comments_url":"https://api.github.com/repos/fabricematrat/juju-gui/comments{/number}","issue_comment_url":"https://api.github.com/repos/fabricematrat/juju-gui/issues/comments{/number}","contents_url":"https://api.github.com/repos/fabricematrat/juju-gui/contents/{+path}","compare_url":"https://api.github.com/repos/fabricematrat/juju-gui/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fabricematrat/juju-gui/merges","archive_url":"https://api.github.com/repos/fabricematrat/juju-gui/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fabricematrat/juju-gui/downloads","issues_url":"https://api.github.com/repos/fabricematrat/juju-gui/issues{/number}","pulls_url":"https://api.github.com/repos/fabricematrat/juju-gui/pulls{/number}","milestones_url":"https://api.github.com/repos/fabricematrat/juju-gui/milestones{/number}","notifications_url":"https://api.github.com/repos/fabricematrat/juju-gui/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fabricematrat/juju-gui/labels{/name}","releases_url":"https://api.github.com/repos/fabricematrat/juju-gui/releases{/id}","created_at":"2015-09-01T18:28:33Z","updated_at":"2015-09-01T18:28:35Z","pushed_at":"2015-09-29T14:12:01Z","git_url":"git://github.com/fabricematrat/juju-gui.git","ssh_url":"git@github.com:fabricematrat/juju-gui.git","clone_url":"https://github.com/fabricematrat/juju-gui.git","svn_url":"https://github.com/fabricematrat/juju-gui","homepage":null,"size":109692,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":0,"default_branch":"develop"}},"base":{"label":"juju:pyramid-fork","ref":"pyramid-fork","sha":"d09d4b78cb0fd889b0a1a70387a6cc3ffe5810cf","user":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"repo":{"id":14960387,"name":"juju-gui","full_name":"juju/juju-gui","owner":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/juju/juju-gui","description":"Juju-GUI is a web-based GUI for Juju <https://juju.ubuntu.com/>.","fork":false,"url":"https://api.github.com/repos/juju/juju-gui","forks_url":"https://api.github.com/repos/juju/juju-gui/forks","keys_url":"https://api.github.com/repos/juju/juju-gui/keys{/key_id}","collaborators_url":"https://api.github.com/repos/juju/juju-gui/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/juju/juju-gui/teams","hooks_url":"https://api.github.com/repos/juju/juju-gui/hooks","issue_events_url":"https://api.github.com/repos/juju/juju-gui/issues/events{/number}","events_url":"https://api.github.com/repos/juju/juju-gui/events","assignees_url":"https://api.github.com/repos/juju/juju-gui/assignees{/user}","branches_url":"https://api.github.com/repos/juju/juju-gui/branches{/branch}","tags_url":"https://api.github.com/repos/juju/juju-gui/tags","blobs_url":"https://api.github.com/repos/juju/juju-gui/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/juju/juju-gui/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/juju/juju-gui/git/refs{/sha}","trees_url":"https://api.github.com/repos/juju/juju-gui/git/trees{/sha}","statuses_url":"https://api.github.com/repos/juju/juju-gui/statuses/{sha}","languages_url":"https://api.github.com/repos/juju/juju-gui/languages","stargazers_url":"https://api.github.com/repos/juju/juju-gui/stargazers","contributors_url":"https://api.github.com/repos/juju/juju-gui/contributors","subscribers_url":"https://api.github.com/repos/juju/juju-gui/subscribers","subscription_url":"https://api.github.com/repos/juju/juju-gui/subscription","commits_url":"https://api.github.com/repos/juju/juju-gui/commits{/sha}","git_commits_url":"https://api.github.com/repos/juju/juju-gui/git/commits{/sha}","comments_url":"https://api.github.com/repos/juju/juju-gui/comments{/number}","issue_comment_url":"https://api.github.com/repos/juju/juju-gui/issues/comments{/number}","contents_url":"https://api.github.com/repos/juju/juju-gui/contents/{+path}","compare_url":"https://api.github.com/repos/juju/juju-gui/compare/{base}...{head}","merges_url":"https://api.github.com/repos/juju/juju-gui/merges","archive_url":"https://api.github.com/repos/juju/juju-gui/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/juju/juju-gui/downloads","issues_url":"https://api.github.com/repos/juju/juju-gui/issues{/number}","pulls_url":"https://api.github.com/repos/juju/juju-gui/pulls{/number}","milestones_url":"https://api.github.com/repos/juju/juju-gui/milestones{/number}","notifications_url":"https://api.github.com/repos/juju/juju-gui/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/juju/juju-gui/labels{/name}","releases_url":"https://api.github.com/repos/juju/juju-gui/releases{/id}","created_at":"2013-12-05T17:15:48Z","updated_at":"2015-08-31T23:27:31Z","pushed_at":"2015-09-29T14:05:15Z","git_url":"git://github.com/juju/juju-gui.git","ssh_url":"git@github.com:juju/juju-gui.git","clone_url":"https://github.com/juju/juju-gui.git","svn_url":"https://github.com/juju/juju-gui","homepage":null,"size":116110,"stargazers_count":54,"watchers_count":54,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":41,"mirror_url":null,"open_issues_count":7,"forks":41,"open_issues":7,"watchers":54,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/830"},"html":{"href":"https://github.com/juju/juju-gui/pull/830"},"issue":{"href":"https://api.github.com/repos/juju/juju-gui/issues/830"},"comments":{"href":"https://api.github.com/repos/juju/juju-gui/issues/830/comments"},"review_comments":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/830/comments"},"review_comment":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/juju/juju-gui/pulls/830/commits"},"statuses":{"href":"https://api.github.com/repos/juju/juju-gui/statuses/91a7356933efb0008944aab822d787a394e58e09"}}}}