-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-0000 - Expose Keycloak Interface
This allows keycloak to be initialized before application bootstrap, substantially optimizing application start time/responsiveness and allowing keycloak access to the URL before the angular router has a chance to mangle it.
- Loading branch information
Showing
8 changed files
with
2,466 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @author Kevin Nielsen <[email protected]> | ||
* @author Robert Parker <[email protected]> | ||
* | ||
* @copyright Alert Logic, Inc 2019 | ||
*/ | ||
|
||
import { WebAuth } from 'auth0-js'; | ||
import Keycloak, { KeycloakLoginOptions, KeycloakOnLoad } from 'keycloak-js'; | ||
import { | ||
AlBehaviorPromise, | ||
AlCabinet, | ||
AlLocation, | ||
AlLocatorService, | ||
AlStopwatch, | ||
} from '../../common'; | ||
import { AlDefaultClient } from '../../client'; | ||
import { AlConduitClient } from './al-conduit-client'; | ||
|
||
export class AlIdentityProviders | ||
{ | ||
/** | ||
* Keycloak and Auth0 client instances | ||
*/ | ||
protected static keycloak:Keycloak = undefined; | ||
protected storage = AlCabinet.persistent("alnav"); | ||
protected allIsLost = false; | ||
|
||
constructor() { | ||
} | ||
|
||
public async warmup() { | ||
try { | ||
await this.getKeycloak(); | ||
} catch( e ) { | ||
console.error( e ); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve a keycloak authentication interface. | ||
*/ | ||
public async getKeycloak():Promise<Keycloak> { | ||
if ( ! AlIdentityProviders.keycloak ) { | ||
const fortraPlatformUri = AlLocatorService.resolveURL( AlLocation.FortraPlatform, '/idp' ); | ||
AlIdentityProviders.keycloak = new Keycloak( { | ||
url: fortraPlatformUri, | ||
realm: 'products', | ||
clientId: 'alertlogic-aims-public', | ||
} ); | ||
|
||
await this.innerGetKeyCloak( AlIdentityProviders.keycloak ); | ||
} | ||
return AlIdentityProviders.keycloak; | ||
} | ||
|
||
/** | ||
* Uses a race to make sure that keycloak initialization doesn't time out -- since a misconfigured client can cause the | ||
* promise to hang indefinitely. | ||
*/ | ||
protected async innerGetKeyCloak( cloak:Keycloak, timeout:number = 5000 ):Promise<void> { | ||
return Promise.race( [ AlStopwatch.promise( timeout ), | ||
new Promise<void>( async ( resolve, reject ) => { | ||
let cloakPhase = this.storage.get("cloakInitPhase", 0 ); | ||
let onLoad:KeycloakOnLoad|undefined = cloakPhase === 0 ? "check-sso" : undefined; | ||
let silentCheckSsoRedirectUri = cloakPhase === 0 ? `${window.location.origin}/sso-check.html` : undefined; | ||
this.storage.set("cloakInitPhase", cloakPhase + 1, 10 ).synchronize(); | ||
if ( cloakPhase > 5 ) { | ||
this.allIsLost = true; | ||
console.log("Refusing to initialize keycloak after too many redirect cycles" ); | ||
resolve(); | ||
} else { | ||
console.log("Initializing cloak in phase [%s]: %s", cloakPhase, onLoad ); | ||
let initResult = await cloak.init( { | ||
onLoad, | ||
silentCheckSsoRedirectUri, | ||
enableLogging: true, | ||
checkLoginIframe: true, | ||
checkLoginIframeInterval: 30, | ||
pkceMethod: 'S256', | ||
responseMode: "query", | ||
messageReceiveTimeout: 5000 | ||
} ); | ||
if ( ! initResult && cloakPhase < 2 ) { | ||
cloak.login( { prompt: 'none', redirectUri: window.location.href } ); | ||
} else { | ||
resolve(); | ||
} | ||
} | ||
} ) ] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters