diff --git a/src/containers/IIIFAuthentication.js b/src/containers/IIIFAuthentication.js index 819796c8c6c516d4ebbe801de0e8f95cf2b27e0b..f1cd7f0db3f83006b87d0c2ab56533e1a5739984 100644 --- a/src/containers/IIIFAuthentication.js +++ b/src/containers/IIIFAuthentication.js @@ -7,6 +7,7 @@ import * as actions from '../state/actions'; import { getCurrentCanvas, getAuth, + isInteractiveAuth, selectCanvasAuthService, getAccessTokens, } from '../state/selectors'; @@ -37,8 +38,6 @@ const mapStateToProps = (state, { windowId }) => { e => e.id === accessTokenService.id, ); - const profile = service && service.getProfile(); - let status; if (!authStatus) { @@ -57,7 +56,7 @@ const mapStateToProps = (state, { windowId }) => { status = 'failed'; } - const isInteractive = profile !== 'http://iiif.io/api/auth/1/external' && profile !== 'http://iiif.io/api/auth/1/kiosk'; + const isInteractive = isInteractiveAuth(state, { canvasId, windowId }); return { accessTokenServiceId: accessTokenService && accessTokenService.id, @@ -70,7 +69,7 @@ const mapStateToProps = (state, { windowId }) => { isInteractive, label: service && service.getLabel()[0].value, logoutServiceId: logoutService && logoutService.id, - profile, + profile: service && service.getProfile(), status, }; }; diff --git a/src/state/selectors/auth.js b/src/state/selectors/auth.js index c1d75341d9e3bc86a3d02aef9c074f24730b3964..5e63c387ac41e8b46028e7bcdeab84331eeda438 100644 --- a/src/state/selectors/auth.js +++ b/src/state/selectors/auth.js @@ -1,7 +1,82 @@ +import { createSelector } from 'reselect'; +import { Utils } from 'manifesto.js/dist-esmodule/Utils'; +import MiradorCanvas from '../../lib/MiradorCanvas'; import { miradorSlice } from './utils'; +import { getConfig } from './config'; +import { selectInfoResponse, getCanvas } from './canvases'; /** */ export const getAccessTokens = state => miradorSlice(state).accessTokens || {}; /** */ export const getAuth = state => miradorSlice(state).auth || {}; + +export const selectCanvasAuthService = createSelector( + [ + selectInfoResponse, + getCanvas, + getConfig, + getAuth, + ], + (infoResponse, canvas, { auth: { serviceProfiles = [] } }, auth) => { + let iiifResource; + iiifResource = infoResponse && infoResponse.json && { ...infoResponse.json, options: {} }; + + if (!iiifResource && canvas) { + const miradorCanvas = new MiradorCanvas(canvas); + const [image] = miradorCanvas.iiifImageResources; + + iiifResource = image && image.getServices()[0]; + } + + if (!iiifResource) return undefined; + + const orderedAuthServiceProfiles = serviceProfiles.map(p => p.profile); + + let lastAttemptedService; + + for (const profile of orderedAuthServiceProfiles) { + const services = getServices(iiifResource, profile); + for (const service of services) { + if (!auth[service.id]) return service; + + lastAttemptedService = service; + + if (auth[service.id].isFetching || auth[service.id].ok) return service; + } + } + + return lastAttemptedService; + }, +); + +/** */ +export function selectAuthStatus({ auth }, service) { + if (!service) return null; + if (!auth[service.id]) return null; + if (auth[service.id].isFetching) return 'fetching'; + if (auth[service.id].ok) return 'ok'; + return 'failed'; +} + +/** Get all the services that match a profile */ +function getServices(resource, profile) { + const services = Utils.getServices(resource); + + return services.filter(service => service.getProfile() === profile); +} + +/** check if the current auth service is "interactive" */ +export const isInteractiveAuth = createSelector( + [ + selectCanvasAuthService, + getConfig, + ], + (service, { auth: { serviceProfiles = [] } }) => { + const profile = service && service.getProfile(); + + return serviceProfiles.some( + config => config.profile === profile && !(config.external || config.kiosk), + ); + }, +); diff --git a/src/state/selectors/canvases.js b/src/state/selectors/canvases.js index eefaea802eef05203aa0a980bc2ea6e0cad36aab..684676bcd8fb15756f7a5246b2c6b535ef12046c 100644 --- a/src/state/selectors/canvases.js +++ b/src/state/selectors/canvases.js @@ -1,12 +1,9 @@ import { createSelector } from 'reselect'; -import { Utils } from 'manifesto.js/dist-esmodule/Utils'; import flatten from 'lodash/flatten'; import CanvasGroupings from '../../lib/CanvasGroupings'; import MiradorCanvas from '../../lib/MiradorCanvas'; import { miradorSlice } from './utils'; import { getWindow } from './getters'; -import { getConfig } from './config'; -import { getAuth } from './auth'; import { getSequence } from './sequences'; import { getWindowViewType } from './windows'; @@ -208,58 +205,3 @@ export const selectInfoResponse = createSelector( && infoResponses[iiifServiceId]; }, ); - -export const selectCanvasAuthService = createSelector( - [ - selectInfoResponse, - getCanvas, - getConfig, - getAuth, - ], - (infoResponse, canvas, { auth: { serviceProfiles = [] } }, auth) => { - let iiifResource; - iiifResource = infoResponse && infoResponse.json && { ...infoResponse.json, options: {} }; - - if (!iiifResource && canvas) { - const miradorCanvas = new MiradorCanvas(canvas); - const [image] = miradorCanvas.iiifImageResources; - - iiifResource = image && image.getServices()[0]; - } - - if (!iiifResource) return undefined; - - const orderedAuthServiceProfiles = serviceProfiles.map(p => p.profile); - - let lastAttemptedService; - - for (const profile of orderedAuthServiceProfiles) { - const services = getServices(iiifResource, profile); - for (const service of services) { - if (!auth[service.id]) return service; - - lastAttemptedService = service; - - if (auth[service.id].isFetching || auth[service.id].ok) return service; - } - } - - return lastAttemptedService; - }, -); - -/** */ -export function selectAuthStatus({ auth }, service) { - if (!service) return null; - if (!auth[service.id]) return null; - if (auth[service.id].isFetching) return 'fetching'; - if (auth[service.id].ok) return 'ok'; - return 'failed'; -} - -/** Get all the services that match a profile */ -function getServices(resource, profile) { - const services = Utils.getServices(resource); - - return services.filter(service => service.getProfile() === profile); -}