Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

client.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. import { createClient } from 'webdav'
  22. import memoize from 'lodash/fp/memoize.js'
  23. import { generateRemoteUrl } from '@nextcloud/router'
  24. import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
  25. export const getClient = memoize((service) => {
  26. // init webdav client
  27. const remote = generateRemoteUrl(`dav/${service}/${getCurrentUser().uid}`)
  28. const client = createClient(remote)
  29. // set CSRF token header
  30. const setHeaders = (token) => {
  31. client.setHeaders({
  32. // Add this so the server knows it is an request from the browser
  33. 'X-Requested-With': 'XMLHttpRequest',
  34. // Inject user auth
  35. requesttoken: token ?? '',
  36. })
  37. }
  38. // refresh headers when request token changes
  39. onRequestTokenUpdate(setHeaders)
  40. setHeaders(getRequestToken())
  41. return client;
  42. })