-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@jitsu/console): add generic OIDC provider SSO (#1152)
* feat: add generic oidc sso option * chore: add AUTH_OIDC_PROVIDER var at .env example * chore: ensure pg healthcheck has same user on devenv compose * chore: remove zookeeper dependency on kafka at devenv compose * chore: add keycloak service at devenv compose * chore: ensure correct parse of AUTH_OIDC_PROVIDER env * fix: change oidc oauth types import path * Fix for auth provider check * visual tweaks
- Loading branch information
1 parent
26f79e6
commit 34420de
Showing
5 changed files
with
135 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers/oauth"; | ||
import { ApiError } from "./shared/errors"; | ||
|
||
export interface OIDCProfile extends Record<string, any> { | ||
sub: string; | ||
name: string; | ||
preferred_username: string; | ||
nickname: string; | ||
email: string; | ||
picture: string; | ||
} | ||
|
||
export type OIDCConfig<P> = OAuthUserConfig<P> & Required<Pick<OAuthConfig<P>, "issuer">>; | ||
|
||
/** | ||
* Creates an OAuth configuration for an OpenID Connect (OIDC) Discovery compliant provider. | ||
* | ||
* @template P - The type of the profile, extending `OIDCProfile`. | ||
* | ||
* @param {OIDCConfig<P>} options - The user configuration options for OAuth authentication. | ||
* | ||
* @returns {OAuthConfig<P>} - An OIDC provider NextAuthJS valid configuration. | ||
* | ||
* @throws {ApiError} - Throws an error if the required fields `issuer`, `clientId`, or `clientSecret` | ||
* are not provided in the options parameter. | ||
* | ||
* @description | ||
* Initializes an OAuth configuration object for a generic OIDC provider that is compliant with the OIDC Discovery. It requires | ||
* the `issuer` (the issuer domain in valid URL format), `clientId`, and `clientSecret` fields in the options. This configuration | ||
* includes default settings for handling the PKCE and state checks and provides | ||
* a profile extraction mechanism. | ||
* | ||
* The well-known configuration endpoint for the provider is automatically set based on the issuer, and | ||
* the default authorization request includes scopes for OpenID, email, and profile information. | ||
*/ | ||
export function OIDCProvider<P extends OIDCProfile>(options: OIDCConfig<P>): OAuthConfig<P> { | ||
if (!options.issuer || !options.clientId || !options.clientSecret) { | ||
throw new ApiError("Malformed OIDC config: issuer, clientId, and clientSecret are required"); | ||
} | ||
|
||
return { | ||
id: "oidc", | ||
name: "OIDC", | ||
wellKnown: `${options.issuer}/.well-known/openid-configuration`, | ||
type: "oauth", | ||
authorization: { params: { scope: "openid email profile" } }, | ||
checks: ["pkce", "state"], | ||
idToken: true, | ||
profile(profile) { | ||
return { | ||
id: profile.sub, | ||
name: profile.name ?? profile.preferred_username ?? profile.nickname, | ||
email: profile.email, | ||
image: profile.picture, | ||
}; | ||
}, | ||
options, | ||
}; | ||
} | ||
|
||
export function ParseJSONConfigFromEnv<P extends OIDCProfile>(env: string): OIDCConfig<P> | undefined { | ||
try { | ||
return env && env != '""' ? (JSON.parse(env) as OIDCConfig<P>) : undefined; | ||
} catch (error: unknown) { | ||
console.error("Failed to parse JSON config from env", error); | ||
return undefined; | ||
} | ||
} |
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