diff --git a/src/components/map-projects/LookupConfig.jsx b/src/components/map-projects/LookupConfig.jsx index 54d90fa..76ce8c3 100644 --- a/src/components/map-projects/LookupConfig.jsx +++ b/src/components/map-projects/LookupConfig.jsx @@ -11,12 +11,31 @@ import DownIcon from '@mui/icons-material/ArrowDropDown'; import VisibilityIcon from '@mui/icons-material/Visibility'; import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'; import { lookupTokenHasSpaces, normalizeLookupConfig } from './lookupConfigFields' +import { isOCLAPILookupURL, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope' +import { getAPIURL } from '../../common/utils' const LookupConfig = ({value, onChange}) => { const { t } = useTranslation() const [open, setOpen] = React.useState(false) const [showToken, setShowToken] = React.useState(false) const tokenHasSpaces = lookupTokenHasSpaces(value?.token) + const isOCLAPI = isOCLAPILookupURL(value?.url, getAPIURL()) + const getTokenHelperText = () => { + if(tokenHasSpaces) + return t('map_project.lookup_configuration_token_has_spaces') + if(isOCLAPI) + return t('map_project.lookup_configuration_token_ocl') + if(!value?.has_token || value?.token) + return '' + return ( + <> + {t('map_project.lookup_configuration_token_saved')} + + + ) + } return (
@@ -34,7 +53,10 @@ const LookupConfig = ({value, onChange}) => { fullWidth required value={value?.url || ''} - onChange={event => onChange({...value, url: event.target.value.trim()})} + onChange={event => { + const url = event.target.value.trim() + onChange({...value, url, has_token: getTokenFlag(value, url)}) + }} />
@@ -43,10 +65,10 @@ const LookupConfig = ({value, onChange}) => { label={t('map_project.lookup_configuration_token')} fullWidth value={value?.token || ''} - onChange={event => onChange({...value, token: event.target.value || ''})} + onChange={event => onChange({...value, token: tokenInputValue(value, event.target.value)})} onBlur={() => onChange(normalizeLookupConfig(value))} color={tokenHasSpaces ? 'warning' : 'primary'} - helperText={tokenHasSpaces ? t('map_project.lookup_configuration_token_has_spaces') : ''} + helperText={getTokenHelperText()} slotProps={{ input: { endAdornment: ( @@ -57,7 +79,7 @@ const LookupConfig = ({value, onChange}) => { ), }, - formHelperText: {sx: {color: 'warning.main'}}, + formHelperText: tokenHasSpaces ? {sx: {color: 'warning.main'}} : {}, }} />
diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 53f18f4..23e8065 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -70,7 +70,7 @@ import pick from 'lodash/pick' import { OperationsContext } from '../app/LayoutContext'; import APIService, { isTransientNetworkError, retryWithBackoff } from '../../services/APIService'; -import { matchToken, lookupToken } from '../../services/tokenScope' +import { matchToken, lookupTokenFor, needsSessionToken, needsSessionLookupToken, withoutTokens, withSavedTokenURL, withoutLocalKeys, isAbsoluteURL } from '../../services/tokenScope' import { buildAttributionHeaders, buildConfigSnapshot, summarizeRunCompletion, matchesOnOCL } from '../../services/attribution' import { CAPACITY_LIMIT, RATE_LIMIT, CAPACITY_WAIT_CAP_MS, HEAVY_REQUEST_TIMEOUT_MS, LIGHT_REQUEST_TIMEOUT_MS, createCapacityGate, createLimiter, isRetryableError, requestWithCapacityRetry, sleepUnlessCancelled, untilCancelled } from '../../services/capacity' import { getAPIURL, highlightTexts, dropVersion, getCurrentUser, hasAuthGroup, hasCapability, getMapperPreview, getNewProjectBlockReason, downloadObject, currentUserToken, refreshCurrentUserCapabilitiesCache } from '../../common/utils'; @@ -326,6 +326,7 @@ const MapProject = () => { const getConfigurationSnapshotRef = React.useRef(null) const projectIdRef = React.useRef(null) const onSaveRef = React.useRef(null) + const lookupTokenWarnedRef = React.useRef(false) const projectLogRef = React.useRef(() => {}) // A save can complete after the user has left the project // (OpenConceptLab/ocl_issues#2829): these track the request still in flight @@ -883,11 +884,12 @@ const MapProject = () => { setProjectPromptTemplateKey(copiedProject.prompt_template_key || '') setName(copiedProject.name ? t('map_project.create_similar_name', {name: copiedProject.name}) : '') setFilters(copiedProject.filters || {}) - setLookupConfig(normalizeLookupConfig(copiedProject.lookup_config || {})) + // A copy starts without the original's saved tokens. + setLookupConfig(normalizeLookupConfig(withoutTokens(copiedProject.lookup_config || {}))) setNamespace(copiedProject.namespace || '') setCandidatesScore(copiedProject.score_configuration || {recommended: 99, available: 70}) setRetired(Boolean(copiedProject.include_retired || false)) - setAlgosSelected(copiedProject.algorithms || []) + setAlgosSelected(withoutTokens(copiedProject.algorithms || [])) setEncoderModel(copiedProject.encoder_model || DEFAULT_ENCODER_MODEL) setUseLexicalVariants(Boolean(copiedProject.use_lexical_variants)) setInputLocale((copiedProject.input_locales || [])[0] || '') @@ -933,7 +935,7 @@ const MapProject = () => { const sheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(sheet, { raw: false, defval: '' }) setProjectFromData(data, response.data) - setAlgosSelected(response?.data?.algorithms || []) + setAlgosSelected(map(response?.data?.algorithms || [], withSavedTokenURL)) if (response?.data.columns?.length > 0) { // Saved mappings are the user's, so they are never re-suggested. const _columns = response.data.columns.map(col => ({...omit(col, ['hidden', 'auto', 'suggested'])})) @@ -1014,7 +1016,7 @@ const MapProject = () => { setRowMatchState(_rowMatchState) conceptCacheRef.current = _cache rowMatchStateRef.current = _rowMatchState - setAlgosSelected(response.data.algorithms) + setAlgosSelected(map(response.data.algorithms, withSavedTokenURL)) setRowStage(_rowStage) setName(response.data?.name || '') @@ -1022,7 +1024,8 @@ const MapProject = () => { setOwner(response.data?.owner_url) setRetired(Boolean(response.data?.include_retired)) setCandidatesScore(response.data?.score_configuration) - setLookupConfig(normalizeLookupConfig(response.data?.lookup_config)) + setLookupConfig(normalizeLookupConfig(withSavedTokenURL(response.data?.lookup_config))) + lookupTokenWarnedRef.current = false setNamespace(response.data?.namespace || '') setEncoderModel(response.data?.encoder_model || DEFAULT_ENCODER_MODEL) setProjectPromptTemplateKey(response.data?.prompt_template_key || '') @@ -1753,9 +1756,9 @@ const MapProject = () => { source: repo.short_code || repo.id, source_version: selectedTargetRepoVersion })) - formData.append('algorithms', JSON.stringify(map(algosSelected, algo => omit(algo, ['__key'])))) + formData.append('algorithms', JSON.stringify(map(algosSelected, withoutLocalKeys))) formData.append('score_configuration', JSON.stringify(candidatesScore)) - formData.append('lookup_config', JSON.stringify(lookupConfig)) + formData.append('lookup_config', JSON.stringify(withoutLocalKeys(lookupConfig))) // Always send `namespace` (including empty string) so clearing the field // in the UI actually propagates to the server. The server reads empty // as "use the project owner default"; gating the append on truthiness @@ -1800,7 +1803,7 @@ const MapProject = () => { description: isAutoSave ? t('map_project.auto_saved_changes') : undefined, created_at: moment().toDate(), user: user.username || user.id, - extras: isAutoSave ? {reasons: options.reasons || []} : (isUpdate ? undefined : {project: response.data}) + extras: isAutoSave ? {reasons: options.reasons || []} : (isUpdate ? undefined : {project: withoutTokens(response.data)}) } // Entries logged while the save was in flight are kept. A save that // lands after the user opened another project uses the logs as they @@ -3186,8 +3189,13 @@ const MapProject = () => { event.stopPropagation() event.preventDefault() setAlert(false) + const algoWithoutKey = find(algosSelected, algo => selectedAlgos.includes(algo.id) && needsSessionToken(algo)) const run = hasAutoMatchWork(selectedAlgos) ? getAutoMatchRunRows(data, selectedAlgos) : null - if(!run?.runRows.length) { + if(algoWithoutKey) { + setAlert({message: t('map_project.api_token_needed', {name: algoWithoutKey.name || algoWithoutKey.id}), severity: 'warning'}) + } else if(needsSessionLookupToken(lookupConfig, getAPIURL())) { + setAlert({message: t('map_project.lookup_token_needed'), severity: 'warning'}) + } else if(!run?.runRows.length) { setAlert({message: t('map_project.auto_match_nothing_to_run'), severity: 'warning'}) } else if(isAnyValidColumn()){ setStartMatchingAt(moment()) @@ -4321,7 +4329,12 @@ const MapProject = () => { } } - if(['ocl-semantic', 'ocl-search', 'custom'].includes(algoDef.type)) { + if(needsSessionToken(algoDef)) { + // Fails this algorithm for the row and goes on with the others. + setAlert({message: t('map_project.api_token_needed', {name: algoDef.name || algoDef.id}), severity: 'warning'}) + markAlgo(__row.__index, algoId, -2) + goOnAfterFailure() + } else if(['ocl-semantic', 'ocl-search', 'custom'].includes(algoDef.type)) { fetchOCLOrCustomCandidates(algoDef, _row, offset, _retired, _filters, onResponse) } else if (algoDef.type === 'ocl-scispacy') { fetchScispacyCandidates(__row, scrollToBottom, forceReload, false, onResponse, goOnAfterFailure) @@ -5121,7 +5134,7 @@ const MapProject = () => { const getLookupService = () => { let service = APIService.new() if(lookupConfig?.url) { - if(lookupConfig.url.startsWith('http')) + if(isAbsoluteURL(lookupConfig.url)) service.URL = lookupConfig.url else service.overrideURL(lookupConfig.url) @@ -5131,10 +5144,15 @@ const MapProject = () => { return service.appendToUrl('concepts/') } - // The token a lookup request to this URL gets (see lookupToken). - const getLookupToken = url => lookupToken(lookupConfig?.token, url, getAPIURL(), currentUserToken()) + // The token a lookup request to this URL gets (see lookupTokenFor). + const getLookupToken = url => lookupTokenFor(lookupConfig, url, getAPIURL(), currentUserToken()) const requestLookup = query => { + // Lookups still go out without the saved token, so say once why they may fail. + if(!lookupTokenWarnedRef.current && needsSessionLookupToken(lookupConfig, getAPIURL())) { + lookupTokenWarnedRef.current = true + setAlert({message: t('map_project.lookup_token_needed'), severity: 'warning'}) + } const service = getLookupService() return service.request('GET', undefined, getLookupToken(service.URL), {query, handlesThrottle: true, timeout: LIGHT_REQUEST_TIMEOUT_MS}) } @@ -5267,7 +5285,7 @@ const MapProject = () => { const dataPromise = session.fetchOnce(oclUrl, () => { const attemptFetch = async () => { let service = APIService.new() - if(oclUrl.startsWith('http://') || oclUrl.startsWith('https://')) { + if(isAbsoluteURL(oclUrl)) { service.URL = oclUrl } else { service = service.overrideURL(oclUrl) diff --git a/src/components/map-projects/MultiAlgoSelector.jsx b/src/components/map-projects/MultiAlgoSelector.jsx index d5d7e43..2c7abf5 100644 --- a/src/components/map-projects/MultiAlgoSelector.jsx +++ b/src/components/map-projects/MultiAlgoSelector.jsx @@ -37,6 +37,7 @@ import { OperationsContext } from '../app/LayoutContext' import { isLikelyCanonicalUrl } from './algorithms' import RepoVersionSearchAutocomplete from '../repos/RepoVersionSearchAutocomplete' import APIService from '../../services/APIService'; +import { needsSessionToken, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope'; import { getRequestLimits, getRequestSettings } from './requestLimits' import { dropVersion } from '../../common/utils' import { @@ -509,7 +510,8 @@ export default function MultiAlgoSelector({ label={t('map_project.api_url')} value={sel.url || ""} onChange={(e) => { - updateSelected(sel.__key, { url: e.target.value }); + // A saved key belongs to the URL it was saved with. + updateSelected(sel.__key, { url: e.target.value, has_token: getTokenFlag(sel, e.target.value) }); if(customMeta.requestError || customMeta.requestSuccess || customMeta.tokenRequired) { setCustomAlgoState(sel.__key, { requestError: '', @@ -518,19 +520,23 @@ export default function MultiAlgoSelector({ }); } }} - onBlur={() => verifyCustomAlgorithm(sel.__key)} + onBlur={() => { + // Without its saved key this session, a check would only get a 401. + if(!needsSessionToken(sel)) + verifyCustomAlgorithm(sel.__key) + }} placeholder="https://example.com/match" error={Boolean(customMeta.requestError)} helperText={customMeta.requestError || customMeta.requestSuccess || ''} /> { - updateSelected(sel.__key, { token: e.target.value }); + updateSelected(sel.__key, { token: tokenInputValue(sel, e.target.value) }); if(customMeta.requestError || customMeta.requestSuccess) { setCustomAlgoState(sel.__key, { requestError: '', @@ -545,7 +551,18 @@ export default function MultiAlgoSelector({ }} placeholder="••••••••" error={Boolean(customMeta.tokenRequired && !sel.token)} - helperText={customMeta.tokenRequired && !sel.token ? 'Authentication is required for this algorithm.' : ''} + helperText={ + (customMeta.tokenRequired && !sel.token) || needsSessionToken(sel) ? ( + <> + {customMeta.tokenRequired && !sel.token ? 'Authentication is required for this algorithm.' : t('map_project.api_token_saved')} + {needsSessionToken(sel) && ( + + )} + + ) : '' + } /> { assert.equal(lookupTokenHasSpaces(''), false) assert.equal(lookupTokenHasSpaces(undefined), false) }) + +test('normalizeLookupConfig: a blank token keeps a saved one, and clears none otherwise', () => { + const saved = normalizeLookupConfig({url: 'https://x.org/', has_token: true, __token_url: 'https://x.org/', token: ' '}) + assert.equal(saved.token, undefined) + assert.equal(JSON.stringify(saved).includes('"token"'), false) + assert.equal(normalizeLookupConfig({url: 'https://x.org/', token: ' '}).token, '') +}) diff --git a/src/components/map-projects/lookupConfigFields.js b/src/components/map-projects/lookupConfigFields.js index 379b7c0..f238f89 100644 --- a/src/components/map-projects/lookupConfigFields.js +++ b/src/components/map-projects/lookupConfigFields.js @@ -1,3 +1,5 @@ +import { TOKEN_URL_KEY } from '../../services/tokenScope.js' + // Stray whitespace in the lookup URL or token breaks every lookup request: // a padded URL becomes `.../%20concepts/` and a token with a space is // rejected by the API (OpenConceptLab/ocl_issues#2925). @@ -5,7 +7,11 @@ // left for the user to fix. const trim = value => typeof value === 'string' ? value.trim() : value -export const normalizeLookupConfig = config => config ? {...config, url: trim(config.url), token: trim(config.token)} : config +// A token that trims to nothing keeps a saved one (the key is left out), as an +// emptied field does. +export const normalizeLookupConfig = config => config ? { + ...config, url: trim(config.url), token: trim(config.token) || (config[TOKEN_URL_KEY] ? undefined : trim(config.token)), +} : config // A token never contains whitespace; one that does was most likely pasted // with a `Token `/`Bearer ` prefix. diff --git a/src/i18n/locales/en/translations.json b/src/i18n/locales/en/translations.json index bf6b226..0f5e377 100644 --- a/src/i18n/locales/en/translations.json +++ b/src/i18n/locales/en/translations.json @@ -260,6 +260,9 @@ "request_limit_up_to": "Up to {{max}}", "api_url": "API URL", "api_token": "API Token", + "api_token_saved": "A token is saved for this endpoint but isn't shown. Enter it again to use this algorithm in this session.", + "api_token_needed": "Enter the API token for {{name}} in the project configuration. A saved token isn't shown, so it's needed again each session.", + "remove_saved_token": "Remove saved token", "score_configuration": "Score Configuration", "score_configuration_description": "Configure Score threshold to group candidates recommendations", "not_recommended": "Not Recommended", @@ -495,6 +498,9 @@ "lookup_configuration": "Lookup Configuration", "lookup_configuration_url": "Repository URL", "lookup_configuration_token": "Token", + "lookup_configuration_token_saved": "A token is saved for this lookup but isn't shown. Enter it again to use it in this session.", + "lookup_token_needed": "Enter the lookup token in the project configuration. A saved token isn't shown, so it's needed again each session; until then, lookups go without it.", + "lookup_configuration_token_ocl": "Lookups on OCL use your own sign-in. A token entered here is used in this session only and isn't saved.", "lookup_configuration_token_has_spaces": "Tokens don't contain spaces. Paste only the token itself, without a \"Token\" or \"Bearer\" prefix.", "lookup_configuration_description": "Configure a Source/CodeSystem for lookup operation to fetch candidates definitions.", "lookup_concept_404": "LookUp Concept:{{code}} failed with 404 (attempt {{attempt}})", diff --git a/src/i18n/locales/es/translations.json b/src/i18n/locales/es/translations.json index e69a4e6..f0dc284 100644 --- a/src/i18n/locales/es/translations.json +++ b/src/i18n/locales/es/translations.json @@ -479,6 +479,9 @@ "request_limit_up_to": "Hasta {{max}}", "api_url": "URL de la API", "api_token": "Token de API", + "api_token_saved": "Hay un token guardado para este endpoint, pero no se muestra. Ingréselo de nuevo para usar este algoritmo en esta sesión.", + "api_token_needed": "Ingrese el token de API de {{name}} en la configuración del proyecto. Un token guardado no se muestra, así que se necesita de nuevo en cada sesión.", + "remove_saved_token": "Eliminar token guardado", "ocl_search_algorithm": "Algoritmo de búsqueda de OCL", "scispacy_warming_up": "El servicio de coincidencia scispacy de OCL se está iniciando. Reintentará automáticamente en 2 minutos.", "algorithms": "Algoritmos", @@ -487,6 +490,9 @@ "lookup_configuration": "Configuración de búsqueda", "lookup_configuration_url": "URL del repositorio", "lookup_configuration_token": "Token", + "lookup_configuration_token_saved": "Hay un token guardado para esta búsqueda, pero no se muestra. Ingréselo de nuevo para usarlo en esta sesión.", + "lookup_token_needed": "Ingrese el token de búsqueda en la configuración del proyecto. Un token guardado no se muestra, así que se necesita de nuevo en cada sesión; mientras tanto, las búsquedas se hacen sin él.", + "lookup_configuration_token_ocl": "Las búsquedas en OCL usan su propia sesión. Un token ingresado aquí solo se usa en esta sesión y no se guarda.", "lookup_configuration_token_has_spaces": "Los tokens no contienen espacios. Pegue solo el token, sin el prefijo \"Token\" o \"Bearer\".", "lookup_configuration_description": "Configure una Fuente/Sistema de códigos para la operación de búsqueda y recuperar definiciones de candidatos.", "lookup_concept_404": "La búsqueda del Concepto:{{code}} falló con 404 (intento {{attempt}})", diff --git a/src/i18n/locales/pt-BR/translations.json b/src/i18n/locales/pt-BR/translations.json index 947521b..c1d2448 100644 --- a/src/i18n/locales/pt-BR/translations.json +++ b/src/i18n/locales/pt-BR/translations.json @@ -260,6 +260,9 @@ "request_limit_up_to": "Até {{max}}", "api_url": "URL da API", "api_token": "Token da API", + "api_token_saved": "Há um token salvo para este endpoint, mas ele não é exibido. Digite-o novamente para usar este algoritmo nesta sessão.", + "api_token_needed": "Digite o token de API de {{name}} na configuração do projeto. Um token salvo não é exibido, então é necessário novamente a cada sessão.", + "remove_saved_token": "Remover token salvo", "score_configuration": "Configuração de Pontuação", "score_configuration_description": "Configurar limite de pontuação para agrupar recomendações de candidatos", "not_recommended": "Não Recomendado", @@ -495,6 +498,9 @@ "lookup_configuration": "Configuração de Busca", "lookup_configuration_url": "URL do Repositório", "lookup_configuration_token": "Token", + "lookup_configuration_token_saved": "Há um token salvo para esta consulta, mas ele não é exibido. Digite-o novamente para usá-lo nesta sessão.", + "lookup_token_needed": "Digite o token de consulta na configuração do projeto. Um token salvo não é exibido, então é necessário novamente a cada sessão; até lá, as consultas são feitas sem ele.", + "lookup_configuration_token_ocl": "Consultas no OCL usam o seu próprio login. Um token digitado aqui é usado somente nesta sessão e não é salvo.", "lookup_configuration_token_has_spaces": "Tokens não contêm espaços. Cole apenas o token, sem o prefixo \"Token\" ou \"Bearer\".", "lookup_configuration_description": "Configure um Source/CodeSystem para operação de busca para buscar definições de candidatos.", "lookup_concept_404": "Busca de Conceito:{{code}} falhou com 404 (tentativa {{attempt}})", diff --git a/src/i18n/locales/zh/translations.json b/src/i18n/locales/zh/translations.json index 9d1b42c..54e1869 100644 --- a/src/i18n/locales/zh/translations.json +++ b/src/i18n/locales/zh/translations.json @@ -490,6 +490,9 @@ "request_limit_up_to": "最多 {{max}}", "api_url": "API URL", "api_token": "API 令牌", + "api_token_saved": "此端点已保存令牌,但不会显示。请再次输入,以便在本次会话中使用此算法。", + "api_token_needed": "请在项目配置中输入 {{name}} 的 API 令牌。已保存的令牌不会显示,因此每次会话都需要重新输入。", + "remove_saved_token": "删除已保存的令牌", "ocl_search_algorithm": "OCL 搜索算法", "scispacy_warming_up": "OCL的scispacy匹配服务正在启动中,将在2分钟后自动重试。", "algorithms": "算法", @@ -498,6 +501,9 @@ "lookup_configuration": "查找配置", "lookup_configuration_url": "仓库 URL", "lookup_configuration_token": "令牌", + "lookup_configuration_token_saved": "此查找已保存令牌,但不会显示。请再次输入,以便在本次会话中使用。", + "lookup_token_needed": "请在项目配置中输入查找令牌。已保存的令牌不会显示,因此每次会话都需要重新输入;在此之前,查找将不带令牌进行。", + "lookup_configuration_token_ocl": "在 OCL 上的查找使用您自己的登录。在此输入的令牌仅在本次会话中使用,不会保存。", "lookup_configuration_token_has_spaces": "令牌不包含空格。请只粘贴令牌本身,不要带 \"Token\" 或 \"Bearer\" 前缀。", "lookup_configuration_description": "配置用于查找操作的源/代码系统,以获取候选项定义。", "lookup_concept_404": "查找概念:{{code}} 失败,返回 404(第 {{attempt}} 次尝试)", diff --git a/src/services/__tests__/attribution.test.js b/src/services/__tests__/attribution.test.js index a7dc905..756159f 100644 --- a/src/services/__tests__/attribution.test.js +++ b/src/services/__tests__/attribution.test.js @@ -298,3 +298,11 @@ test('matchesOnOCL: built-in algorithms and a custom one without a URL go to OCL assert.equal(matchesOnOCL({ id: 'c', type: 'custom', url: 'https://example.org/$match/' }), false) assert.equal(matchesOnOCL(undefined), true) }) + +test('buildConfigSnapshot leaves out every token', () => { + const snapshot = buildConfigSnapshot({ + selectedAlgos: [{id: 'custom-1', type: 'custom', url: 'https://match.example.com/', token: 'T', has_token: true}], + }) + assert.deepEqual(snapshot.algorithms, [{id: 'custom-1', type: 'custom', url: 'https://match.example.com/'}]) + assert.equal(JSON.stringify(snapshot).includes('"token"'), false) +}) diff --git a/src/services/__tests__/tokenScope.test.js b/src/services/__tests__/tokenScope.test.js index fc96c21..c5d5a97 100644 --- a/src/services/__tests__/tokenScope.test.js +++ b/src/services/__tests__/tokenScope.test.js @@ -6,7 +6,11 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { isAPIHost, matchToken, lookupToken, joinAPIPath } from '../tokenScope.js' +import { + isAPIHost, matchToken, lookupToken, joinAPIPath, isOCLAPILookupURL, needsSessionToken, withoutTokens, + isAbsoluteURL, needsSessionLookupToken, withSavedTokenURL, getTokenFlag, tokenInputValue, removeSavedToken, + withoutLocalKeys, TOKEN_URL_KEY, lookupTokenFor, +} from '../tokenScope.js' const API = 'https://api.example.org' const USER = 'USER-TOKEN' @@ -63,3 +67,82 @@ test('overrideURL cannot change the host', () => { assert.equal(new URL(joinAPIPath(API, ':80@evil.com/')).host, 'api.example.org') assert.equal(joinAPIPath(API, '/orgs/x/'), `${API}/orgs/x/`) }) + +test('isOCLAPILookupURL: no URL, a path, or the API origin', () => { + assert.equal(isOCLAPILookupURL(undefined, API), true) + assert.equal(isOCLAPILookupURL('', API), true) + assert.equal(isOCLAPILookupURL('/orgs/CIEL/sources/CIEL/', API), true) + assert.equal(isOCLAPILookupURL(`${API}/orgs/CIEL/`, API), true) + assert.equal(isOCLAPILookupURL('https://terms.example.com/orgs/X/', API), false) + assert.equal(isOCLAPILookupURL('HTTPS://terms.example.com/orgs/X/', API), false) + assert.equal(isOCLAPILookupURL('//terms.example.com/orgs/X/', API), true) +}) + +test('isAbsoluteURL: only http(s)://, in any case', () => { + assert.equal(isAbsoluteURL('HTTPS://x.org/'), true) + assert.equal(isAbsoluteURL('http://x.org/'), true) + assert.equal(isAbsoluteURL('//x.org/'), false) + assert.equal(isAbsoluteURL('/orgs/X/'), false) + assert.equal(isAbsoluteURL(undefined), false) +}) + +test('needsSessionLookupToken: a saved token on another host, not entered this session', () => { + const url = 'https://terms.example.com/orgs/X/' + assert.equal(needsSessionLookupToken({url, has_token: true}, API), true) + assert.equal(needsSessionLookupToken({url, has_token: true, token: 'T'}, API), false) + assert.equal(needsSessionLookupToken({url: '/orgs/X/', has_token: true}, API), false) + assert.equal(needsSessionLookupToken({url}, API), false) +}) + +test('saved token URL: has_token comes back on the original URL, and is never sent', () => { + const url = 'https://match.example.com/' + const loaded = withSavedTokenURL({__key: 'k', url, has_token: true}) + assert.equal(getTokenFlag(loaded, 'https://other.example.com/'), undefined) + assert.equal(getTokenFlag(loaded, url), true) + assert.equal(getTokenFlag({url}, url), undefined) + assert.deepEqual(withoutLocalKeys(loaded), {url, has_token: true}) + assert.deepEqual(withSavedTokenURL({url}), {url}) + assert.equal(withoutTokens(loaded)[TOKEN_URL_KEY], undefined) +}) + +test('an emptied token field keeps a saved token; removing it clears it', () => { + const loaded = withSavedTokenURL({url: 'u', has_token: true}) + assert.equal(tokenInputValue(loaded, ''), undefined) + assert.equal(tokenInputValue(loaded, 'T'), 'T') + assert.equal(tokenInputValue({url: 'u'}, ''), '') + assert.equal(tokenInputValue(loaded, ' '), undefined) + const removed = removeSavedToken(loaded) + assert.equal(removed.token, '') + assert.equal(removed.has_token, undefined) + assert.equal(tokenInputValue(removed, ''), '') + assert.equal(JSON.parse(JSON.stringify(withoutLocalKeys(removed))).token, '') +}) + +test('needsSessionToken: a custom algorithm with a saved key not entered this session', () => { + const url = 'https://match.example.com/' + assert.equal(needsSessionToken({type: 'custom', url, has_token: true}), true) + assert.equal(needsSessionToken({type: 'custom', url, has_token: true, token: 'T'}), false) + assert.equal(needsSessionToken({type: 'custom', url}), false) + assert.equal(needsSessionToken({type: 'custom', has_token: true}), false) + assert.equal(needsSessionToken({type: 'ocl-semantic', url, has_token: true}), false) + assert.equal(needsSessionToken(undefined), false) +}) + +test('withoutTokens: removes token and has_token at any depth', () => { + assert.deepEqual( + withoutTokens([{url: 'u', token: 'T', has_token: true, nested: {token: 'N', keep: 1}}, 'x']), + [{url: 'u', nested: {keep: 1}}, 'x'] + ) + assert.deepEqual(withoutTokens({url: 'u', has_token: true}), {url: 'u'}) + assert.equal(withoutTokens(null), null) +}) + +test('lookupTokenFor: the configured token goes only to the lookup origin', () => { + const external = {url: 'https://terms.example.com/orgs/X/', token: 'LOOKUP'} + assert.equal(lookupTokenFor(external, 'https://terms.example.com/orgs/X/concepts/', API, USER), 'LOOKUP') + assert.equal(lookupTokenFor(external, `${API}/orgs/CIEL/concepts/1/`, API, USER), USER) + assert.equal(lookupTokenFor(external, 'https://evil.example/x/', API, USER), false) + const onOCL = {url: '/orgs/X/sources/Y/', token: 'OWN'} + assert.equal(lookupTokenFor(onOCL, `${API}/orgs/X/sources/Y/concepts/`, API, USER), 'OWN') + assert.equal(lookupTokenFor({}, `${API}/orgs/X/`, API, USER), USER) +}) diff --git a/src/services/attribution.js b/src/services/attribution.js index 336ef41..25a9a77 100644 --- a/src/services/attribution.js +++ b/src/services/attribution.js @@ -19,6 +19,7 @@ */ import isFunction from 'lodash/isFunction.js' +import { withoutTokens } from './tokenScope.js' export const REQUEST_SOURCE = { AUTOMATCH: 'automatch', @@ -100,7 +101,7 @@ export const buildAttributionHeaders = ({ /** * Assemble the JSON-safe project config captured at run start * (AutomatchRun.config_snapshot). Strips function-valued algorithm fields - * (e.g. getIcon) so the POST body serializes cleanly. + * (e.g. getIcon) so the POST body serializes cleanly, and every token. * * @param {object} [opts] * @param {object[]} [opts.selectedAlgos] algorithms selected for the run @@ -124,7 +125,7 @@ export const buildConfigSnapshot = ({ Object.keys(algo || {}).forEach(key => { if(!isFunction(algo[key])) clean[key] = algo[key] }) - return clean + return withoutTokens(clean) }), encoder: encoderModel || null, score_config: scoreConfig || null, diff --git a/src/services/tokenScope.js b/src/services/tokenScope.js index a75e23a..b37de0f 100644 --- a/src/services/tokenScope.js +++ b/src/services/tokenScope.js @@ -27,8 +27,69 @@ export const matchToken = algo => export const lookupToken = (configToken, url, apiURL, userToken) => configToken || (isAPIHost(url, apiURL) ? userToken : false) +// The configured lookup token goes only to the lookup's own origin: its URL's, or +// the API's for a path. Elsewhere, such as an OCL concept's own URL, lookupToken +// decides without it. +export const lookupTokenFor = (config, url, apiURL, userToken) => + lookupToken(isAPIHost(url, isAbsoluteURL(config?.url) ? config.url : apiURL) ? config?.token : undefined, url, apiURL, userToken) + // Without a leading '/', a value like '@host/x' would change the request's host. export const joinAPIPath = (apiURL, path) => { const value = String(path ?? '') return `${apiURL}${value.startsWith('/') ? '' : '/'}${value}` } + +// Only an http(s):// URL, in any case, is absolute; anything else is a path on the OCL API. +export const isAbsoluteURL = url => /^https?:\/\//i.test(String(url ?? '')) + +// A lookup the Mapper sends to the OCL API: none (the target repo), a path, or +// a URL on the API's origin. The API doesn't save a token for one. +export const isOCLAPILookupURL = (url, apiURL) => + !url || !isAbsoluteURL(url) || isAPIHost(url, apiURL) + +// The API never returns a saved token, only has_token. A custom algorithm with +// a saved key needs it entered again before it can run in this session. +export const needsSessionToken = algo => + Boolean(algo?.type === 'custom' && algo?.url && algo?.has_token && !algo?.token) + +// The same for a lookup on another host. +export const needsSessionLookupToken = (config, apiURL) => + Boolean(config?.has_token && !config?.token && !isOCLAPILookupURL(config?.url, apiURL)) + +// The URL a loaded entry's token was saved with, kept locally so has_token +// comes back if the URL is edited back to it. Never sent to the API. +export const TOKEN_URL_KEY = '__token_url' + +export const withSavedTokenURL = entry => + entry?.has_token ? {...entry, [TOKEN_URL_KEY]: entry.url} : entry + +export const getTokenFlag = (entry, url) => + (entry?.[TOKEN_URL_KEY] && url === entry[TOKEN_URL_KEY]) ? true : undefined + +// An emptied field keeps a saved token (the key is left out); '' clears one +// only once removeSavedToken has been used. +export const tokenInputValue = (entry, value) => + value?.trim() ? value : (entry?.[TOKEN_URL_KEY] ? undefined : '') + +export const removeSavedToken = entry => + ({...entry, token: '', has_token: undefined, [TOKEN_URL_KEY]: undefined}) + +// The entry as sent to the API: without the Mapper's own local keys. +export const withoutLocalKeys = entry => { + if(!entry || typeof entry !== 'object') + return entry + return Object.fromEntries(Object.entries(entry).filter(([key]) => key !== '__key' && key !== TOKEN_URL_KEY)) +} + +const TOKEN_KEYS = ['token', 'has_token', TOKEN_URL_KEY] + +// Removes tokens and their flags, at any depth: for copies and run snapshots. +export const withoutTokens = value => { + if(Array.isArray(value)) + return value.map(withoutTokens) + if(value && typeof value === 'object') + return Object.fromEntries( + Object.entries(value).filter(([key]) => !TOKEN_KEYS.includes(key)).map(([key, val]) => [key, withoutTokens(val)]) + ) + return value +}