Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bratislava.sk support #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ export const enabledUrls = [
"https://pfseform.financnasprava.sk/*",
"https://www.financnasprava.sk/*",
"https://eformulare.socpoist.sk/*",
"https://city-account-next.dev.bratislava.sk/*",
"https://city-account-next.staging.bratislava.sk/*",
"https://konto.bratislava.sk/*",
...(process.env.NODE_ENV !== "production" ? ["http://localhost:3000/*"] : []),
];
71 changes: 55 additions & 16 deletions src/dbridge_js/inject-ditec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import { ditecX } from "./ditecx/ditecx";

type OriginalDitec = object;
type OriginalDitec = { dSigXadesBpJs?: object };

const useProxy = false;
const useProxyWithOriginal = useProxy && false;
export function inject(windowAny: { ditec?: OriginalDitec }): void {

const DITEC_INITIALIZATION_TIMEOUT = 30000;
const DITEC_CHECK_INTERVAL = 10;

/**
* Periodically checks if Ditec was initialized (or timeout was reached).
*
* Most of the websites load the signer synchronously, but e.g. konto.bratislava.sk loads it asynchronously, so it's
* necessary to wait for it.
*/
function waitForDitec(windowAny: { ditec?: OriginalDitec }) {
if (windowAny.ditec?.dSigXadesBpJs) {
return Promise.resolve();
}

return new Promise<void>((resolve, reject) => {
// eslint-disable-next-line prefer-const
let interval: NodeJS.Timeout;

const timeout = setTimeout(() => {
clearInterval(interval);
reject(new Error(`Ditec was not initialized within ${DITEC_INITIALIZATION_TIMEOUT}ms timeout.`));
}, DITEC_INITIALIZATION_TIMEOUT);

interval = setInterval(() => {
// Check for `windowAny.ditec` is not enough (e.g. only config is loaded).
if (windowAny.ditec?.dSigXadesBpJs) {
clearTimeout(timeout);
clearInterval(interval);
resolve();
}
}, DITEC_CHECK_INTERVAL);
});
}

export async function inject(windowAny: { ditec?: OriginalDitec }) {
console.log("Start inject");
console.log(windowAny.ditec);

if (windowAny.ditec) {
if (useProxy) {
import("./proxy").then(({ wrapWithProxy }) => {
windowAny.ditec = useProxyWithOriginal
? wrapWithProxy(windowAny.ditec)
: wrapWithProxy(ditecX);
});
} else {
windowAny.ditec = ditecX;
}

// windowAny.ditec = wrapWithProxy(ditecX);
try {
await waitForDitec(windowAny);
} catch (e) {
console.error(e);
return;
}
console.log(windowAny.ditec)

if (useProxy) {
import("./proxy").then(({ wrapWithProxy }) => {
windowAny.ditec = useProxyWithOriginal
? wrapWithProxy(windowAny.ditec)
: wrapWithProxy(ditecX);
});
} else {
windowAny.ditec = ditecX;
}

// windowAny.ditec = wrapWithProxy(ditecX);
}
2 changes: 1 addition & 1 deletion src/entrypoint/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject } from "../dbridge_js/inject-ditec";

type WindowWithDitec = Window & { ditec?: object };
type WindowWithDitec = Window & { ditec?: { dSigXadesBpJs?: object } };

const windowAny = window as WindowWithDitec;

Expand Down