Skip to content

Commit d8a603b

Browse files
committed
feat: use DV local storage key prefix and pass it trough js-dataverse
1 parent f75fc38 commit d8a603b

File tree

6 files changed

+101
-21
lines changed

6 files changed

+101
-21
lines changed

src/App.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { Router } from './router'
55
import { SessionProvider } from './sections/session/SessionProvider'
66
import { UserJSDataverseRepository } from './users/infrastructure/repositories/UserJSDataverseRepository'
77
import { Route } from './sections/Route.enum'
8-
import { DATAVERSE_BACKEND_URL } from './config'
8+
import { OIDC_AUTH_CONFIG, DATAVERSE_BACKEND_URL } from './config'
99
import 'react-loading-skeleton/dist/skeleton.css'
1010

1111
if (DATAVERSE_BACKEND_URL === '') {
1212
throw Error('VITE_DATAVERSE_BACKEND_URL environment variable should be specified.')
1313
} else {
14-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
14+
ApiConfig.init(
15+
`${DATAVERSE_BACKEND_URL}/api/v1`,
16+
DataverseApiAuthMechanism.BEARER_TOKEN,
17+
undefined,
18+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
19+
)
1520
}
1621

1722
const origin = window.location.origin
@@ -26,7 +31,8 @@ const authConfig: TAuthConfig = {
2631
redirectUri: `${origin}${BASENAME_URL}${Route.AUTH_CALLBACK}`,
2732
scope: 'openid',
2833
autoLogin: false,
29-
clearURL: false
34+
clearURL: false,
35+
storageKeyPrefix: OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX
3036
}
3137

3238
const userRepository = new UserJSDataverseRepository()

src/axiosInstance.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios'
2-
import { DATAVERSE_BACKEND_URL } from './config'
2+
import { OIDC_AUTH_CONFIG, DATAVERSE_BACKEND_URL } from './config'
33
import { Utils } from './shared/helpers/Utils'
44

