Skip to content

Commit d2134f7

Browse files
Merge pull request #2625 from cfpb/5181-fix-keycloak-redirections-for-aws
We're taking [Kenneth's good work](#2385), making it work for more domains, and trying to get it into `master` so it doesn't have to get deployed separately. ## Changes - redirects to the correct keycloak instance for a given hostname (see the logic diagrams on the enterprise wiki for [Keycloak Redirect Logic as of October 8th 2025](https://[ENT]/HMDA-Operations/hmda-devops/wiki/Keycloak-Redirect-Logic-as-of-October-8th-2025)) - changes `checkLoginIframe` to false due to CORS issues now related to trying to access an iframe from another domain... <img width="1492" height="136" alt="Screenshot 2025-10-08 at 2 04 48 PM" src="https://github.com/user-attachments/assets/a5a6ae7d-9b83-4b70-9638-62a647da61c3" /> ... and probably future problems with it as cookies get more locked down based on some of my digging through keycloak docs and issues. Take a look at issue 23872 [on the keycloak repo](https://github.com/keycloak/keycloak/issues) (avoiding a direct link to the issue so we're not linked into it) for more info on browser incompatibilities. There are some downsides to not having it, particularly if someone logs off in another tab then it might not auto-detect the log out. Having said that, Kenneth's PR that implements it has been on [hmda beta side](https://ffiec.beta.cfpb.gov/) for almost a year without complaints. I still [made an issue](#2624) to follow up and investigate re-enabling it or ameliorating any issues, if we feel like we need to down the line. ## Testing 1. Does it work on all the environments? ### Local <img width="775" height="692" alt="5181-works-on-local" src="https://github.com/user-attachments/assets/b55cbcd5-0730-4255-8c36-a7d920a2dc6a" /> ### Dev <img width="613" height="587" alt="5181-works-on-hmdadev" src="https://github.com/user-attachments/assets/0e92a982-17fc-436f-821e-e1eba870384f" /> ### Dev Beta <img width="609" height="295" alt="5181-works-on-dev-beta" src="https://github.com/user-attachments/assets/d0f8a23b-9be8-432d-83cc-532844cbe11f" /> ### Prod Beta <img width="625" height="360" alt="5181-works-on-prod-beta" src="https://github.com/user-attachments/assets/6bac9d33-77ec-4fef-8d24-e26c2fb5d0a9" /> ### Prod (will wait till release) ### Alto environments (will need to wait to see on those) ## Notes - Although we're moving [Kenneth's PR](#2385) into master, I don't need to replicate `update prod-beta-config.json` like [Kenneth did](https://github.com/cfpb/hmda-frontend/pull/2385/files#diff-2ab1df37bbc1d33f86eebba246041c6c3a549ad602fb661251acaebcd096b1ee), because those changes were already [merged into master](https://github.com/cfpb/hmda-frontend/pull/2389/files#diff-2ab1df37bbc1d33f86eebba246041c6c3a549ad602fb661251acaebcd096b1eeR71) earlier.
2 parents 12f6fa5 + 384cef4 commit d2134f7

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/common/api/Keycloak.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@ let keycloak = null
44
let isInitialized = false
55
let initPromise = null
66

7+
// check out [ENT]/HMDA-Operations/hmda-devops/blob/master/eks/hmda_urls.md for more details
8+
// about the different HMDA environments
9+
10+
const prodKeycloakDomain = 'ffiec.cfpb.gov'
11+
const domainsToBeRedirectedToProdKeycloak = [
12+
'ffiec.beta.cfpb.gov',
13+
'prod-regtech',
14+
'ffiec-beta-test',
15+
'ffiec-test',
16+
]
17+
18+
const devKeycloakDomain = 'hmdadev.cfpb.gov'
19+
const domainsToBeRedirectedToDevKeycloak = ['hmda4-beta.demo']
20+
21+
const getKeycloakInstance = (hostname) => {
22+
const isRedirectedToProd = domainsToBeRedirectedToProdKeycloak.some(
23+
(domain) => hostname.includes(domain),
24+
)
25+
const isRedirectedToDev = domainsToBeRedirectedToDevKeycloak.some((domain) =>
26+
hostname.includes(domain),
27+
)
28+
29+
if (isRedirectedToProd) return prodKeycloakDomain
30+
if (isRedirectedToDev) return devKeycloakDomain
31+
32+
// if hostname doesn't match any known redirect patterns, assume keycloak is hosted on the same domain
33+
return hostname
34+
}
35+
36+
const hostname = window.location.hostname
37+
const keycloakInstance = getKeycloakInstance(hostname)
38+
39+
const keycloakConfig = {
40+
realm: 'hmda2',
41+
url: `https://${keycloakInstance}/auth`,
42+
clientId: 'hmda2-api',
43+
'public-client': true,
44+
'use-resource-role-mappings': true,
45+
'confidential-port': 0,
46+
'ssl-required': 'all',
47+
}
48+
749
export const setKeycloak = (cloak) => {
850
keycloak = cloak
951
return keycloak
@@ -28,12 +70,12 @@ export const initKeycloak = (overrides) => {
2870
} else if (import.meta.env.MODE === 'development') {
2971
keycloak = new Keycloak('/local_keycloak.json')
3072
} else {
31-
keycloak = new Keycloak('/keycloak.json')
73+
keycloak = new Keycloak(keycloakConfig)
3274
}
3375
}
3476

3577
initPromise = keycloak
36-
.init({ pkceMethod: 'S256' })
78+
.init({ pkceMethod: 'S256', checkLoginIframe: false })
3779
.then((authenticated) => {
3880
console.log('Keycloak initialized, authenticated:', authenticated)
3981
isInitialized = true

0 commit comments

Comments
 (0)