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
27 changes: 25 additions & 2 deletions app/javascript/packages/document-capture/context/acuant.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import AnalyticsContext from './analytics';

/** @typedef {import('react').ReactNode} ReactNode */

/**
* @typedef AcuantConfig
*
* @prop {string=} path Path from which to load SDK service worker.
*
* @see https://github.com/Acuant/JavascriptWebSDKV11/blob/11.4.3/SimpleHTMLApp/webSdk/dist/AcuantJavascriptWebSdk.js#L1025-L1027
* @see https://github.com/Acuant/JavascriptWebSDKV11/blob/11.4.3/SimpleHTMLApp/webSdk/dist/AcuantJavascriptWebSdk.js#L1049
*/

/**
* @typedef AcuantCamera
*
Expand Down Expand Up @@ -40,8 +49,9 @@ import AnalyticsContext from './analytics';
/**
* @typedef AcuantGlobals
*
* @prop {()=>void} onAcuantSdkLoaded Acuant initialization callback.
* @prop {AcuantCamera} AcuantCamera Acuant camera API.
* @prop {AcuantConfig=} acuantConfig Acuant configuration.
* @prop {()=>void} onAcuantSdkLoaded Acuant initialization callback.
* @prop {AcuantCamera} AcuantCamera Acuant camera API.
* @prop {AcuantJavaScriptWebSDK} AcuantJavascriptWebSdk Acuant web SDK.
*/

Expand Down Expand Up @@ -74,6 +84,15 @@ export const DEFAULT_ACCEPTABLE_GLARE_SCORE = 30;
*/
export const DEFAULT_ACCEPTABLE_SHARPNESS_SCORE = 30;

/**
* Returns the containing directory of the given file, including a trailing slash.
*
* @param {string} file
*
* @return {string}
*/
export const dirname = (file) => file.split('/').slice(0, -1).concat('').join('/');

const AcuantContext = createContext({
isReady: false,
isAcuantLoaded: false,
Expand Down Expand Up @@ -175,6 +194,9 @@ function AcuantContextProvider({
);
};

const originalAcuantConfig = /** @type {AcuantGlobal} */ (window).acuantConfig;
/** @type {AcuantGlobal} */ (window).acuantConfig = { path: dirname(sdkSrc) };

const script = document.createElement('script');
script.async = true;
script.src = sdkSrc;
Expand All @@ -183,6 +205,7 @@ function AcuantContextProvider({

return () => {
/** @type {AcuantGlobal} */ (window).onAcuantSdkLoaded = originalOnAcuantSdkLoaded;
/** @type {AcuantGlobal} */ (window).acuantConfig = originalAcuantConfig;
document.body.removeChild(script);
};
}, []);
Expand Down
5 changes: 4 additions & 1 deletion config/initializers/secure_headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def call(env)
product(%w[doc_auth capture_doc]).
map do |locale, flow|
File.join('/', *locale, '/verify', flow, worker_js)
end.to_set.freeze
end.
push('/acuant/11.4.3/AcuantImageProcessingWorker.min.js').
to_set.
freeze

config.middleware.insert_before(
SecureHeaders::Middleware,
Expand Down
10 changes: 10 additions & 0 deletions spec/javascripts/packages/document-capture/context/acuant-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useContext } from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { DeviceContext, AnalyticsContext } from '@18f/identity-document-capture';
import AcuantContext, {
dirname,
Provider as AcuantContextProvider,
DEFAULT_ACCEPTABLE_GLARE_SCORE,
DEFAULT_ACCEPTABLE_SHARPNESS_SCORE,
Expand All @@ -15,6 +16,15 @@ describe('document-capture/context/acuant', () => {
delete window.AcuantCamera;
});

describe('dirname', () => {
it('returns the containing directory with trailing slash', () => {
const file = '/acuant/AcuantJavascriptWebSdk.min.js';
const result = dirname(file);

expect(result).to.equal('/acuant/');
});
});

it('provides default context value', () => {
const { result } = renderHook(() => useContext(AcuantContext));

Expand Down
6 changes: 4 additions & 2 deletions spec/requests/acuant_sdk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
# - /en/verify/capture_doc/AcuantImageProcessingWorker.min.js
[nil, *I18n.available_locales].
product(%w[doc_auth capture_doc]).
each do |locale, verify_path|
map do |locale, verify_path|
base_url = "#{locale && "/#{locale}"}/verify/#{verify_path}"

end.
push('/acuant/11.4.3').
each do |base_url|
min_js = "#{base_url}/AcuantImageProcessingWorker.min.js"
context min_js do
before { get min_js }
Expand Down