Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/components/map-projects/LookupConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
<Button size='small' variant='text' sx={{textTransform: 'none', minWidth: 0, padding: '0 4px'}} onClick={() => onChange(removeSavedToken(value))}>
{t('map_project.remove_saved_token')}
</Button>
</>
)
}

return (
<div className='col-xs-12 padding-0' style={{marginBottom: '16px', marginTop: '4px'}}>
Expand All @@ -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)})
}}
/>
</div>
<div className='col-xs-6' style={{padding: '0 0 0 4px'}}>
Expand All @@ -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: (
Expand All @@ -57,7 +79,7 @@ const LookupConfig = ({value, onChange}) => {
</InputAdornment>
),
},
formHelperText: {sx: {color: 'warning.main'}},
formHelperText: tokenHasSpaces ? {sx: {color: 'warning.main'}} : {},
}}
/>
</div>
Expand Down
48 changes: 33 additions & 15 deletions src/components/map-projects/MapProject.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] || '')
Expand Down Expand Up @@ -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'])}))
Expand Down Expand Up @@ -1014,15 +1016,16 @@ 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 || '')
setDescription(response.data?.description || '')
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 || '')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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})
}
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 22 additions & 5 deletions src/components/map-projects/MultiAlgoSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: '',
Expand All @@ -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 || ''}
/>
<TextField
fullWidth
required={Boolean(customMeta.tokenRequired)}
required={Boolean(customMeta.tokenRequired || needsSessionToken(sel))}
type='password'
label={t('map_project.api_token')}
value={sel.token || ""}
onChange={(e) => {
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: '',
Expand All @@ -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) && (
<Button size='small' variant='text' sx={{textTransform: 'none', minWidth: 0, padding: '0 4px'}} onClick={() => updateSelected(sel.__key, removeSavedToken(sel))}>
{t('map_project.remove_saved_token')}
</Button>
)}
</>
) : ''
}
/>
<Stack direction={{ xs: "column", sm: "row" }} spacing={1.5}>
<TextField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ test('lookupTokenHasSpaces flags whitespace inside the token only', () => {
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, '')
})
8 changes: 7 additions & 1 deletion src/components/map-projects/lookupConfigFields.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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).
// Only leading/trailing whitespace is removed; a space inside the token is
// 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.
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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}})",
Expand Down
Loading
Loading