-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add support for single-sign-on via openid connect authentication #16
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { useRef, useCallback } from 'react'; | ||
|
|
||
| /** | ||
| * useOnceAtATime | ||
| * | ||
| * A React hook that ensures an async function is only executed once at a time. | ||
| * While the function is in-flight, all subsequent calls return the same Promise. | ||
| * Once the function resolves or rejects, it can be called again. | ||
| * | ||
| * Useful for deduplicating concurrent requests (e.g. token validation, lazy loading). | ||
| * | ||
| * @template TArgs - Argument types of the async function | ||
| * @template TResult - Return type of the async function | ||
| * | ||
| * @param fn - The async function to guard | ||
| * @returns An object with: | ||
| * - run: the deduplicated function | ||
| * - isRunning: boolean indicating whether the function is currently executing | ||
| */ | ||
| export function useOnceAtATime<TArgs extends any[], TResult>( | ||
| fn: (...args: TArgs) => Promise<TResult> | ||
| ) { | ||
| const inFlightRef = useRef<Promise<TResult> | null>(null); | ||
|
|
||
| const run = useCallback((...args: TArgs): Promise<TResult> => { | ||
| if (inFlightRef.current) return inFlightRef.current; | ||
|
|
||
| inFlightRef.current = (async () => { | ||
| try { | ||
| return await fn(...args); | ||
| } finally { | ||
| inFlightRef.current = null; // allow future calls | ||
| } | ||
| })(); | ||
|
|
||
| return inFlightRef.current; | ||
| }, [fn]); | ||
|
|
||
| const isRunning = !!inFlightRef.current; | ||
|
|
||
| return { run, isRunning }; | ||
| } | ||
This file contains hidden or 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,89 @@ | ||
| export interface IOpenIdConnectSettings | ||
| { | ||
| /** | ||
| * Enables or disables OpenID Connect authentication. | ||
| * | ||
| * Set to `true` to activate OIDC support in your application. If `false`, all OIDC logic | ||
| * (such as login redirects and token handling) will be disabled, even if configs are defined. | ||
| */ | ||
| enabled: boolean | ||
| /** | ||
| * An array of OpenID Connect configuration objects. | ||
| * Each defines the settings required to authenticate against a specific identity provider. | ||
| * At least one configuration must be provided. | ||
| */ | ||
| configs: IOpenIdConnectConfig[] | ||
| /** | ||
| * True will automatically redirect the user to the | ||
| * first openIdConnect config stored if the token is expired, or invalid. | ||
| * This is a simplified use case. For more control, or when you need to | ||
| * handle multiple identity providers set this to false and handle redirect on your | ||
| * own by calling `loginWithOpenIdConnect` | ||
| */ | ||
| autoRedirect?: boolean | ||
| /** | ||
| * The name of the query parameter under which the ordercloud access token will be stored under after successful login. | ||
| * This will vary based on your [OpenIdConnect.AppStartUrl](https://ordercloud.io/api-reference/authentication-and-authorization/open-id-connects/create). | ||
| * For example if your `AppStartUrl` is `https://my-buyer-application.com/login?token={0}` then the value should be `token` | ||
| */ | ||
| accessTokenQueryParamName: string | ||
| /** | ||
| * The **optional** name of the query parameter under which the ordercloud refresh token will be stored | ||
| * under after successful login. This will vary based on your [OpenIdConnect.AppStartUrl](https://ordercloud.io/api-reference/authentication-and-authorization/open-id-connects/create). | ||
| * For example if your `AppStartUrl` is `https://my-buyer-application.com/login?token={0}&refresh={3}` then the value should be `refresh` | ||
| */ | ||
| refreshTokenQueryParamName?: string | ||
| /** | ||
| * The **optional** name of the query parameter under which the idp access token will be stored | ||
| * under after successful login. This will vary based on your [OpenIdConnect.AppStartUrl](https://ordercloud.io/api-reference/authentication-and-authorization/open-id-connects/create). | ||
| * For example if your `AppStartUrl` is `https://my-buyer-application.com/login?token={0}&idptoken={1}` then the value should be `idptoken` | ||
| */ | ||
| idpAccessTokenQueryParamName?: string | ||
| /** | ||
| * An **optional** path to redirect the user to after returning from the identity provider. | ||
| * See [here](https://ordercloud.io/knowledge-base/sso-via-openid-connect#deep-linking) for more information | ||
| * This global setting will be used if not overridden by the `appStartPath` in the individual OpenID Connect configurations. | ||
| * Call `setAppStartPath()` to change this value at runtime. | ||
| */ | ||
| appStartPath?: string | ||
| /** | ||
| * **optional** query parameters passed along to the `AuthorizationEndpoint`. | ||
| * See [here](https://ordercloud.io/knowledge-base/sso-via-openid-connect) for more information | ||
| * This global setting will be used if not overridden by the `customParams` in the individual OpenID Connect configurations. | ||
| * Call `setCustomParams()` to change this value at runtime. | ||
| */ | ||
| customParams?: string | ||
| } | ||
|
|
||
| export interface IOpenIdConnectConfig | ||
| { | ||
| /** | ||
| * The ID of the [OpenID connect configuration](https://ordercloud.io/api-reference/authentication-and-authorization/open-id-connects/create) | ||
| * that should be targeted for authentication | ||
| */ | ||
| id: string | ||
| /** | ||
| * An **optional** array of roles that will be requested when authenticating. | ||
| * If excluded, the token generated will contain any roles assigned to the user. | ||
| * Unless you have a specific reason for limiting roles, we recommend omitting this option. | ||
| */ | ||
| roles?: string[] | ||
| /** | ||
| * An **optional** OrderCloud clientId to authenticate against. | ||
| * By default, will use `clientId` at the root of the provider settings. | ||
| */ | ||
| clientId?: string | ||
| /** | ||
| * An **optional** path to redirect the user to after returning from the identity provider. | ||
| * See [here](https://ordercloud.io/knowledge-base/sso-via-openid-connect#deep-linking) for more information | ||
| * call `setAppStartPath(openIdConnectConfigId)` to change this value at runtime. | ||
| */ | ||
| appStartPath?: string | ||
|
|
||
| /** | ||
| * **optional** query parameters passed along to the `AuthorizationEndpoint`. | ||
| * See [here](https://ordercloud.io/knowledge-base/sso-via-openid-connect) for more information | ||
| * call `setCustomParams(openIdConnectConfigId)` to change this value at runtime. | ||
| */ | ||
| customParams?: string | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 had to use this with
verifyTokento solve a race condition I was facing where a second invocation thought I wasn't logged in even though I was.