diff options
Diffstat (limited to 'core/src/services')
-rw-r--r-- | core/src/services/BrowserStorageService.js | 11 | ||||
-rw-r--r-- | core/src/services/BrowsersListService.js | 13 | ||||
-rw-r--r-- | core/src/services/LegacyUnifiedSearchService.js | 76 | ||||
-rw-r--r-- | core/src/services/UnifiedSearchService.js | 108 | ||||
-rw-r--r-- | core/src/services/WebAuthnAuthenticationService.ts | 42 |
5 files changed, 250 insertions, 0 deletions
diff --git a/core/src/services/BrowserStorageService.js b/core/src/services/BrowserStorageService.js new file mode 100644 index 00000000000..b7d34bf1716 --- /dev/null +++ b/core/src/services/BrowserStorageService.js @@ -0,0 +1,11 @@ +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { getBuilder } from '@nextcloud/browser-storage' + +export default getBuilder('core') + .clearOnLogout() + .persist() + .build() diff --git a/core/src/services/BrowsersListService.js b/core/src/services/BrowsersListService.js new file mode 100644 index 00000000000..77f217a86ac --- /dev/null +++ b/core/src/services/BrowsersListService.js @@ -0,0 +1,13 @@ +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { getUserAgentRegex } from 'browserslist-useragent-regexp' +// eslint-disable-next-line n/no-extraneous-import +import browserslist from 'browserslist' +import browserslistConfig from '@nextcloud/browserslist-config' + +// Generate a regex that matches user agents to detect incompatible browsers +export const supportedBrowsersRegExp = getUserAgentRegex({ allowHigherVersions: true, browsers: browserslistConfig }) +export const supportedBrowsers = browserslist(browserslistConfig) diff --git a/core/src/services/LegacyUnifiedSearchService.js b/core/src/services/LegacyUnifiedSearchService.js new file mode 100644 index 00000000000..5b79c09b8b2 --- /dev/null +++ b/core/src/services/LegacyUnifiedSearchService.js @@ -0,0 +1,76 @@ +/** + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { generateOcsUrl } from '@nextcloud/router' +import { loadState } from '@nextcloud/initial-state' +import axios from '@nextcloud/axios' + +export const defaultLimit = loadState('unified-search', 'limit-default') +export const minSearchLength = loadState('unified-search', 'min-search-length', 1) +export const enableLiveSearch = loadState('unified-search', 'live-search', true) + +export const regexFilterIn = /(^|\s)in:([a-z_-]+)/ig +export const regexFilterNot = /(^|\s)-in:([a-z_-]+)/ig + +/** + * Create a cancel token + * + * @return {import('axios').CancelTokenSource} + */ +const createCancelToken = () => axios.CancelToken.source() + +/** + * Get the list of available search providers + * + * @return {Promise<Array>} + */ +export async function getTypes() { + try { + const { data } = await axios.get(generateOcsUrl('search/providers'), { + params: { + // Sending which location we're currently at + from: window.location.pathname.replace('/index.php', '') + window.location.search, + }, + }) + if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) { + // Providers are sorted by the api based on their order key + return data.ocs.data + } + } catch (error) { + console.error(error) + } + return [] +} + +/** + * Get the list of available search providers + * + * @param {object} options destructuring object + * @param {string} options.type the type to search + * @param {string} options.query the search + * @param {number|string|undefined} options.cursor the offset for paginated searches + * @return {object} {request: Promise, cancel: Promise} + */ +export function search({ type, query, cursor }) { + /** + * Generate an axios cancel token + */ + const cancelToken = createCancelToken() + + const request = async () => axios.get(generateOcsUrl('search/providers/{type}/search', { type }), { + cancelToken: cancelToken.token, + params: { + term: query, + cursor, + // Sending which location we're currently at + from: window.location.pathname.replace('/index.php', '') + window.location.search, + }, + }) + + return { + request, + cancel: cancelToken.cancel, + } +} diff --git a/core/src/services/UnifiedSearchService.js b/core/src/services/UnifiedSearchService.js new file mode 100644 index 00000000000..7067c994c90 --- /dev/null +++ b/core/src/services/UnifiedSearchService.js @@ -0,0 +1,108 @@ +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { generateOcsUrl, generateUrl } from '@nextcloud/router' +import axios from '@nextcloud/axios' +import { getCurrentUser } from '@nextcloud/auth' + +/** + * Create a cancel token + * + * @return {import('axios').CancelTokenSource} + */ +const createCancelToken = () => axios.CancelToken.source() + +/** + * Get the list of available search providers + * + * @return {Promise<Array>} + */ +export async function getProviders() { + try { + const { data } = await axios.get(generateOcsUrl('search/providers'), { + params: { + // Sending which location we're currently at + from: window.location.pathname.replace('/index.php', '') + window.location.search, + }, + }) + if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) { + // Providers are sorted by the api based on their order key + return data.ocs.data + } + } catch (error) { + console.error(error) + } + return [] +} + +/** + * Get the list of available search providers + * + * @param {object} options destructuring object + * @param {string} options.type the type to search + * @param {string} options.query the search + * @param {number|string|undefined} options.cursor the offset for paginated searches + * @param {string} options.since the search + * @param {string} options.until the search + * @param {string} options.limit the search + * @param {string} options.person the search + * @param {object} options.extraQueries additional queries to filter search results + * @return {object} {request: Promise, cancel: Promise} + */ +export function search({ type, query, cursor, since, until, limit, person, extraQueries = {} }) { + /** + * Generate an axios cancel token + */ + const cancelToken = createCancelToken() + + const request = async () => axios.get(generateOcsUrl('search/providers/{type}/search', { type }), { + cancelToken: cancelToken.token, + params: { + term: query, + cursor, + since, + until, + limit, + person, + // Sending which location we're currently at + from: window.location.pathname.replace('/index.php', '') + window.location.search, + ...extraQueries, + }, + }) + + return { + request, + cancel: cancelToken.cancel, + } +} + +/** + * Get the list of active contacts + * + * @param {object} filter filter contacts by string + * @param {string} filter.searchTerm the query + * @return {object} {request: Promise} + */ +export async function getContacts({ searchTerm }) { + const { data: { contacts } } = await axios.post(generateUrl('/contactsmenu/contacts'), { + filter: searchTerm, + }) + /* + * Add authenticated user to list of contacts for search filter + * If authtenicated user is searching/filtering, do not add them to the list + */ + if (!searchTerm) { + let authenticatedUser = getCurrentUser() + authenticatedUser = { + id: authenticatedUser.uid, + fullName: authenticatedUser.displayName, + emailAddresses: [], + } + contacts.unshift(authenticatedUser) + return contacts + } + + return contacts +} diff --git a/core/src/services/WebAuthnAuthenticationService.ts b/core/src/services/WebAuthnAuthenticationService.ts new file mode 100644 index 00000000000..df1837254ad --- /dev/null +++ b/core/src/services/WebAuthnAuthenticationService.ts @@ -0,0 +1,42 @@ +/** + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { AuthenticationResponseJSON, PublicKeyCredentialRequestOptionsJSON } from '@simplewebauthn/browser' + +import { startAuthentication as startWebauthnAuthentication } from '@simplewebauthn/browser' +import { generateUrl } from '@nextcloud/router' + +import Axios from '@nextcloud/axios' +import logger from '../logger' + +export class NoValidCredentials extends Error {} + +/** + * Start webautn authentication + * This loads the challenge, connects to the authenticator and returns the repose that needs to be sent to the server. + * + * @param loginName Name to login + */ +export async function startAuthentication(loginName: string) { + const url = generateUrl('/login/webauthn/start') + + const { data } = await Axios.post<PublicKeyCredentialRequestOptionsJSON>(url, { loginName }) + if (!data.allowCredentials || data.allowCredentials.length === 0) { + logger.error('No valid credentials returned for webauthn') + throw new NoValidCredentials() + } + return await startWebauthnAuthentication({ optionsJSON: data }) +} + +/** + * Verify webauthn authentication + * @param authData The authentication data to sent to the server + */ +export async function finishAuthentication(authData: AuthenticationResponseJSON) { + const url = generateUrl('/login/webauthn/finish') + + const { data } = await Axios.post(url, { data: JSON.stringify(authData) }) + return data +} |