Skip to content
Snippets Groups Projects
Commit 5bc07298 authored by Chris Beer's avatar Chris Beer
Browse files

Move canvas auth selectors into auth

parent c8fdd86a
No related branches found
No related tags found
No related merge requests found
......@@ -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,
};
};
......
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),
);
},
);
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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment