Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { describe, expect, it } from "vitest";
import { getProviderSetupLinks } from "#/components/features/super-admin/auth-provider-setup-links";

describe("getProviderSetupLinks", () => {
it("links GitHub to the OAuth app pages on github.com when no URL is saved", () => {
expect(getProviderSetupLinks("github", ["", null])).toEqual([
{
kind: "create",
url: "https://github.com/settings/applications/new",
service: "GitHub",
},
{
kind: "list",
url: "https://github.com/settings/developers",
service: "GitHub",
},
]);
});

it("follows the entered authorization URL for GitHub Enterprise Server", () => {
const links = getProviderSetupLinks("github", [
"https://ghe.example.com/login/oauth/authorize",
"https://github.com/login/oauth/access_token",
]);
expect(links.map((l) => l.url)).toEqual([
"https://ghe.example.com/settings/applications/new",
"https://ghe.example.com/settings/developers",
]);
});

it("uses the token URL when the authorization URL is empty or a placeholder", () => {
const tokenUrl = "https://gitlab.corp.io/oauth/token";
expect(getProviderSetupLinks("gitlab", ["", tokenUrl])[0].url).toBe(
"https://gitlab.corp.io/-/user_settings/applications",
);
expect(
getProviderSetupLinks("gitlab", [
"https://PLACEHOLDER.COM/oauth/authorize",
tokenUrl,
])[0].url,
).toBe("https://gitlab.corp.io/-/user_settings/applications");
});

it("falls back to the brand default for unparsable or placeholder URLs", () => {
expect(getProviderSetupLinks("github", ["not a url"])[0].url).toBe(
"https://github.com/settings/applications/new",
);
expect(
getProviderSetupLinks("gitlab", [
"https://PLACEHOLDER.COM/oauth/authorize",
])[0].url,
).toBe("https://gitlab.com/-/user_settings/applications");
});

it("links GitLab user and admin application pages", () => {
expect(
getProviderSetupLinks("gitlab", [
"https://gitlab.corp.io/oauth/authorize",
]),
).toEqual([
{
kind: "list",
url: "https://gitlab.corp.io/-/user_settings/applications",
service: "GitLab",
},
{
kind: "admin",
url: "https://gitlab.corp.io/admin/applications",
service: "GitLab",
},
]);
});

it("builds no link for a brand with no default host and only placeholder URLs", () => {
expect(
getProviderSetupLinks("forgejo", [
"https://YOUR_FORGEJO_HOST/login/oauth/authorize",
]),
).toEqual([]);
});

it("names the external service on every link", () => {
expect(getProviderSetupLinks("bitbucket", [])).toEqual([
expect.objectContaining({ kind: "docs", service: "Atlassian" }),
]);
expect(getProviderSetupLinks("bitbucket_data_center", [])).toEqual([
expect.objectContaining({ kind: "docs", service: "Atlassian" }),
]);
expect(getProviderSetupLinks("azure_devops", [])).toEqual([
expect.objectContaining({ kind: "list", service: "Microsoft Entra" }),
expect.objectContaining({ kind: "docs", service: "Microsoft Learn" }),
]);
});

it("has no links for generic enterprise SSO", () => {
expect(getProviderSetupLinks("enterprise_sso", [])).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,56 @@ describe("Super Admin Auth Provider edit page", () => {
);
});

it("links a GitHub row to the GitHub OAuth app pages", async () => {
const form = await renderEditPage(GITHUB_PROVIDER);

const links = within(form).getByTestId(
"super-admin-auth-provider-setup-links",
);
const create = within(links).getByTestId(
"super-admin-auth-provider-setup-link-create",
);
expect(create).toHaveAttribute(
"href",
"https://github.com/settings/applications/new",
);
expect(create).toHaveAttribute("target", "_blank");
expect(create).toHaveAttribute("rel", "noopener noreferrer");
expect(
within(create).getByTestId(
"super-admin-auth-provider-setup-link-external-icon",
),
).toBeInTheDocument();
expect(
within(links).getByTestId("super-admin-auth-provider-setup-link-list"),
).toHaveAttribute("href", "https://github.com/settings/developers");
});

it("re-points the setup links when the authorization URL is edited", async () => {
const user = userEvent.setup();
const form = await renderEditPage(GITHUB_PROVIDER);

await user.type(
within(form).getByTestId("super-admin-auth-provider-authorization-url"),
"https://ghe.example.com/login/oauth/authorize",
);

expect(
within(form).getByTestId("super-admin-auth-provider-setup-link-create"),
).toHaveAttribute(
"href",
"https://ghe.example.com/settings/applications/new",
);
});

it("shows no setup links for generic enterprise SSO", async () => {
const form = await renderEditPage(NEW_OAUTH_PROVIDER);

expect(
within(form).queryByTestId("super-admin-auth-provider-setup-links"),
).not.toBeInTheDocument();
});

it("only says the current secret will be kept when one is configured", async () => {
const form = await renderEditPage(NEW_OAUTH_PROVIDER);
expect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { OAuthProviderBrand } from "#/api/auth-providers-service/auth-providers.types";
import { I18nKey } from "#/i18n/declaration";

export type ProviderSetupLinkKind = "create" | "list" | "admin" | "docs";

export interface ProviderSetupLink {
kind: ProviderSetupLinkKind;
url: string;
/** Name of the external service the link opens, shown in its label. */
service: string;
}

export const PROVIDER_SETUP_LINK_LABELS: Record<
ProviderSetupLinkKind,
I18nKey
> = {
create: I18nKey.SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_CREATE,
list: I18nKey.SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_LIST,
admin: I18nKey.SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_ADMIN,
docs: I18nKey.SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_DOCS,
};

const DEFAULT_ORIGINS: Partial<Record<OAuthProviderBrand, string>> = {
github: "https://github.com",
gitlab: "https://gitlab.com",
};

// Presets seed hosts like YOUR_BITBUCKET_HOST / PLACEHOLDER.COM until the
// admin fills them in; a link to those would only 404.
const PLACEHOLDER_HOST = /^(your[_-]|placeholder)/i;

/** The provider's web origin, taken from the first usable URL the admin has
* entered (so GitHub Enterprise Server / self-managed GitLab links follow
* their own host), else the brand's public default. */
function providerOrigin(
brand: OAuthProviderBrand,
urls: (string | null)[],
): string | null {
for (const url of urls) {
if (url) {
try {
const { origin, hostname } = new URL(url);
if (!PLACEHOLDER_HOST.test(hostname)) {
return origin;
}
} catch {
// Not a URL yet -- try the next one.
}
}
}
return DEFAULT_ORIGINS[brand] ?? null;
}

const BITBUCKET_CLOUD_DOCS =
"https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/";
const BITBUCKET_DC_DOCS =
"https://confluence.atlassian.com/bitbucketserver/configure-an-incoming-link-1108483657.html";
const ENTRA_APP_REGISTRATIONS =
"https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
const ENTRA_REGISTER_APP_DOCS =
"https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app";

/** Links into the external provider's UI where the OAuth app this row needs
* (client ID + secret + callback URL) is created or listed. Empty when there
* is nothing useful to link to. `urls` are the row's endpoint URLs that share
* the provider's web host (authorization, token) -- not userinfo, which is on
* `api.github.com` for github.com. */
export function getProviderSetupLinks(
brand: OAuthProviderBrand,
urls: (string | null)[],
): ProviderSetupLink[] {
const origin = providerOrigin(brand, urls);

switch (brand) {
case "github":
return origin
? [
{
kind: "create",
url: `${origin}/settings/applications/new`,
service: "GitHub",
},
{
kind: "list",
url: `${origin}/settings/developers`,
service: "GitHub",
},
]
: [];
case "gitlab":
return origin
? [
{
kind: "list",
url: `${origin}/-/user_settings/applications`,
service: "GitLab",
},
{
kind: "admin",
url: `${origin}/admin/applications`,
service: "GitLab",
},
]
: [];
case "forgejo":
return origin
? [
{
kind: "list",
url: `${origin}/user/settings/applications`,
service: "Forgejo",
},
]
: [];
case "bitbucket":
return [
{ kind: "docs", url: BITBUCKET_CLOUD_DOCS, service: "Atlassian" },
];
case "bitbucket_data_center":
return [{ kind: "docs", url: BITBUCKET_DC_DOCS, service: "Atlassian" }];
case "azure_devops":
return [
{
kind: "list",
url: ENTRA_APP_REGISTRATIONS,
service: "Microsoft Entra",
},
{
kind: "docs",
url: ENTRA_REGISTER_APP_DOCS,
service: "Microsoft Learn",
},
];
default:
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import {
buildUpdatePayload,
toFormState,
} from "./auth-provider-form";
import LinkExternalIcon from "#/icons/link-external.svg?react";
import {
PROVIDER_SETUP_LINK_LABELS,
getProviderSetupLinks,
} from "./auth-provider-setup-links";

/** A read-only URL field with a Copy button -- used for the per-row URLs
* (OAuth redirect URL, SAML SP metadata URL) that only exist once a row has
Expand Down Expand Up @@ -128,6 +133,13 @@ function AuthProviderForm({ provider }: AuthProviderFormProps) {
initial.kind === KIND_OAUTH
? `${window.location.origin}/oauth/${providerId}/callback`
: null;
const setupLinks =
form.kind === KIND_OAUTH
? getProviderSetupLinks(form.providerType, [
form.authorizationUrl,
form.tokenUrl,
])
: [];
const spMetadataUrl =
initial.kind === KIND_SAML
? `${window.location.origin}/saml/${providerId}/metadata`
Expand Down Expand Up @@ -344,6 +356,39 @@ function AuthProviderForm({ provider }: AuthProviderFormProps) {
copyTestId="super-admin-auth-provider-redirect-url-copy"
/>
) : null}
{setupLinks.length > 0 ? (
<div
data-testid="super-admin-auth-provider-setup-links"
className="flex flex-col gap-1 text-sm text-neutral-400"
>
<p>{t(I18nKey.SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINKS_HINT)}</p>
<ul className="flex flex-wrap gap-x-4 gap-y-1">
{setupLinks.map((link) => (
<li key={link.url}>
<a
data-testid={`super-admin-auth-provider-setup-link-${link.kind}`}
href={link.url}
target="_blank"
rel="noopener noreferrer"
title={link.url}
className="inline-flex items-center gap-1 underline underline-offset-2"
>
{t(PROVIDER_SETUP_LINK_LABELS[link.kind], {
service: link.service,
})}
<LinkExternalIcon
data-testid="super-admin-auth-provider-setup-link-external-icon"
width={12}
height={12}
color="currentColor"
aria-hidden="true"
/>
</a>
</li>
))}
</ul>
</div>
) : null}
<SettingsInput
testId="super-admin-auth-provider-client-id"
label={t(I18nKey.SUPER_ADMIN$AUTH_PROVIDER_CLIENT_ID)}
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/i18n/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,11 @@ export enum I18nKey {
SUPER_ADMIN$AUTH_PROVIDER_NAMED_SECRET_HINT = "SUPER_ADMIN$AUTH_PROVIDER_NAMED_SECRET_HINT",
SUPER_ADMIN$AUTH_PROVIDER_REDIRECT_URL = "SUPER_ADMIN$AUTH_PROVIDER_REDIRECT_URL",
SUPER_ADMIN$AUTH_PROVIDER_REDIRECT_URL_HINT = "SUPER_ADMIN$AUTH_PROVIDER_REDIRECT_URL_HINT",
SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINKS_HINT = "SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINKS_HINT",
SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_CREATE = "SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_CREATE",
SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_LIST = "SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_LIST",
SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_ADMIN = "SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_ADMIN",
SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_DOCS = "SUPER_ADMIN$AUTH_PROVIDER_SETUP_LINK_DOCS",
SUPER_ADMIN$AUTH_PROVIDER_SAML_FIELDS_OPTIONAL_HINT = "SUPER_ADMIN$AUTH_PROVIDER_SAML_FIELDS_OPTIONAL_HINT",
SUPER_ADMIN$AUTH_PROVIDER_METADATA_URL = "SUPER_ADMIN$AUTH_PROVIDER_METADATA_URL",
SUPER_ADMIN$AUTH_PROVIDER_METADATA_URL_HINT = "SUPER_ADMIN$AUTH_PROVIDER_METADATA_URL_HINT",
Expand Down
Loading
Loading