|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { identity } from 'lodash'; |
| 8 | +import { uiModules } from 'ui/modules'; |
| 9 | +import { Path } from 'plugins/xpack_main/services/path'; |
| 10 | +import { xpackInfo } from 'plugins/xpack_main/services/xpack_info'; |
| 11 | +import { xpackInfoSignature } from 'plugins/xpack_main/services/xpack_info_signature'; |
| 12 | + |
| 13 | +const module = uiModules.get('xpack_main', []); |
| 14 | + |
| 15 | +module.factory('checkXPackInfoChange', ($q, Private, $injector) => { |
| 16 | + /** |
| 17 | + * Intercept each network response to look for the kbn-xpack-sig header. |
| 18 | + * When that header is detected, compare its value with the value cached |
| 19 | + * in the browser storage. When the value is new, call `xpackInfo.refresh()` |
| 20 | + * so that it will pull down the latest x-pack info |
| 21 | + * |
| 22 | + * @param {object} response - the angular $http response object |
| 23 | + * @param {function} handleResponse - callback, expects to receive the response |
| 24 | + * @return |
| 25 | + */ |
| 26 | + function interceptor(response, handleResponse) { |
| 27 | + if (Path.isUnauthenticated()) { |
| 28 | + return handleResponse(response); |
| 29 | + } |
| 30 | + |
| 31 | + const currentSignature = response.headers('kbn-xpack-sig'); |
| 32 | + const cachedSignature = xpackInfoSignature.get(); |
| 33 | + |
| 34 | + if (currentSignature && cachedSignature !== currentSignature) { |
| 35 | + // Signature from the server differ from the signature of our |
| 36 | + // cached info, so we need to refresh it. |
| 37 | + // Intentionally swallowing this error |
| 38 | + // because nothing catches it and it's an ugly console error. |
| 39 | + xpackInfo.refresh($injector).catch(() => {}); |
| 40 | + } |
| 41 | + |
| 42 | + return handleResponse(response); |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + response: (response) => interceptor(response, identity), |
| 47 | + responseError: (response) => interceptor(response, $q.reject) |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +module.config(($httpProvider) => { |
| 52 | + $httpProvider.interceptors.push('checkXPackInfoChange'); |
| 53 | +}); |
0 commit comments