-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shell: Add defaultDexConnector option to automatically trigger login on a predefined Dex connector #3443
shell: Add defaultDexConnector option to automatically trigger login on a predefined Dex connector #3443
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import { | |
} from 'oidc-react'; | ||
import { useShellConfig } from '../initFederation/ShellConfigProvider'; | ||
import { getUserGroups } from '../navbar/auth/permissionUtils'; | ||
import { WebStorageStateStore } from 'oidc-client'; | ||
import { MetadataService, WebStorageStateStore } from 'oidc-client'; | ||
|
||
export function AuthProvider({ children }: { children: Node }) { | ||
const { authConfig } = useAuthConfig(); | ||
|
@@ -25,6 +25,24 @@ export function AuthProvider({ children }: { children: Node }) { | |
return <OAuth2AuthProvider>{children}</OAuth2AuthProvider>; | ||
} | ||
|
||
function defaultDexConnectorMetadataService(connectorId: string) { | ||
class DexDefaultConnectorMetadataService extends MetadataService { | ||
getAuthorizationEndpoint() { | ||
return this._getMetadataProperty('authorization_endpoint').then( | ||
(authorizationEndpoint) => { | ||
const queryParamas = new URLSearchParams(window.location.search); | ||
if (!queryParamas.has('displayLoginChoice')) { | ||
return authorizationEndpoint + '?connector_id=' + connectorId; | ||
} | ||
return authorizationEndpoint; | ||
}, | ||
); | ||
} | ||
} | ||
|
||
return DexDefaultConnectorMetadataService; | ||
} | ||
|
||
function OAuth2AuthProvider({ children }: { children: Node }) { | ||
const { authConfig } = useAuthConfig(); | ||
|
||
|
@@ -39,6 +57,9 @@ function OAuth2AuthProvider({ children }: { children: Node }) { | |
loadUserInfo: true, | ||
automaticSilentRenew: true, | ||
monitorSession: false, | ||
MetadataServiceCtor: authConfig.defaultDexConnector | ||
? defaultDexConnectorMetadataService(authConfig.defaultDexConnector) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more a question: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't we start documenting all this rework of shell-ui? Would be useful IMO, both now for reviewers catching up with the progress, and in the future for maintenance / onboarding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Providing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened #3476 regarding the documentation and will address it as soon as possible |
||
: MetadataService, | ||
userStore: new WebStorageStateStore({ store: localStorage }), | ||
}); | ||
|
||
|
@@ -95,28 +116,45 @@ export function useLogOut() { | |
let auth; | ||
try { | ||
auth = useOauth2Auth(); | ||
} catch(e) { | ||
} catch (e) { | ||
//If an exception is raised here it is likely because the app is not using OIDC auth kind, so we can ignore this | ||
console.log('Failed to retrieve auth informations for OIDC auth kind', e) | ||
console.log('Failed to retrieve auth informations for OIDC auth kind', e); | ||
} | ||
|
||
return {logOut: useCallback(() => { | ||
if (!authConfig) { | ||
return; | ||
} | ||
|
||
if (authConfig.kind === 'OAuth2Proxy') { | ||
throw new Error('OAuth2Proxy authentication kind is not yet supported'); | ||
} | ||
if (auth && auth.userManager) { | ||
auth.userManager.revokeAccessToken(); | ||
if (authConfig.providerLogout) { | ||
auth.userManager.signoutRedirect(); | ||
} else { | ||
auth.userManager.removeUser(); | ||
location.reload(); | ||
return { | ||
logOut: useCallback(() => { | ||
if (!authConfig) { | ||
return; | ||
} | ||
} | ||
}, [JSON.stringify(authConfig), auth])} | ||
|
||
|
||
if (authConfig.kind === 'OAuth2Proxy') { | ||
throw new Error('OAuth2Proxy authentication kind is not yet supported'); | ||
} | ||
if (auth && auth.userManager) { | ||
auth.userManager.revokeAccessToken(); | ||
if (authConfig.providerLogout) { | ||
auth.userManager.signoutRedirect().catch((e) => { | ||
if (e.message === 'no end session endpoint') { | ||
console.log( | ||
"OIDC provider doesn't support end session endpoint, fallback to clearing document cookies", | ||
); | ||
document.cookie.split(';').forEach(function (c) { | ||
document.cookie = | ||
c.trim().split('=')[0] + | ||
'=;' + | ||
'expires=Thu, 01 Jan 1970 00:00:00 UTC;'; | ||
}); | ||
gdemonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
console.error(e); | ||
} | ||
auth.userManager.removeUser(); | ||
location.reload(); | ||
}); | ||
} else { | ||
auth.userManager.removeUser(); | ||
location.reload(); | ||
} | ||
} | ||
}, [JSON.stringify(authConfig), auth]), | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering should we add this
defaultDexConnector
prop to typeOIDCConfig
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in db1812c