| 1 |
/** |
| 2 |
* Duke Energy OAuth Helper - Background Service Worker |
| 3 |
* |
| 4 |
* This extension intercepts OAuth redirects from Duke Energy's Auth0 mobile |
| 5 |
* flow and extracts the authorization code. Two redirect patterns are handled: |
| 6 |
* |
| 7 |
* - https://login.duke-energy.com/ios/... (iOS HTTPS redirect) |
| 8 |
* - cma-prod://... (Android/legacy custom-scheme redirect) |
| 9 |
* |
| 10 |
* Both are caught in onBeforeNavigate. |
| 11 |
* onErrorOccurred catches cma-prod:// in case Chrome fires the error before the navigation is |
| 12 |
* intercepted. |
| 13 |
* |
| 14 |
* If the state parameter is a JWT containing a flow_id, it automatically |
| 15 |
* redirects to Home Assistant's OAuth endpoint. |
| 16 |
*/ |
| 17 |
|
| 18 |
// Check if state is a JWT with flow_id (for Home Assistant) |
| 19 |
function isHomeAssistantFlow(state) { |
| 20 |
if (!state) return false; |
| 21 |
try { |
| 22 |
// JWT has 3 parts separated by dots |
| 23 |
const parts = state.split("."); |
| 24 |
if (parts.length !== 3) return false; |
| 25 |
|
| 26 |
// Decode the payload (middle part) |
| 27 |
const payload = JSON.parse(atob(parts[1])); |
| 28 |
return payload && typeof payload.flow_id === "string"; |
| 29 |
} catch (e) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Shared handler for both iOS and Android OAuth redirects. |
| 35 |
function handleOAuthRedirect(tabId, url) { |
| 36 |
const urlObj = new URL(url); |
| 37 |
const code = urlObj.searchParams.get("code"); |
| 38 |
const state = urlObj.searchParams.get("state"); |
| 39 |
|
| 40 |
if (code && isHomeAssistantFlow(state)) { |
| 41 |
// Redirect to Home Assistant OAuth endpoint |
| 42 |
const haUrl = new URL("https://my.home-assistant.io/redirect/oauth"); |
| 43 |
haUrl.searchParams.set("code", code); |
| 44 |
haUrl.searchParams.set("state", state); |
| 45 |
chrome.tabs.update(tabId, { url: haUrl.toString() }); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Otherwise, show capture page |
| 50 |
const captureUrl = new URL(chrome.runtime.getURL("capture.html")); |
| 51 |
captureUrl.searchParams.set("code", code || ""); |
| 52 |
captureUrl.searchParams.set("state", state || ""); |
| 53 |
captureUrl.searchParams.set("error", urlObj.searchParams.get("error") || ""); |
| 54 |
captureUrl.searchParams.set( |
| 55 |
"error_description", |
| 56 |
urlObj.searchParams.get("error_description") || "", |
| 57 |
); |
| 58 |
|
| 59 |
if (code) { |
| 60 |
chrome.action.setBadgeText({ text: "✓" }); |
| 61 |
chrome.action.setBadgeBackgroundColor({ color: "#4CAF50" }); |
| 62 |
} |
| 63 |
|
| 64 |
chrome.tabs.update(tabId, { url: captureUrl.toString() }); |
| 65 |
} |
| 66 |
|
| 67 |
// Catch Auth0 pushState URL changes (no real navigation fires onBeforeNavigate). |
| 68 |
// This handles the case where Auth0's Universal Login uses history.pushState |
| 69 |
// to update the address bar to the callback URL after credential validation, |
| 70 |
// rather than issuing a real HTTP redirect. |
| 71 |
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { |
| 72 |
const url = changeInfo.url; |
| 73 |
if (url && url.startsWith("https://login.duke-energy.com/ios/")) { |
| 74 |
handleOAuthRedirect(tabId, url); |
| 75 |
} |
| 76 |
}); |
| 77 |
|
| 78 |
// Intercept iOS HTTPS redirects and Android custom-scheme redirects before |
| 79 |
// the browser attempts to load them (covers real page loads and refreshes). |
| 80 |
chrome.webNavigation.onBeforeNavigate.addListener((details) => { |
| 81 |
const url = details.url; |
| 82 |
|
| 83 |
if ( |
| 84 |
url.startsWith("https://login.duke-energy.com/ios/") || |
| 85 |
url.startsWith("cma-prod://") |
| 86 |
) { |
| 87 |
handleOAuthRedirect(details.tabId, url); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
// Fallback: catch cma-prod:// if Chrome fires onErrorOccurred before |
| 92 |
// onBeforeNavigate can redirect the tab. |
| 93 |
chrome.webNavigation.onErrorOccurred.addListener((details) => { |
| 94 |
if (details.url && details.url.startsWith("cma-prod://")) { |
| 95 |
handleOAuthRedirect(details.tabId, details.url); |
| 96 |
} |
| 97 |
}); |