API Docs for: 0.0.8
Show:

File: client/token/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. define([
  5. 'p-promise',
  6. 'client/lib/constants',
  7. 'client/lib/xhr'
  8. ], function (p, Constants, Xhr) {
  9. 'use strict';
  10. /**
  11. * @class TokenAPI
  12. * @constructor
  13. * @param {string} clientId - the OAuth client ID for the relier
  14. * @param {Object} [options={}] - configuration
  15. * @param {String} [options.clientSecret]
  16. * Client secret
  17. * @param {String} [options.oauthHost]
  18. * Firefox Accounts OAuth Server host
  19. */
  20. function TokenAPI(clientId, options) {
  21. if (! clientId) {
  22. throw new Error('clientId is required');
  23. }
  24. this._clientId = clientId;
  25. options = options || {};
  26. this._clientSecret = options.clientSecret;
  27. this._oauthHost = options.oauthHost || Constants.DEFAULT_OAUTH_HOST;
  28. }
  29. TokenAPI.prototype = {
  30. /**
  31. * Trade an OAuth code for a longer lived OAuth token. See
  32. * https://github.com/mozilla/fxa-oauth-server/blob/master/docs/api.md#post-v1token
  33. *
  34. * @method tradeCode
  35. * @param {String} code
  36. * OAuth code
  37. * @returns {String}
  38. * OAuth token
  39. * @param {Object} [options={}] - configuration
  40. * @param {String} [options.xhr]
  41. * XMLHttpRequest compatible object to use to make the request.
  42. * @returns {Promise}
  43. * Response resolves to an object with `access_token`, `scope`, and
  44. * `token_type`.
  45. */
  46. tradeCode: function (code, options) {
  47. if (! this._clientSecret) {
  48. return p.reject(new Error('clientSecret is required'));
  49. }
  50. if (! code) {
  51. return p.reject(new Error('code is required'));
  52. }
  53. var endpoint = this._oauthHost + '/token';
  54. return Xhr.post(endpoint, {
  55. client_id: this._clientId,
  56. client_secret: this._clientSecret,
  57. code: code
  58. }, options);
  59. },
  60. /**
  61. * Verify an OAuth token is valid. See
  62. * https://github.com/mozilla/fxa-oauth-server/blob/master/docs/api.md#post-v1verify
  63. *
  64. * @method verifyToken
  65. * @param {String} token
  66. * OAuth token to verify
  67. * @param {Object} [options={}] - configuration
  68. * @param {String} [options.xhr]
  69. * XMLHttpRequest compatible object to use to make the request.
  70. * @returns {Promise}
  71. * Response resolves to an object with `user`, `client_id`, and
  72. * `scopes`.
  73. */
  74. verifyToken: function (token, options) {
  75. if (! token) {
  76. return p.reject(new Error('token is required'));
  77. }
  78. var endpoint = this._oauthHost + '/verify';
  79. return Xhr.post(endpoint, {
  80. token: token
  81. }, options);
  82. },
  83. /**
  84. * After a client is done using a token, the responsible thing to do is to
  85. * destroy the token afterwards.
  86. * See https://github.com/mozilla/fxa-oauth-server/blob/master/docs/api.md#post-v1destroy
  87. *
  88. * @method destroyToken
  89. * @param {String} token
  90. * OAuth token to verify
  91. * @param {Object} [options={}] - configuration
  92. * @param {String} [options.xhr]
  93. * XMLHttpRequest compatible object to use to make the request.
  94. * @returns {Promise}
  95. * Response resolves to an empty object.
  96. */
  97. destroyToken: function (token, options) {
  98. if (! this._clientSecret) {
  99. return p.reject(new Error('clientSecret is required'));
  100. }
  101. if (! token) {
  102. return p.reject(new Error('token is required'));
  103. }
  104. var endpoint = this._oauthHost + '/destroy';
  105. return Xhr.post(endpoint, {
  106. client_secret: this._clientSecret,
  107. token: token
  108. }, options);
  109. }
  110. };
  111. return TokenAPI;
  112. });