Skip to content

Commit

Permalink
fix: missing script injection
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-peruzzo committed Oct 24, 2024
1 parent de40c0d commit 8a4b9b1
Showing 1 changed file with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LogoutOtherSessionsComponent } from '@keycloakify/angular/login/compone
import { KcClassDirective } from '@keycloakify/angular/login/directives/kc-class';
import type { I18n } from '@keycloakify/angular/login/i18n';
import type { KcContext } from '@keycloakify/angular/login/KcContext';
import { LoginResourceInjectorService } from '@keycloakify/angular/login/services/login-resource-injector';
import { LOGIN_CLASSES } from '@keycloakify/angular/login/tokens/classes';
import { LOGIN_I18N } from '@keycloakify/angular/login/tokens/i18n';
import { KC_LOGIN_CONTEXT } from '@keycloakify/angular/login/tokens/kc-context';
Expand All @@ -26,6 +27,7 @@ import type { ClassKey } from 'keycloakify/login/lib/kcClsx';
export class LoginRecoveryAuthnCodeConfigComponent extends ComponentReference {
kcContext = inject<Extract<KcContext, { pageId: 'login-recovery-authn-code-config.ftl' }>>(KC_LOGIN_CONTEXT);
i18n = inject<I18n>(LOGIN_I18N);
loginResourceInjectorService = inject(LoginResourceInjectorService);

override doUseDefaultCss = inject<boolean>(USE_DEFAULT_CSS);
override classes = inject<Partial<Record<ClassKey, string>>>(LOGIN_CLASSES);
Expand All @@ -44,4 +46,125 @@ export class LoginRecoveryAuthnCodeConfigComponent extends ComponentReference {
toggleRecoveryCodesConfirmation = signal(false);

olRecoveryCodesListId = 'kc-recovery-codes-list';

constructor() {
super();
this.loginResourceInjectorService.insertAdditionalScripts([
{
type: 'text/javascript',
id: `${this.olRecoveryCodesListId}-script`,
textContent: `
/* copy recovery codes */
function copyRecoveryCodes() {
var tmpTextarea = document.createElement("textarea");
var codes = document.querySelectorAll("#${this.olRecoveryCodesListId} li");
for (i = 0; i < codes.length; i++) {
tmpTextarea.value = tmpTextarea.value + codes[i].innerText + "\\n";
}
document.body.appendChild(tmpTextarea);
tmpTextarea.select();
document.execCommand("copy");
document.body.removeChild(tmpTextarea);
}
var copyButton = document.getElementById("copyRecoveryCodes");
copyButton && copyButton.addEventListener("click", function () {
copyRecoveryCodes();
});
/* download recovery codes */
function formatCurrentDateTime() {
var dt = new Date();
var options = {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short'
};
return dt.toLocaleString('en-US', options);
}
function parseRecoveryCodeList() {
var recoveryCodes = document.querySelectorAll("#${this.olRecoveryCodesListId} li");
var recoveryCodeList = "";
for (var i = 0; i < recoveryCodes.length; i++) {
var recoveryCodeLiElement = recoveryCodes[i].innerText;
recoveryCodeList += recoveryCodeLiElement + "\\r\\n";
}
return recoveryCodeList;
}
function buildDownloadContent() {
var recoveryCodeList = parseRecoveryCodeList();
var dt = new Date();
var options = {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short'
};
return fileBodyContent =
${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-header'))} + "\\n\\n" +
recoveryCodeList + "\\n" +
${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-description'))} + "\\n\\n" +
${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-date'))} + " " + formatCurrentDateTime();
}
function setUpDownloadLinkAndDownload(filename, text) {
var el = document.createElement('a');
el.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
el.setAttribute('download', filename);
el.style.display = 'none';
document.body.appendChild(el);
el.click();
document.body.removeChild(el);
}
function downloadRecoveryCodes() {
setUpDownloadLinkAndDownload('kc-download-recovery-codes.txt', buildDownloadContent());
}
var downloadButton = document.getElementById("downloadRecoveryCodes");
downloadButton && downloadButton.addEventListener("click", downloadRecoveryCodes);
/* print recovery codes */
function buildPrintContent() {
var recoveryCodeListHTML = document.getElementById('${this.olRecoveryCodesListId}').innerHTML;
var styles =
\`@page { size: auto; margin-top: 0; }
body { width: 480px; }
div { list-style-type: none; font-family: monospace }
p:first-of-type { margin-top: 48px }\`;
return printFileContent =
"<html><style>" + styles + "</style><body>" +
"<title>kc-download-recovery-codes</title>" +
"<p>" + ${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-header'))} + "</p>" +
"<div>" + recoveryCodeListHTML + "</div>" +
"<p>" + ${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-description'))} + "</p>" +
"<p>" + ${JSON.stringify(this.i18n.msgStr('recovery-codes-download-file-date'))} + " " + formatCurrentDateTime() + "</p>" +
"</body></html>";
}
function printRecoveryCodes() {
var w = window.open();
w.document.write(buildPrintContent());
w.print();
w.close();
}
var printButton = document.getElementById("printRecoveryCodes");
printButton && printButton.addEventListener("click", printRecoveryCodes);
`
}
]);
}
}

0 comments on commit 8a4b9b1

Please sign in to comment.