Skip to content
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

Fix prompt behaviour #58

Merged
merged 5 commits into from
Jan 7, 2025
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
1 change: 1 addition & 0 deletions lib/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("index exports", () => {
const expectedExports = [
// types
"IssuerRouteTypes",
"PromptTypes",
"Scopes",
"StorageKeys",

Expand Down
16 changes: 11 additions & 5 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export enum Scopes {
offline_access = "offline",
}

export enum PromptTypes {
none = "none",
create = "create",
login = "login",
}

export type LoginMethodParams = Pick<
LoginOptions,
| "audience"
Expand Down Expand Up @@ -72,12 +78,12 @@ export type LoginOptions = {
* Prompt to use for the login
*
* This can be one of the following:
* - login
* - create
* - none
* - login (force user re-authentication)
* - create (show registration screen)
* - none (silently authenticate user without prompting for action)
*
*/
prompt: string;
prompt?: PromptTypes;
/**
* Redirect URL to use for the login
*/
Expand All @@ -95,7 +101,7 @@ export type LoginOptions = {
* - email
* - profile
* - openid
* - offline_access
* - offline
*/
scope?: Scopes[];
/**
Expand Down
46 changes: 36 additions & 10 deletions lib/utils/generateAuthUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { IssuerRouteTypes, LoginOptions, Scopes } from "../types";
import { IssuerRouteTypes, LoginOptions, PromptTypes, Scopes } from "../types";
import { generateAuthUrl } from "./generateAuthUrl";
import { MemoryStorage, StorageKeys } from "../sessionManager";
import { setActiveStorage } from "./token";
Expand All @@ -16,11 +16,10 @@ describe("generateAuthUrl", () => {
connectionId: "conn123",
redirectURL: "https://example.com",
audience: "audience123",
prompt: "login",
state: "state123",
};
const expectedUrl =
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&start_page=login&login_hint=user%40example.com&is_create_org=true&connection_id=conn123&redirect_uri=https%3A%2F%2Fexample.com&audience=audience123&scope=openid+profile&prompt=login&state=state123&code_challenge_method=S256";
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&login_hint=user%40example.com&is_create_org=true&connection_id=conn123&redirect_uri=https%3A%2F%2Fexample.com&audience=audience123&scope=openid+profile&state=state123&code_challenge_method=S256";

const result = await generateAuthUrl(
domain,
Expand All @@ -37,6 +36,33 @@ describe("generateAuthUrl", () => {
expect(result.url.toString()).toBe(expectedUrl);
});

it("should generate the register URL when type is 'registration'", async () => {
const domain = "https://auth.example.com";
const options: LoginOptions = {
clientId: "client123",
responseType: "code",
scope: [Scopes.openid, Scopes.profile],
state: "state123",
codeChallenge: "challenge123",
codeChallengeMethod: "S256",
redirectURL: "https://example2.com",
};
const expectedUrl =
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile&state=state123&code_challenge=challenge123&code_challenge_method=S256&prompt=create";

const result = await generateAuthUrl(
domain,
IssuerRouteTypes.register,
options,
);
const nonce = result.url.searchParams.get("nonce");
expect(nonce).not.toBeNull();
expect(nonce!.length).toBe(16);
result.url.searchParams.delete("nonce");

expect(result.url.toString()).toBe(expectedUrl);
});

it("should include optional parameters if provided", async () => {
const domain = "https://auth.example.com";
const options: LoginOptions = {
Expand All @@ -47,10 +73,10 @@ describe("generateAuthUrl", () => {
codeChallenge: "challenge123",
codeChallengeMethod: "S256",
redirectURL: "https://example2.com",
prompt: "create",
prompt: PromptTypes.login,
};
const expectedUrl =
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&start_page=login&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile&prompt=create&state=state123&code_challenge=challenge123&code_challenge_method=S256";
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile&prompt=login&state=state123&code_challenge=challenge123&code_challenge_method=S256";

const result = await generateAuthUrl(
domain,
Expand All @@ -71,11 +97,11 @@ describe("generateAuthUrl", () => {
clientId: "client123",
scope: [Scopes.openid, Scopes.profile, Scopes.offline_access],
redirectURL: "https://example2.com",
prompt: "create",
prompt: PromptTypes.create,
state: "state123",
};
const expectedUrl =
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&start_page=login&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&state=state123&code_challenge_method=S256";
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&state=state123&code_challenge_method=S256";

const result = await generateAuthUrl(
domain,
Expand All @@ -100,10 +126,10 @@ describe("generateAuthUrl", () => {
clientId: "client123",
scope: [Scopes.openid, Scopes.profile, Scopes.offline_access],
redirectURL: "https://example2.com",
prompt: "create",
prompt: PromptTypes.create,
};
const expectedUrl =
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&start_page=login&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&code_challenge_method=S256";
"https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&code_challenge_method=S256";

const result = await generateAuthUrl(
domain,
Expand Down Expand Up @@ -138,7 +164,7 @@ describe("generateAuthUrl", () => {
connectionId: "conn123",
redirectURL: "https://example.com",
audience: "audience123",
prompt: "login",
prompt: PromptTypes.login,
};

await generateAuthUrl(domain, IssuerRouteTypes.login, options);
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/generateAuthUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { base64UrlEncode, getInsecureStorage, StorageKeys } from "../main";
import { IssuerRouteTypes, LoginOptions } from "../types";
import { IssuerRouteTypes, LoginOptions, PromptTypes } from "../types";
import { generateRandomString } from "./generateRandomString";
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";

Expand All @@ -24,7 +24,6 @@ export const generateAuthUrl = async (
const searchParams: Record<string, string> = {
client_id: options.clientId,
response_type: options.responseType || "code",
start_page: type,
...mapLoginMethodParamsForUrl(options),
};

Expand Down Expand Up @@ -59,6 +58,10 @@ export const generateAuthUrl = async (
searchParams["code_challenge_method"] = options.codeChallengeMethod;
}

if (!options.prompt && type === IssuerRouteTypes.register) {
searchParams["prompt"] = PromptTypes.create;
}

authUrl.search = new URLSearchParams(searchParams).toString();
return {
url: authUrl,
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/mapLoginMethodParamsForUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// mapLoginMethodParamsForUrl.test.ts
import { describe, expect, it } from "vitest";
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";
import { LoginMethodParams, Scopes } from "../types";
import { LoginMethodParams, PromptTypes, Scopes } from "../types";

describe("mapLoginMethodParamsForUrl", () => {
it("should map login method params to URL parameters", () => {
Expand All @@ -12,7 +12,7 @@ describe("mapLoginMethodParamsForUrl", () => {
redirectURL: "https://example.com",
audience: "audience123",
scope: [Scopes.openid, Scopes.profile],
prompt: "login",
prompt: PromptTypes.login,
lang: "en",
orgCode: "org123",
orgName: "Example Org",
Expand Down
Loading