Source: sessions/basic-session.ts

  1. /**
  2. * @fileoverview A Basic Box API Session.
  3. */
  4. // ------------------------------------------------------------------------------
  5. // Requirements
  6. // ------------------------------------------------------------------------------
  7. import { Promise } from 'bluebird';
  8. // ------------------------------------------------------------------------------
  9. // Typedefs
  10. // ------------------------------------------------------------------------------
  11. type TokenManager = any /* FIXME */;
  12. type TokenRequestOptions = Record<string, any> /*FIXME*/;
  13. // ------------------------------------------------------------------------------
  14. // Public
  15. // ------------------------------------------------------------------------------
  16. /**
  17. * A BasicSession holds only a single accessToken. It has no idea how to authenticate,
  18. * refresh, or persist its token information. When that token expires, the session
  19. * and any clients using it will become useless.
  20. *
  21. * Basic API Session is the most simple API Session to use, which makes it a good choice
  22. * for simple applications, developers who are just getting started, and applications
  23. * that wish to manage tokens themselves.
  24. *
  25. * @param {string} accessToken The existing access token for a user
  26. * @param {TokenManager} tokenManager The token manager
  27. * @constructor
  28. */
  29. class BasicSession {
  30. _accessToken: string;
  31. _tokenManager: TokenManager;
  32. constructor(accessToken: string, tokenManager: TokenManager) {
  33. this._accessToken = accessToken;
  34. this._tokenManager = tokenManager;
  35. }
  36. /**
  37. * Returns the clients access token. BasicSession never returns an error, since it doesn't
  38. * know the status of its own token.
  39. *
  40. * @returns {Promise<string>} Promise resolving to the access token
  41. */
  42. getAccessToken() {
  43. return Promise.resolve(this._accessToken);
  44. }
  45. /**
  46. * Revokes the session's access token.
  47. *
  48. * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
  49. * @returns {Promise} Promise resolving if the revoke succeeds
  50. */
  51. revokeTokens(options?: TokenRequestOptions) {
  52. return this._tokenManager.revokeTokens(this._accessToken, options);
  53. }
  54. /**
  55. * Exchange the client access token for one with lower scope
  56. * @param {string|string[]} scopes The scope(s) requested for the new token
  57. * @param {string} [resource] The absolute URL of an API resource to scope the new token to
  58. * @param {Object} [options] - Optional parameters
  59. * @param {TokenRequestOptions} [options.tokenRequestOptions] - Sets optional behavior for the token grant
  60. * @param {ActorParams} [options.actor] - Optional actor parameters for creating annotator tokens
  61. * @returns {Promise<TokenInfo>} Promise resolving to the new token info
  62. */
  63. exchangeToken(
  64. scopes: string | string[],
  65. resource?: string,
  66. options?: {
  67. tokenRequestOptions?: TokenRequestOptions;
  68. actor?: any /* FIXME */;
  69. }
  70. ) {
  71. return this._tokenManager.exchangeToken(
  72. this._accessToken,
  73. scopes,
  74. resource,
  75. options
  76. );
  77. }
  78. }
  79. /**
  80. * @module box-node-sdk/lib/sessions/basic-session
  81. * @see {@Link BasicSession}
  82. */
  83. export = BasicSession;