Automates Okta login form with username and password challenge
Size
4.6 KB
Version
1.0.1
Created
Nov 24, 2025
Updated
22 days ago
1// ==UserScript==
2// @name Okta Login Automator
3// @description Automates Okta login form with username and password challenge
4// @version 1.0.1
5// @match https://*.labcorp-holdings.okta.com/*
6// @icon https://labcorp-holdings.okta.com/bc/image/fileStoreRecord?id=fs0k01xcdevEfkBih1t7
7// @grant GM.getValue
8// @grant GM.setValue
9// ==/UserScript==
10(function() {
11 'use strict';
12
13 console.log('Okta Login Automator started');
14
15 // Configuration - Set your credentials here or they will be prompted
16 const CONFIG = {
17 username: 'hemantha.kallati@labcorp.com', // Change this to your email
18 password: '', // Set your password here or leave empty to be prompted
19 autoSubmit: true, // Automatically submit forms
20 delay: 1000 // Delay in ms before auto-submitting
21 };
22
23 // Debounce function to prevent multiple rapid calls
24 function debounce(func, wait) {
25 let timeout;
26 return function executedFunction(...args) {
27 const later = () => {
28 clearTimeout(timeout);
29 func(...args);
30 };
31 clearTimeout(timeout);
32 timeout = setTimeout(later, wait);
33 };
34 }
35
36 // Handle username form (first step)
37 async function handleUsernameForm() {
38 const usernameInput = document.querySelector('input[name="identifier"][type="text"]');
39 const nextButton = document.querySelector('input[type="submit"][value="Next"]');
40
41 if (!usernameInput || !nextButton) {
42 console.log('Username form not found');
43 return false;
44 }
45
46 console.log('Username form detected');
47
48 // Fill username if empty
49 if (!usernameInput.value && CONFIG.username) {
50 usernameInput.value = CONFIG.username;
51 console.log('Username filled');
52 }
53
54 // Auto-submit if enabled
55 if (CONFIG.autoSubmit && usernameInput.value) {
56 console.log('Auto-submitting username form in ' + CONFIG.delay + 'ms');
57 setTimeout(() => {
58 nextButton.click();
59 console.log('Username form submitted');
60 }, CONFIG.delay);
61 }
62
63 return true;
64 }
65
66 // Handle password form (second step)
67 async function handlePasswordForm() {
68 const passwordInput = document.querySelector('input[name="credentials.passcode"][type="password"]');
69 const verifyButton = document.querySelector('input[type="submit"][value="Verify"]');
70
71 if (!passwordInput || !verifyButton) {
72 console.log('Password form not found');
73 return false;
74 }
75
76 console.log('Password form detected');
77
78 // Get stored password or use config
79 let password = CONFIG.password;
80 if (!password) {
81 password = await GM.getValue('okta_password', '');
82 }
83
84 // Fill password if we have it and field is empty
85 if (password && !passwordInput.value) {
86 passwordInput.value = password;
87 console.log('Password filled');
88 }
89
90 // Auto-submit if enabled and password is filled
91 if (CONFIG.autoSubmit && passwordInput.value) {
92 console.log('Auto-submitting password form in ' + CONFIG.delay + 'ms');
93 setTimeout(() => {
94 verifyButton.click();
95 console.log('Password form submitted');
96 }, CONFIG.delay);
97 }
98
99 return true;
100 }
101
102 // Main function to check and handle forms
103 async function checkAndHandleForms() {
104 // Try to handle username form first
105 const usernameHandled = await handleUsernameForm();
106 if (usernameHandled) return;
107
108 // If not username form, try password form
109 const passwordHandled = await handlePasswordForm();
110 if (passwordHandled) return;
111
112 console.log('No login form detected on this page');
113 }
114
115 // Debounced version to prevent multiple rapid calls
116 const debouncedCheck = debounce(checkAndHandleForms, 500);
117
118 // Observer to watch for DOM changes
119 const observer = new MutationObserver(debouncedCheck);
120
121 // Initialize
122 function init() {
123 console.log('Initializing Okta Login Automator');
124
125 // Check immediately
126 checkAndHandleForms();
127
128 // Observe DOM changes for dynamic content
129 observer.observe(document.body, {
130 childList: true,
131 subtree: true
132 });
133
134 console.log('Observer started - watching for login forms');
135 }
136
137 // Wait for page to be ready
138 if (document.readyState === 'loading') {
139 document.addEventListener('DOMContentLoaded', init);
140 } else {
141 init();
142 }
143
144})();