55
/**
@@ -12,7 +12,9 @@ const axiosInstance = axios.create({
1212
})
1313

1414
axiosInstance.interceptors.request.use((config) => {
15-
const token = Utils.getLocalStorageItem<string>('ROCP_token')
15+
const token = Utils.getLocalStorageItem<string>(
16+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}_token`
17+
)
1618

1719
if (token) {
1820
config.headers.Authorization = `Bearer ${token}`

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export const DATAVERSE_BACKEND_URL = (import.meta.env.VITE_DATAVERSE_BACKEND_URL as string) ?? ''
2+
3+
export const OIDC_AUTH_CONFIG = {
4+
LOCAL_STORAGE_KEY_PREFIX: 'DV_'
5+
}

tests/e2e-integration/integration/collection/CollectionJSDataverseRepository.spec.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
UpwardHierarchyNode
88
} from '../../../../src/shared/hierarchy/domain/models/UpwardHierarchyNode'
99
import { Collection } from '../../../../src/collection/domain/models/Collection'
10-
import { DATAVERSE_BACKEND_URL } from '@/config'
10+
import { DATAVERSE_BACKEND_URL, OIDC_AUTH_CONFIG } from '@/config'
1111
import {
1212
ApiConfig,
1313
DataverseApiAuthMechanism
@@ -47,7 +47,12 @@ describe('Collection JSDataverse Repository', () => {
4747

4848
// Change the api config to use bearer token
4949
cy.wrap(
50-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
50+
ApiConfig.init(
51+
`${DATAVERSE_BACKEND_URL}/api/v1`,
52+
DataverseApiAuthMechanism.BEARER_TOKEN,
53+
undefined,
54+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
55+
)
5156
)
5257

5358
await collectionRepository.getById(collectionResponse.id).then((collection) => {
@@ -65,7 +70,12 @@ describe('Collection JSDataverse Repository', () => {
6570

6671
// Change the api config to use bearer token
6772
cy.wrap(
68-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
73+
ApiConfig.init(
74+
`${DATAVERSE_BACKEND_URL}/api/v1`,
75+
DataverseApiAuthMechanism.BEARER_TOKEN,
76+
undefined,
77+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
78+
)
6979
)
7080

7181
await collectionRepository.publish(collectionResponse.id)

tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { CollectionHelper } from '../../shared/collection/CollectionHelper'
2323
const DRAFT_PARAM = DatasetNonNumericVersion.DRAFT
2424
import { VersionUpdateType } from '../../../../src/dataset/domain/models/VersionUpdateType'
2525
import { ApiConfig } from '@iqss/dataverse-client-javascript'
26-
import { DATAVERSE_BACKEND_URL } from '@/config'
26+
import { DATAVERSE_BACKEND_URL, OIDC_AUTH_CONFIG } from '@/config'
2727
import { DataverseApiAuthMechanism } from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig'
2828

2929
chai.use(chaiAsPromised)
@@ -163,7 +163,12 @@ describe('Dataset JSDataverse Repository', () => {
163163

164164
// Change the api config to use bearer token
165165
cy.wrap(
166-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
166+
ApiConfig.init(
167+
`${DATAVERSE_BACKEND_URL}/api/v1`,
168+
DataverseApiAuthMechanism.BEARER_TOKEN,
169+
undefined,
170+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
171+
)
167172
)
168173

169174
await datasetRepository
@@ -197,7 +202,12 @@ describe('Dataset JSDataverse Repository', () => {
197202

198203
// Change the api config to use bearer token
199204
cy.wrap(
200-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
205+
ApiConfig.init(
206+
`${DATAVERSE_BACKEND_URL}/api/v1`,
207+
DataverseApiAuthMechanism.BEARER_TOKEN,
208+
undefined,
209+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
210+
)
201211
)
202212

203213
await datasetRepository
@@ -248,7 +258,12 @@ describe('Dataset JSDataverse Repository', () => {
248258

249259
// Change the api config to use bearer token
250260
cy.wrap(
251-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
261+
ApiConfig.init(
262+
`${DATAVERSE_BACKEND_URL}/api/v1`,
263+
DataverseApiAuthMechanism.BEARER_TOKEN,
264+
undefined,
265+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
266+
)
252267
)
253268

254269
await datasetRepository
@@ -288,7 +303,12 @@ describe('Dataset JSDataverse Repository', () => {
288303

289304
// Change the api config to use bearer token
290305
cy.wrap(
291-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
306+
ApiConfig.init(
307+
`${DATAVERSE_BACKEND_URL}/api/v1`,
308+
DataverseApiAuthMechanism.BEARER_TOKEN,
309+
undefined,
310+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
311+
)
292312
)
293313

294314
await datasetRepository
@@ -310,7 +330,12 @@ describe('Dataset JSDataverse Repository', () => {
310330

311331
// Change the api config to use bearer token
312332
cy.wrap(
313-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
333+
ApiConfig.init(
334+
`${DATAVERSE_BACKEND_URL}/api/v1`,
335+
DataverseApiAuthMechanism.BEARER_TOKEN,
336+
undefined,
337+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
338+
)
314339
)
315340

316341
await datasetRepository.getByPrivateUrlToken(privateUrlResponse.token).then((dataset) => {
@@ -335,7 +360,12 @@ describe('Dataset JSDataverse Repository', () => {
335360

336361
// Change the api config to use bearer token
337362
cy.wrap(
338-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
363+
ApiConfig.init(
364+
`${DATAVERSE_BACKEND_URL}/api/v1`,
365+
DataverseApiAuthMechanism.BEARER_TOKEN,
366+
undefined,
367+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
368+
)
339369
)
340370

341371
await datasetRepository
@@ -361,7 +391,12 @@ describe('Dataset JSDataverse Repository', () => {
361391

362392
// Change the api config to use bearer token
363393
cy.wrap(
364-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
394+
ApiConfig.init(
395+
`${DATAVERSE_BACKEND_URL}/api/v1`,
396+
DataverseApiAuthMechanism.BEARER_TOKEN,
397+
undefined,
398+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
399+
)
365400
)
366401

367402
return datasetRepository
@@ -388,7 +423,12 @@ describe('Dataset JSDataverse Repository', () => {
388423

389424
// Change the api config to use bearer token
390425
cy.wrap(
391-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
426+
ApiConfig.init(
427+
`${DATAVERSE_BACKEND_URL}/api/v1`,
428+
DataverseApiAuthMechanism.BEARER_TOKEN,
429+
undefined,
430+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
431+
)
392432
)
393433

394434
await datasetRepository.getByPersistentId(datasetResponse.persistentId).then((dataset) => {
@@ -406,7 +446,12 @@ describe('Dataset JSDataverse Repository', () => {
406446

407447
// Change the api config to use bearer token
408448
cy.wrap(
409-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
449+
ApiConfig.init(
450+
`${DATAVERSE_BACKEND_URL}/api/v1`,
451+
DataverseApiAuthMechanism.BEARER_TOKEN,
452+
undefined,
453+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
454+
)
410455
)
411456

412457
await datasetRepository
@@ -430,7 +475,12 @@ describe('Dataset JSDataverse Repository', () => {
430475
it('creates a new dataset from DatasetDTO', async () => {
431476
// Change the api config to use bearer token
432477
cy.wrap(
433-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
478+
ApiConfig.init(
479+
`${DATAVERSE_BACKEND_URL}/api/v1`,
480+
DataverseApiAuthMechanism.BEARER_TOKEN,
481+
undefined,
482+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
483+
)
434484
)
435485

436486
const datasetDTO: DatasetDTO = {
@@ -470,7 +520,12 @@ describe('Dataset JSDataverse Repository', () => {
470520

471521
// Change the api config to use bearer token
472522
cy.wrap(
473-
ApiConfig.init(`${DATAVERSE_BACKEND_URL}/api/v1`, DataverseApiAuthMechanism.BEARER_TOKEN)
523+
ApiConfig.init(
524+
`${DATAVERSE_BACKEND_URL}/api/v1`,
525+
DataverseApiAuthMechanism.BEARER_TOKEN,
526+
undefined,
527+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
528+
)
474529
)
475530

476531
await datasetRepository.publish(datasetResponse.persistentId).then((response) => {

tests/support/commands.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { SessionProvider } from '../../src/sections/session/SessionProvider'
4848
import { MemoryRouter } from 'react-router-dom'
4949
import { TestsUtils } from '@tests/e2e-integration/shared/TestsUtils'
5050
import { Utils } from '@/shared/helpers/Utils'
51+
import { OIDC_AUTH_CONFIG } from '@/config'
5152

5253
// Define your custom mount function
5354

@@ -88,7 +89,9 @@ Cypress.Commands.add('login', () => {
8889
cy.url()
8990
.should('eq', `${Cypress.config().baseUrl as string}/spa`)
9091
.then(() => {
91-
const token = Utils.getLocalStorageItem<string>('ROCP_token')
92+
const token = Utils.getLocalStorageItem<string>(
93+
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}_token`
94+
)
9295

9396
return cy.wrap(token)
9497
})

0 commit comments

Comments
 (0)