Skip to content

Commit 014734f

Browse files
committed
feat: update tests
1 parent 1b3a667 commit 014734f

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

lib/main.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("index exports", () => {
2929
const expectedExports = [
3030
// types
3131
"IssuerRouteTypes",
32+
"PromptTypes",
3233
"Scopes",
3334
"StorageKeys",
3435

lib/types.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ export enum Scopes {
55
offline_access = "offline",
66
}
77

8+
export enum PromptTypes {
9+
none = "none",
10+
create = "create",
11+
login = "login",
12+
}
13+
814
export type LoginMethodParams = Pick<
915
LoginOptions,
1016
| "audience"
@@ -72,12 +78,12 @@ export type LoginOptions = {
7278
* Prompt to use for the login
7379
*
7480
* This can be one of the following:
75-
* - login
76-
* - create
77-
* - none
81+
* - login (force user re-authentication)
82+
* - create (show registration screen)
83+
* - none (silently authenticate user without prompting for action)
7884
*
7985
*/
80-
prompt: string;
86+
prompt?: PromptTypes;
8187
/**
8288
* Redirect URL to use for the login
8389
*/
@@ -95,7 +101,7 @@ export type LoginOptions = {
95101
* - email
96102
* - profile
97103
* - openid
98-
* - offline_access
104+
* - offline
99105
*/
100106
scope?: Scopes[];
101107
/**

lib/utils/generateAuthUrl.test.ts

+36-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ describe("generateAuthUrl", () => {
1616
connectionId: "conn123",
1717
redirectURL: "https://example.com",
1818
audience: "audience123",
19-
prompt: "login",
2019
state: "state123",
2120
};
2221
const expectedUrl =
23-
"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";
22+
"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";
2423

2524
const result = await generateAuthUrl(
2625
domain,
2726
IssuerRouteTypes.login,
28-
options,
27+
options
2928
);
3029
const nonce = result.url.searchParams.get("nonce");
3130
expect(nonce).not.toBeNull();
@@ -37,6 +36,33 @@ describe("generateAuthUrl", () => {
3736
expect(result.url.toString()).toBe(expectedUrl);
3837
});
3938

39+
it("should generate the register URL when type is 'registration'", async () => {
40+
const domain = "https://auth.example.com";
41+
const options: LoginOptions = {
42+
clientId: "client123",
43+
responseType: "code",
44+
scope: [Scopes.openid, Scopes.profile],
45+
state: "state123",
46+
codeChallenge: "challenge123",
47+
codeChallengeMethod: "S256",
48+
redirectURL: "https://example2.com",
49+
};
50+
const expectedUrl =
51+
"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";
52+
53+
const result = await generateAuthUrl(
54+
domain,
55+
IssuerRouteTypes.register,
56+
options
57+
);
58+
const nonce = result.url.searchParams.get("nonce");
59+
expect(nonce).not.toBeNull();
60+
expect(nonce!.length).toBe(16);
61+
result.url.searchParams.delete("nonce");
62+
63+
expect(result.url.toString()).toBe(expectedUrl);
64+
});
65+
4066
it("should include optional parameters if provided", async () => {
4167
const domain = "https://auth.example.com";
4268
const options: LoginOptions = {
@@ -47,15 +73,15 @@ describe("generateAuthUrl", () => {
4773
codeChallenge: "challenge123",
4874
codeChallengeMethod: "S256",
4975
redirectURL: "https://example2.com",
50-
prompt: "create",
76+
prompt: "login",
5177
};
5278
const expectedUrl =
53-
"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";
79+
"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";
5480

5581
const result = await generateAuthUrl(
5682
domain,
5783
IssuerRouteTypes.login,
58-
options,
84+
options
5985
);
6086
const nonce = result.url.searchParams.get("nonce");
6187
expect(nonce).not.toBeNull();
@@ -75,12 +101,12 @@ describe("generateAuthUrl", () => {
75101
state: "state123",
76102
};
77103
const expectedUrl =
78-
"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";
104+
"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";
79105

80106
const result = await generateAuthUrl(
81107
domain,
82108
IssuerRouteTypes.login,
83-
options,
109+
options
84110
);
85111
const nonce = result.url.searchParams.get("nonce");
86112
expect(nonce).not.toBeNull();
@@ -103,12 +129,12 @@ describe("generateAuthUrl", () => {
103129
prompt: "create",
104130
};
105131
const expectedUrl =
106-
"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";
132+
"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";
107133

108134
const result = await generateAuthUrl(
109135
domain,
110136
IssuerRouteTypes.login,
111-
options,
137+
options
112138
);
113139
const nonce = result.url.searchParams.get("nonce");
114140
expect(nonce).not.toBeNull();

lib/utils/generateAuthUrl.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { base64UrlEncode, getInsecureStorage, StorageKeys } from "../main";
2-
import { IssuerRouteTypes, LoginOptions } from "../types";
2+
import { IssuerRouteTypes, LoginOptions, PromptTypes } from "../types";
33
import { generateRandomString } from "./generateRandomString";
44
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";
55

@@ -12,7 +12,7 @@ import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";
1212
export const generateAuthUrl = async (
1313
domain: string,
1414
type: IssuerRouteTypes = IssuerRouteTypes.login,
15-
options: LoginOptions,
15+
options: LoginOptions
1616
): Promise<{
1717
url: URL;
1818
state: string;
@@ -24,7 +24,6 @@ export const generateAuthUrl = async (
2424
const searchParams: Record<string, string> = {
2525
client_id: options.clientId,
2626
response_type: options.responseType || "code",
27-
start_page: type,
2827
...mapLoginMethodParamsForUrl(options),
2928
};
3029

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

61+
if (!options.prompt && type === IssuerRouteTypes.register) {
62+
searchParams["prompt"] = PromptTypes.create;
63+
}
64+
6265
authUrl.search = new URLSearchParams(searchParams).toString();
6366
return {
6467
url: authUrl,

0 commit comments

Comments
 (0)