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
29 changes: 27 additions & 2 deletions src/common/__tests__/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import test from 'node:test'
import assert from 'node:assert/strict'

import { getEnv, toAppOrigin } from '../env.js'
import { getEnv, isTBv3URL, toAppOrigin } from '../env.js'

const envAt = origin => {
globalThis.window = {location: {origin}}
Expand Down Expand Up @@ -32,13 +32,38 @@ test('app.v2.<env>.openconceptlab.org is that environment', () => {
assert.deepEqual(envAt('https://app.v2.dev.openconceptlab.org'), ['dev', 'dev'])
})

test('app.classic[.<env>].openconceptlab.org is that environment', () => {
assert.deepEqual(envAt('https://app.classic.openconceptlab.org'), ['production', ''])
assert.deepEqual(envAt('https://app.classic.staging.openconceptlab.org'), ['staging', 'staging'])
assert.deepEqual(envAt('https://app.classic.qa.openconceptlab.org'), ['qa', 'qa'])
assert.deepEqual(envAt('https://app.classic.demo.openconceptlab.org'), ['demo', 'demo'])
assert.deepEqual(envAt('https://app.classic.dev.openconceptlab.org'), ['dev', 'dev'])
})

test('any other host is development', () => {
assert.deepEqual(envAt('http://localhost:4000'), ['development', 'development'])
})

test('toAppOrigin maps app.v2.<env> to app.<env> and leaves other origins alone', () => {
test('toAppOrigin maps app.v2.<env> and app.classic.<env> to app.<env> and leaves other origins alone', () => {
assert.equal(toAppOrigin('https://app.v2.openconceptlab.org'), 'https://app.openconceptlab.org')
assert.equal(toAppOrigin('https://app.v2.staging.openconceptlab.org'), 'https://app.staging.openconceptlab.org')
assert.equal(toAppOrigin('https://app.classic.openconceptlab.org'), 'https://app.openconceptlab.org')
assert.equal(toAppOrigin('https://app.classic.qa.openconceptlab.org'), 'https://app.qa.openconceptlab.org')
assert.equal(toAppOrigin('https://app.v3.qa.openconceptlab.org'), 'https://app.v3.qa.openconceptlab.org')
assert.equal(toAppOrigin('https://app.qa.openconceptlab.org'), 'https://app.qa.openconceptlab.org')
assert.equal(toAppOrigin('http://localhost:4000'), 'http://localhost:4000')
})

test('isTBv3URL accepts app.<env> and app.v3.<env>, and not TBv2\'s hosts', () => {
assert.equal(isTBv3URL('https://app.openconceptlab.org/?auth=true'), true)
assert.equal(isTBv3URL('https://app.staging.openconceptlab.org/'), true)
assert.equal(isTBv3URL('https://app.v3.qa.openconceptlab.org/#/orgs/CIEL/'), true)
assert.equal(isTBv3URL('https://app.openconceptlab.org'), true)
assert.equal(isTBv3URL('https://app.v2.openconceptlab.org/'), false)
assert.equal(isTBv3URL('https://app.classic.openconceptlab.org/'), false)
assert.equal(isTBv3URL('https://app.classic.staging.openconceptlab.org/'), false)
assert.equal(isTBv3URL('https://map.openconceptlab.org/'), false)
assert.equal(isTBv3URL('https://app.openconceptlab.org.evil.example/'), false)
assert.equal(isTBv3URL('http://app.openconceptlab.org/'), false)
assert.equal(isTBv3URL(undefined), false)
})
11 changes: 8 additions & 3 deletions src/common/env.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TBv2 is served from app[.<env>].openconceptlab.org and from app.v2[.<env>].openconceptlab.org, where it stays
// once TBv3 becomes the default Term Browser (OpenConceptLab/ocl_online#236); either host names the same environment.
export const toAppOrigin = origin => origin.replace('://app.v2.', '://app.')
// TBv2 is served from app[.<env>].openconceptlab.org and from app.v2[.<env>] and app.classic[.<env>].openconceptlab.org,
// where it stays once TBv3 becomes the default Term Browser; each host names the same environment.
export const toAppOrigin = origin => origin.replace(/:\/\/app\.(v2|classic)\./, '://app.')

export const getEnv = forURL => {
const fqdn = toAppOrigin(window.location.origin);
Expand All @@ -20,3 +20,8 @@ export const getEnv = forURL => {

return 'development';
}

// TBv3 is the default Term Browser at app[.<env>].openconceptlab.org and is still at app.v3[.<env>]. The other app hosts
// (app.v2, app.classic) are TBv2's.
const TBV3_URL = /^https:\/\/app\.(v3\.)?([a-z]+\.)*openconceptlab\.org([/?#]|$)/
export const isTBv3URL = url => TBV3_URL.test(url || '') && !/^https:\/\/app\.classic\./.test(url)
14 changes: 5 additions & 9 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import GAService from '../services/GAService';
import { ensureIdToken } from './ssoSession';
import { SERVER_CONFIGS } from './serverConfigs';
import { windowConfig } from './windowConfig';
import { getEnv, toAppOrigin } from './env';
import { getEnv, isTBv3URL, toAppOrigin } from './env';

export const currentPath = () => window.location.hash.split('?')[0];

Expand Down Expand Up @@ -972,8 +972,8 @@ export const consumeAndValidateOAuthState = returnedState => {
return !returnedState || returnedState === storedState
}

// Sign-in, sign-up and sign-out come back to the host they started on (app.<env> or app.v2.<env>), since the
// PKCE verifier and OAuth state are in that origin's sessionStorage. LOGIN_REDIRECT_URL only turns SSO on.
// Sign-in, sign-up and sign-out come back to the host they started on (app.<env>, app.v2.<env> or app.classic.<env>),
// since the PKCE verifier and OAuth state are in that origin's sessionStorage. LOGIN_REDIRECT_URL only turns SSO on.
export const getLoginURL = async returnTo => {
let redirectURL = window.location.origin
const oidClientID = windowConfig('OIDC_RP_CLIENT_ID') || process.env.OIDC_RP_CLIENT_ID
Expand Down Expand Up @@ -1283,11 +1283,7 @@ export const isV3URL = url => {
return false
if(url.startsWith('http://localhost:4002'))
return true
if(!url.includes('.openconceptlab.org'))
return false
if(url.startsWith('https://app.v3.'))
return true
return false
return isTBv3URL(url)
}

export const isCommunitySiteURL = url => {
Expand All @@ -1312,6 +1308,6 @@ export const isRedirectingToLoginViaReferrer = location => {
const queryParams = new URLSearchParams(search)
const referrer = queryParams.get('referrer')
const parts = hash ? hash.split('?') : referrer?.split('?')
let params = params?.length > 0 ? new URLSearchParams(parts[1]) : {}
const params = new URLSearchParams(parts?.[1] || '')
return isOtherOCLClientURL(referrer) && params.get('auth') === 'true'
}
Loading