API Docs for: 0.0.8
Show:

File: client/auth/lightbox/api.js

  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. /*globals define*/
  5. define([
  6. 'p-promise',
  7. 'client/lib/object',
  8. 'client/lib/options',
  9. 'client/lib/constants',
  10. '../base/api',
  11. './lightbox',
  12. './iframe_channel'
  13. ], function (p, ObjectHelpers, Options, Constants, BaseBroker,
  14. Lightbox, IFrameChannel) {
  15. 'use strict';
  16. function getLightbox() {
  17. //jshint validthis: true
  18. var self = this;
  19. if (self._lightbox) {
  20. return self._lightbox;
  21. }
  22. self._lightbox = new Lightbox({
  23. window: self._window
  24. });
  25. return self._lightbox;
  26. }
  27. function openLightbox(fxaUrl, options) {
  28. /*jshint validthis: true*/
  29. var self = this;
  30. return p().then(function() {
  31. if (self._lightbox && self._lightbox.isLoaded()) {
  32. throw new Error('lightbox already open');
  33. }
  34. var lightbox = getLightbox.call(self);
  35. lightbox.load(fxaUrl, options);
  36. return lightbox;
  37. });
  38. }
  39. function getChannel(lightbox) {
  40. //jshint validthis: true
  41. var self = this;
  42. if (self._channel) {
  43. return self._channel;
  44. }
  45. self._channel = new IFrameChannel({
  46. iframeHost: self._contentHost,
  47. contentWindow: lightbox.getContentWindow(),
  48. window: self._window
  49. });
  50. return self._channel;
  51. }
  52. function waitForAuthentication(lightbox) {
  53. /*jshint validthis: true*/
  54. var self = this;
  55. return p().then(function () {
  56. var channel = getChannel.call(self, lightbox);
  57. return channel.attach();
  58. });
  59. }
  60. /**
  61. * Authenticate users with a lightbox
  62. *
  63. * @class LightboxBroker
  64. * @extends BaseBroker
  65. * @constructor
  66. * @param {string} clientId - the OAuth client ID for the relier
  67. * @param {Object} [options={}] - configuration
  68. * @param {String} [options.contentHost]
  69. * Firefox Accounts Content Server host
  70. * @param {String} [options.oauthHost]
  71. * Firefox Accounts OAuth Server host
  72. * @param {Object} [options.window]
  73. * window override, used for unit tests
  74. * @param {Object} [options.lightbox]
  75. * lightbox override, used for unit tests
  76. * @param {Object} [options.channel]
  77. * channel override, used for unit tests
  78. */
  79. function LightboxBroker(clientId, options) {
  80. options = options || {};
  81. BaseBroker.call(this, clientId, options);
  82. this._lightbox = options.lightbox;
  83. this._channel = options.channel;
  84. this._contentHost = options.contentHost || Constants.DEFAULT_CONTENT_HOST;
  85. this.setContext('iframe');
  86. }
  87. LightboxBroker.prototype = Object.create(BaseBroker.prototype);
  88. ObjectHelpers.extend(LightboxBroker.prototype, {
  89. openFxa: function (fxaUrl, options) {
  90. /*jshint validthis: true*/
  91. var self = this;
  92. return openLightbox.call(self, fxaUrl, options)
  93. .then(function (lightbox) {
  94. return waitForAuthentication.call(self, lightbox);
  95. })
  96. .then(function (result) {
  97. self.unload();
  98. return result;
  99. }, function (err) {
  100. self.unload();
  101. throw err;
  102. });
  103. },
  104. /**
  105. * Unload the lightbox
  106. *
  107. * @method unload
  108. */
  109. unload: function () {
  110. var self = this;
  111. return p().then(function () {
  112. if (! self._lightbox) {
  113. throw new Error('lightbox not open');
  114. }
  115. self._lightbox.unload();
  116. self._channel.detach();
  117. });
  118. }
  119. });
  120. return LightboxBroker;
  121. });