api.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. ;(function($, navigator, window_location) {
  5. 'use strict';
  6. // Public API
  7. var django_browserid = {
  8. /**
  9. * Retrieve an assertion and use it to log the user into your site.
  10. * @param {object} requestArgs Options to pass to navigator.id.request.
  11. * @param {string} next URL to redirect the user to if login is
  12. * successful.
  13. * @return {jQuery.Deferred} Deferred that resolves once the user has
  14. * been logged in.
  15. */
  16. login: function login(requestArgs, next) {
  17. if (typeof requestArgs === 'string') {
  18. next = requestArgs;
  19. requestArgs = undefined;
  20. }
  21. return django_browserid.getAssertion(requestArgs).then(function(assertion) {
  22. return django_browserid.verifyAssertion(assertion, next);
  23. });
  24. },
  25. /**
  26. * Log the user out of your site.
  27. * @param {string} next URL to redirect the user to if logout is
  28. * successful.
  29. * @return {jQuery.Deferred} Deferred that resolves once the user has
  30. * been logged out.
  31. */
  32. logout: function logout(next) {
  33. var info = this.getInfo();
  34. return this.getCsrfToken().then(function(csrfToken) {
  35. return $.ajax(info.logoutUrl, {
  36. type: 'POST',
  37. data: {next: next},
  38. headers: {'X-CSRFToken': csrfToken},
  39. });
  40. });
  41. },
  42. /**
  43. * Retrieve an assertion via BrowserID.
  44. * @param {object} requestArgs Options to pass to navigator.id.request.
  45. * @return {jQuery.Deferred} Deferred that resolves with the assertion
  46. * once it is retrieved.
  47. */
  48. getAssertion: function getAssertion(requestArgs) {
  49. requestArgs = $.extend({}, this.getInfo().requestArgs, requestArgs);
  50. this._requestDeferred = $.Deferred();
  51. navigator.id.request(requestArgs);
  52. return this._requestDeferred;
  53. },
  54. /**
  55. * Verify that the given assertion is valid, and log the user in.
  56. * @param {string} assertion Assertion to verify.
  57. * @param {string} next URL to redirect the user to if the assertion is
  58. * valid.
  59. * @return {jQuery.Deferred} Deferred that resolves with the login view
  60. * response once login is complete.
  61. */
  62. verifyAssertion: function verifyAssertion(assertion, next) {
  63. var info = this.getInfo();
  64. return this.getCsrfToken().then(function(csrfToken) {
  65. return $.ajax(info.loginUrl, {
  66. type: 'POST',
  67. data: {assertion: assertion, next: next},
  68. headers: {'X-CSRFToken': csrfToken},
  69. });
  70. });
  71. },
  72. // Cache for the info fetched by django_browserid.getInfo().
  73. _info: null,
  74. /**
  75. * Fetch the info for the Persona popup and login requests.
  76. * @return {object} Data encoded in the browserid-info tag.
  77. */
  78. getInfo: function getInfo() {
  79. if (!this._info) {
  80. this._info = $('#browserid-info').data('info');
  81. }
  82. return this._info;
  83. },
  84. /**
  85. * Fetch a CSRF token from the backend.
  86. * @return {jqXHR} jQuery XmlHttpResponse that returns the token.
  87. */
  88. getCsrfToken: function getCsrfToken() {
  89. return $.get(this.getInfo().csrfUrl);
  90. },
  91. // Deferred for post-watch-callback actions.
  92. // Stored on the public API so tests can reset it.
  93. _requestDeferred: null,
  94. /**
  95. * Register callbacks with navigator.id.watch that make the API work.
  96. * This must be called before calling any other API methods.
  97. * @param {function} Function to run once the user agent is ready to
  98. * process login requests.
  99. */
  100. registerWatchHandlers: function registerWatchHandlers(onReady) {
  101. var assertion = null;
  102. var self = this;
  103. navigator.id.watch({
  104. onlogin: function(assertion) {
  105. if (self._requestDeferred) {
  106. self._requestDeferred.resolve(assertion);
  107. }
  108. },
  109. onready: onReady,
  110. });
  111. }
  112. };
  113. window.django_browserid = django_browserid;
  114. })(window.jQuery, window.navigator, window.location);