Skip to content
Snippets Groups Projects
Commit 52c5d80a authored by Jack Reed's avatar Jack Reed Committed by Chris Beer
Browse files

Wipeaway the infoResponses for a given token that is reset

parent 9fb8fc5f
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ describe('info response reducer', () => { ...@@ -28,6 +28,7 @@ describe('info response reducer', () => {
content: 'lots of canvases and metadata and such', content: 'lots of canvases and metadata and such',
id: 'abc123', id: 'abc123',
}, },
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_INFO_RESPONSE, type: ActionTypes.RECEIVE_INFO_RESPONSE,
}, },
)).toMatchObject({ )).toMatchObject({
...@@ -35,6 +36,7 @@ describe('info response reducer', () => { ...@@ -35,6 +36,7 @@ describe('info response reducer', () => {
id: 'abc123', id: 'abc123',
isFetching: false, isFetching: false,
json: {}, json: {},
tokenServiceId: 'efg456',
}, },
}); });
}); });
...@@ -49,6 +51,7 @@ describe('info response reducer', () => { ...@@ -49,6 +51,7 @@ describe('info response reducer', () => {
{ {
error: "This institution didn't enable CORS.", error: "This institution didn't enable CORS.",
infoId: 'abc123', infoId: 'abc123',
tokenServiceId: 'efg456',
type: ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE, type: ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE,
}, },
)).toEqual({ )).toEqual({
...@@ -56,6 +59,7 @@ describe('info response reducer', () => { ...@@ -56,6 +59,7 @@ describe('info response reducer', () => {
error: "This institution didn't enable CORS.", error: "This institution didn't enable CORS.",
id: 'abc123', id: 'abc123',
isFetching: false, isFetching: false,
tokenServiceId: 'efg456',
}, },
}); });
}); });
...@@ -88,4 +92,27 @@ describe('info response reducer', () => { ...@@ -88,4 +92,27 @@ describe('info response reducer', () => {
type: ActionTypes.IMPORT_MIRADOR_STATE, type: ActionTypes.IMPORT_MIRADOR_STATE,
})).toEqual({}); })).toEqual({});
}); });
it('should handle RESET_AUTHENTICATION_STATE', () => {
expect(infoResponsesReducer(
{
abc123: {
stuff: 'foo',
tokenServiceId: 'abc123',
},
def456: {
stuff: 'foo',
tokenServiceId: 'def456',
},
},
{
tokenServiceId: 'abc123',
type: ActionTypes.RESET_AUTHENTICATION_STATE,
},
)).toEqual({
def456: {
stuff: 'foo',
tokenServiceId: 'def456',
},
});
});
}); });
...@@ -22,11 +22,12 @@ export function requestInfoResponse(infoId) { ...@@ -22,11 +22,12 @@ export function requestInfoResponse(infoId) {
* @param {Object} manifestJson * @param {Object} manifestJson
* @memberof ActionCreators * @memberof ActionCreators
*/ */
export function receiveInfoResponse(infoId, infoJson, ok) { export function receiveInfoResponse(infoId, infoJson, ok, tokenServiceId) {
return { return {
infoId, infoId,
infoJson, infoJson,
ok, ok,
tokenServiceId,
type: ActionTypes.RECEIVE_INFO_RESPONSE, type: ActionTypes.RECEIVE_INFO_RESPONSE,
}; };
} }
...@@ -38,15 +39,16 @@ export function receiveInfoResponse(infoId, infoJson, ok) { ...@@ -38,15 +39,16 @@ export function receiveInfoResponse(infoId, infoJson, ok) {
* @param {String} error * @param {String} error
* @memberof ActionCreators * @memberof ActionCreators
*/ */
export function receiveInfoResponseFailure(infoId, error) { export function receiveInfoResponseFailure(infoId, error, tokenServiceId) {
return { return {
error, error,
infoId, infoId,
tokenServiceId,
type: ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE, type: ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE,
}; };
} }
/** @private */ /** @private */
function getAccessToken({ accessTokens }, iiifService) { function getAccessTokenService({ accessTokens }, iiifService) {
if (!iiifService) return undefined; if (!iiifService) return undefined;
const services = Utils.getServices(iiifService).filter(s => s.getProfile().match(/http:\/\/iiif.io\/api\/auth\/1\//)); const services = Utils.getServices(iiifService).filter(s => s.getProfile().match(/http:\/\/iiif.io\/api\/auth\/1\//));
...@@ -55,7 +57,7 @@ function getAccessToken({ accessTokens }, iiifService) { ...@@ -55,7 +57,7 @@ function getAccessToken({ accessTokens }, iiifService) {
const authService = services[i]; const authService = services[i];
const accessTokenService = Utils.getService(authService, 'http://iiif.io/api/auth/1/token'); const accessTokenService = Utils.getService(authService, 'http://iiif.io/api/auth/1/token');
const token = accessTokens[accessTokenService.id]; const token = accessTokens[accessTokenService.id];
if (token && token.json) return token.json.accessToken; if (token && token.json) return token;
} }
return undefined; return undefined;
...@@ -79,21 +81,22 @@ export function fetchInfoResponse({ imageId, imageResource }) { ...@@ -79,21 +81,22 @@ export function fetchInfoResponse({ imageId, imageResource }) {
&& !state.infoResponses[infoId].isFetching && !state.infoResponses[infoId].isFetching
&& state.infoResponses[infoId].json; && state.infoResponses[infoId].json;
const token = getAccessToken( const tokenService = getAccessTokenService(
getState(), getState(),
infoResponse || (imageResource && imageResource.getServices()[0]), infoResponse || (imageResource && imageResource.getServices()[0]),
); );
if (token) { if (tokenService) {
headers.Authorization = `Bearer ${token}`; headers.Authorization = `Bearer ${tokenService.json.accessToken}`;
} }
dispatch(requestInfoResponse(infoId)); dispatch(requestInfoResponse(infoId));
const tokenServiceId = tokenService && tokenService.id;
return fetch(`${infoId.replace(/\/$/, '')}/info.json`, { headers }) return fetch(`${infoId.replace(/\/$/, '')}/info.json`, { headers })
.then(response => response.json().then(json => ({ json, ok: response.ok }))) .then(response => response.json().then(json => ({ json, ok: response.ok })))
.then(({ json, ok }) => dispatch(receiveInfoResponse(infoId, json, ok))) .then(({ json, ok }) => dispatch(receiveInfoResponse(infoId, json, ok, tokenServiceId)))
.catch(error => dispatch(receiveInfoResponseFailure(infoId, error))); .catch(error => dispatch(receiveInfoResponseFailure(infoId, error, tokenServiceId)));
}); });
} }
......
...@@ -22,6 +22,7 @@ export const infoResponsesReducer = (state = {}, action) => { ...@@ -22,6 +22,7 @@ export const infoResponsesReducer = (state = {}, action) => {
id: action.infoId, id: action.infoId,
isFetching: false, isFetching: false,
json: action.infoJson, json: action.infoJson,
tokenServiceId: action.tokenServiceId,
}, },
}; };
case ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE: case ActionTypes.RECEIVE_INFO_RESPONSE_FAILURE:
...@@ -31,6 +32,7 @@ export const infoResponsesReducer = (state = {}, action) => { ...@@ -31,6 +32,7 @@ export const infoResponsesReducer = (state = {}, action) => {
error: action.error, error: action.error,
id: action.infoId, id: action.infoId,
isFetching: false, isFetching: false,
tokenServiceId: action.tokenServiceId,
}, },
}; };
case ActionTypes.REMOVE_INFO_RESPONSE: case ActionTypes.REMOVE_INFO_RESPONSE:
...@@ -42,6 +44,13 @@ export const infoResponsesReducer = (state = {}, action) => { ...@@ -42,6 +44,13 @@ export const infoResponsesReducer = (state = {}, action) => {
}, {}); }, {});
case ActionTypes.IMPORT_MIRADOR_STATE: case ActionTypes.IMPORT_MIRADOR_STATE:
return {}; return {};
case ActionTypes.RESET_AUTHENTICATION_STATE:
return Object.keys(state).reduce((object, key) => {
if (state[key].tokenServiceId !== action.tokenServiceId) {
object[key] = state[key]; // eslint-disable-line no-param-reassign
}
return object;
}, {});
default: return state; default: return state;
} }
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment