|
| 1 | +import { type SalesforceUrlPath } from '../../../browserforce.js'; |
| 2 | +import { BrowserforcePlugin } from '../../../plugin.js'; |
| 3 | + |
| 4 | +// Authentication Service page selectors |
| 5 | +const AUTH_SERVICE_EDIT_PATH = '/domainname/EditLogin.apexp'; |
| 6 | +const AUTH_SERVICE_FORM_SELECTOR = 'form[id="BrandSetup:brandSetupForm"]'; |
| 7 | +const AUTH_SERVICE_CHECKBOX_SELECTOR = 'input.authOption[type="checkbox"]'; |
| 8 | +const AUTH_SERVICE_SAVE_BUTTON_SELECTOR = 'input[id$=":Save"]'; |
| 9 | + |
| 10 | +export type Config = { |
| 11 | + authProviderId: string; |
| 12 | + developerName: string; |
| 13 | + enabled: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +export class AuthenticationService extends BrowserforcePlugin { |
| 17 | + public async retrieve(definition: Config): Promise<boolean> { |
| 18 | + // Navigate to Authentication Service edit page |
| 19 | + await using page = await this.browserforce.openPage(AUTH_SERVICE_EDIT_PATH as SalesforceUrlPath); |
| 20 | + const authServiceFrameOrPage = await this.browserforce.waitForSelectorInFrameOrPage( |
| 21 | + page, |
| 22 | + AUTH_SERVICE_FORM_SELECTOR, |
| 23 | + ); |
| 24 | + |
| 25 | + // Find the checkbox with value attribute matching the AuthProvider ID |
| 26 | + const checkboxLocators = await authServiceFrameOrPage.locator(AUTH_SERVICE_CHECKBOX_SELECTOR).all(); |
| 27 | + for (const checkboxLocator of checkboxLocators) { |
| 28 | + const value = await checkboxLocator.getAttribute('value'); |
| 29 | + if (definition.authProviderId.includes(value)) { |
| 30 | + return await checkboxLocator.isChecked(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + throw new Error( |
| 35 | + `Authentication Service checkbox not found for AuthProvider '${definition.developerName}' (ID: ${definition.authProviderId}). ` + |
| 36 | + `The AuthProvider may not be available in the Authentication Service configuration.`, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public async apply(config: Config): Promise<void> { |
| 41 | + this.browserforce.logger?.log('enableAuthenticationService', config.enabled.toString()); |
| 42 | + console.log(`[AuthenticationService] Updating Authentication Service for ${config.developerName} (ID: ${config.authProviderId} Enabled: ${config.enabled})`); |
| 43 | + |
| 44 | + // Navigate to Authentication Service edit page |
| 45 | + await using page = await this.browserforce.openPage(AUTH_SERVICE_EDIT_PATH as SalesforceUrlPath); |
| 46 | + const authServiceFrameOrPage = await this.browserforce.waitForSelectorInFrameOrPage( |
| 47 | + page, |
| 48 | + AUTH_SERVICE_FORM_SELECTOR, |
| 49 | + ); |
| 50 | + |
| 51 | + // Find the checkbox with value attribute matching the AuthProvider ID |
| 52 | + const checkboxLocators = await authServiceFrameOrPage.locator(AUTH_SERVICE_CHECKBOX_SELECTOR).all(); |
| 53 | + let checkboxFound = false; |
| 54 | + |
| 55 | + for (const checkboxLocator of checkboxLocators) { |
| 56 | + const value = await checkboxLocator.getAttribute('value'); |
| 57 | + if (config.authProviderId.includes(value)) { |
| 58 | + checkboxFound = true; |
| 59 | + const isChecked = await checkboxLocator.isChecked(); |
| 60 | + |
| 61 | + if (config.enabled !== isChecked) { |
| 62 | + // Check / Uncheck the checkbox |
| 63 | + if (config.enabled) { |
| 64 | + await checkboxLocator.check(); |
| 65 | + } else { |
| 66 | + await checkboxLocator.uncheck(); |
| 67 | + } |
| 68 | + console.log(`[AuthenticationService] Updated Authentication Service checkbox for ${config.developerName}`); |
| 69 | + } else { |
| 70 | + console.log(`[AuthenticationService] Authentication Service checkbox already updated for ${config.developerName}`); |
| 71 | + } |
| 72 | + |
| 73 | + // Save the changes |
| 74 | + await Promise.all([ |
| 75 | + page.waitForResponse((resp) => new URL(resp.url()).pathname === '/domainname/DomainName.apexp'), |
| 76 | + authServiceFrameOrPage.locator(AUTH_SERVICE_SAVE_BUTTON_SELECTOR).first().click(), |
| 77 | + ]); |
| 78 | + |
| 79 | + console.log(`[AuthenticationService] Saved Authentication Service configuration for ${config.developerName}`); |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (!checkboxFound) { |
| 85 | + throw new Error( |
| 86 | + `Authentication Service checkbox not found for AuthProvider '${config.developerName}' (ID: ${config.authProviderId}). ` + |
| 87 | + `The AuthProvider may not be available in the Authentication Service configuration.`, |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments