-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add setRequestToken and fetchRequestToken methods
#900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus' | ||
| import { generateUrl } from '@nextcloud/router' | ||
|
|
||
| export interface CsrfTokenObserver { | ||
| (token: string): void | ||
| } | ||
|
|
||
| _subscribeToTokenUpdates() // TODO: remove once we drop support for Nextcloud 33 and before | ||
|
|
||
| /** | ||
| * Get current request token | ||
| * | ||
| * @return Current request token or null if not set | ||
| */ | ||
| export function getRequestToken(): string | null { | ||
| if (globalThis._nc_auth_requestToken) { | ||
| return globalThis._nc_auth_requestToken | ||
| } | ||
|
|
||
| if (globalThis.document) { | ||
| // for service workers or other contexts without DOM we need to safeguard this | ||
| return document.head.dataset.requesttoken ?? null | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| /** | ||
| * Set a new CSRF token (e.g. because of session refresh). | ||
| * This also emits an event bus event for the updated token. | ||
| * | ||
| * @param token - The new token | ||
| * @throws {Error} - If the passed token is not a potential valid token | ||
| */ | ||
| export function setRequestToken(token: string): void { | ||
| if (!token || typeof token !== 'string') { | ||
| throw new Error('Invalid CSRF token given', { cause: { token } }) | ||
| } | ||
|
|
||
| if (globalThis._nc_auth_requestToken === token) { | ||
| // token is the same as before, no need to update and especially no need to notify the observers | ||
| return | ||
| } | ||
|
|
||
| globalThis._nc_auth_requestToken = token | ||
| if (globalThis.document) { | ||
| // For DOM environments we also set the token to the DOM, so it is available for legacy code | ||
| document.head.dataset.requesttoken = token | ||
| } | ||
|
|
||
| emit('csrf-token-update', { token, _internal: true }) | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the request token from the API. | ||
| * This does also set it on the current context, see `setRequestToken`. | ||
| * | ||
| * @throws {Error} - If the request failed | ||
| */ | ||
| export async function fetchRequestToken(): Promise<string> { | ||
| const url = generateUrl('/csrftoken') | ||
|
|
||
| const response = await fetch(url) | ||
| if (!response.ok) { | ||
| throw new Error('Could not fetch CSRF token from API', { cause: response }) | ||
| } | ||
|
|
||
| try { | ||
| const { token } = await response.json() | ||
| setRequestToken(token) | ||
| return token | ||
| } catch (error) { | ||
| throw new Error('Could not parse CSRF token from API response', { cause: error }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Add an observer which is called when the CSRF token changes | ||
| * | ||
| * @param observer The observer | ||
| * @return A function to unsubscribe the observer | ||
| */ | ||
| export function onRequestTokenUpdate(observer: CsrfTokenObserver): () => void { | ||
| const wrapper = async ({ token }: { token: string }) => { | ||
| try { | ||
| observer(token) | ||
| } catch (error) { | ||
| // we cannot use the logger as the logger uses this library = circular dependency | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error updating CSRF token observer', error) | ||
| } | ||
| } | ||
|
|
||
| subscribe('csrf-token-update', wrapper) | ||
| return () => unsubscribe('csrf-token-update', wrapper) | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe to token update events from server. | ||
| * | ||
| * @todo - This is legacy and not needed once all supported server versions use `setRequestToken` of this library. | ||
| */ | ||
| function _subscribeToTokenUpdates(): void { | ||
| // Listen to server event and keep token in sync | ||
| subscribe('csrf-token-update', ({ token, _internal }) => { | ||
| if (!_internal) { | ||
| // Only update the token if the event is not emitted from this library, otherwise we would end in a loop | ||
| setRequestToken(token) | ||
| } | ||
| }) | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.