You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.web.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // @flow
  2. import { Dropbox, DropboxAuth } from 'dropbox';
  3. /**
  4. * Executes the oauth flow.
  5. *
  6. * @param {string} authUrl - The URL to oauth service.
  7. * @returns {Promise<string>} - The URL with the authorization details.
  8. */
  9. function authorize(authUrl: string): Promise<string> {
  10. const windowName = `oauth${Date.now()}`;
  11. return new Promise(resolve => {
  12. // eslint-disable-next-line prefer-const
  13. let popup;
  14. const handleAuth = ({ data }) => {
  15. if (data && data.type === 'dropbox-login' && data.windowName === windowName) {
  16. if (popup) {
  17. popup.close();
  18. }
  19. window.removeEventListener('message', handleAuth);
  20. resolve(data.url);
  21. }
  22. };
  23. window.addEventListener('message', handleAuth);
  24. popup = window.open(authUrl, windowName);
  25. });
  26. }
  27. /**
  28. * Returns the token's expiry date as UNIX timestamp.
  29. *
  30. * @param {number} expiresIn - The seconds in which the token expires.
  31. * @returns {number} - The timestamp value for the expiry date.
  32. */
  33. function getTokenExpiresAtTimestamp(expiresIn: number) {
  34. return new Date(Date.now() + (expiresIn * 1000)).getTime();
  35. }
  36. /**
  37. * Action to authorize the Jitsi Recording app in dropbox.
  38. *
  39. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  40. * @param {string} redirectURI - The return URL.
  41. * @returns {Promise<Object>}
  42. */
  43. export function _authorizeDropbox(
  44. appKey: string,
  45. redirectURI: string
  46. ): Promise<Object> {
  47. const dropbox = new DropboxAuth({ clientId: appKey });
  48. return dropbox.getAuthenticationUrl(redirectURI, undefined, 'code', 'offline', undefined, undefined, true)
  49. .then(authorize)
  50. .then(returnUrl => {
  51. const params = new URLSearchParams(new URL(returnUrl).search);
  52. const code = params.get('code');
  53. return dropbox.getAccessTokenFromCode(redirectURI, code);
  54. })
  55. .then(resp => {
  56. return {
  57. token: resp.result.access_token,
  58. rToken: resp.result.refresh_token,
  59. expireDate: getTokenExpiresAtTimestamp(resp.result.expires_in)
  60. };
  61. });
  62. }
  63. /**
  64. * Gets a new acccess token based on the refresh token.
  65. *
  66. * @param {string} appKey - The dropbox appKey.
  67. * @param {string} rToken - The refresh token.
  68. * @returns {Promise}
  69. */
  70. export function getNewAccessToken(appKey: string, rToken: string) {
  71. const dropbox = new DropboxAuth({ clientId: appKey });
  72. dropbox.setRefreshToken(rToken);
  73. return dropbox.refreshAccessToken()
  74. .then(() => {
  75. return {
  76. token: dropbox.getAccessToken(),
  77. rToken: dropbox.getRefreshToken(),
  78. expireDate: dropbox.getAccessTokenExpiresAt().getTime()
  79. };
  80. });
  81. }
  82. /**
  83. * Returns the display name for the current dropbox account.
  84. *
  85. * @param {string} token - The dropbox access token.
  86. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  87. * @returns {Promise<string>}
  88. */
  89. export function getDisplayName(token: string, appKey: string) {
  90. const dropboxAPI = new Dropbox({
  91. accessToken: token,
  92. clientId: appKey
  93. });
  94. return (
  95. dropboxAPI.usersGetCurrentAccount()
  96. .then(account => account.result.name.display_name));
  97. }
  98. /**
  99. * Returns information about the space usage for the current dropbox account.
  100. *
  101. * @param {string} token - The dropbox access token.
  102. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  103. * @returns {Promise<Object>}
  104. */
  105. export function getSpaceUsage(token: string, appKey: string) {
  106. const dropboxAPI = new Dropbox({
  107. accessToken: token,
  108. clientId: appKey
  109. });
  110. return dropboxAPI.usersGetSpaceUsage().then(space => {
  111. const { allocation, used } = space.result;
  112. const { allocated } = allocation;
  113. return {
  114. allocated,
  115. used
  116. };
  117. });
  118. }
  119. /**
  120. * Returns <tt>true</tt> if the dropbox features is enabled and <tt>false</tt>
  121. * otherwise.
  122. *
  123. * @param {Object} state - The redux state.
  124. * @returns {boolean}
  125. */
  126. export function isEnabled(state: Object) {
  127. const { dropbox = {} } = state['features/base/config'];
  128. return typeof dropbox.appKey === 'string';
  129. }