Skip to content

Commit 6399e6f

Browse files
Add some more tests for pkce inferrence
1 parent 42e212c commit 6399e6f

File tree

1 file changed

+100
-9
lines changed

1 file changed

+100
-9
lines changed

test/thirdparty/oidc.test.js

+100-9
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@
1313
* under the License.
1414
*/
1515

16-
const { printPath, setupST, startSTWithMultitenancy, killAllST, cleanST, removeAppAndTenants } = require("../utils");
16+
const { printPath, setupST, startSTWithMultitenancy, killAllST, cleanST } = require("../utils");
1717
let STExpress = require("../../");
1818
let assert = require("assert");
1919
let { ProcessState } = require("../../lib/build/processState");
2020
let ThirdPartyRecipe = require("../../lib/build/recipe/thirdparty/recipe").default;
2121
let Multitenancy = require("../../lib/build/recipe/multitenancy");
22-
let Session = require("../../lib/build/recipe/session");
2322
let ThirdParty = require("../../lib/build/recipe/thirdparty");
24-
let nock = require("nock");
25-
const express = require("express");
26-
const request = require("supertest");
27-
const { default: fetch } = require("cross-fetch");
28-
let { middleware, errorHandler } = require("../../framework/express");
29-
const { configsForVerification, providers } = require("./tpConfigsForVerification");
3023

3124
describe(`oidcTest: ${printPath("[test/thirdparty/oidc.test.js]")}`, function () {
3225
beforeEach(async function () {
@@ -40,7 +33,7 @@ describe(`oidcTest: ${printPath("[test/thirdparty/oidc.test.js]")}`, function ()
4033
await cleanST();
4134
});
4235

43-
it("test the code challenge method is used if supported by the provider", async function () {
36+
it("should use code challenge method S256 if supported by the provider", async function () {
4437
const connectionURI = await startSTWithMultitenancy();
4538

4639
STExpress.init({
@@ -91,4 +84,102 @@ describe(`oidcTest: ${printPath("[test/thirdparty/oidc.test.js]")}`, function ()
9184
assert.ok(codeChallengeMethod);
9285
assert.strictEqual(codeChallengeMethod, "S256");
9386
});
87+
88+
it("should not use pkce if oidc response does not support S256", async function () {
89+
const connectionURI = await startSTWithMultitenancy();
90+
91+
STExpress.init({
92+
supertokens: {
93+
connectionURI,
94+
},
95+
appInfo: {
96+
apiDomain: "api.supertokens.io",
97+
appName: "SuperTokens",
98+
websiteDomain: "supertokens.io",
99+
},
100+
recipeList: [ThirdPartyRecipe.init()],
101+
});
102+
103+
await Multitenancy.createOrUpdateThirdPartyConfig("public", {
104+
thirdPartyId: "custom-fb",
105+
name: "Custom provider",
106+
clients: [
107+
{
108+
clientId: "...",
109+
clientSecret: "...",
110+
scope: ["profile", "email"],
111+
},
112+
],
113+
oidcDiscoveryEndpoint: "https://facebook.com/.well-known/openid-configuration",
114+
userInfoMap: {
115+
fromIdTokenPayload: {
116+
userId: "id",
117+
email: "email",
118+
emailVerified: "email_verified",
119+
},
120+
},
121+
});
122+
123+
const providerInfo = await ThirdParty.getProvider("public", "custom-fb");
124+
assert.deepStrictEqual(providerInfo.config.codeChallengeMethodsSupported, undefined);
125+
126+
const authUrl = await providerInfo.getAuthorisationRedirectURL({
127+
redirectURIOnProviderDashboard: "http://localhost:3000/callback",
128+
});
129+
assert.strictEqual(authUrl.urlWithQueryParams.includes("code_challenge"), false);
130+
assert.strictEqual(authUrl.urlWithQueryParams.includes("code_challenge_method"), false);
131+
});
132+
133+
it("should use pkce if oidc response does not support S256 but forcePKCE is true", async function () {
134+
const connectionURI = await startSTWithMultitenancy();
135+
136+
STExpress.init({
137+
supertokens: {
138+
connectionURI,
139+
},
140+
appInfo: {
141+
apiDomain: "api.supertokens.io",
142+
appName: "SuperTokens",
143+
websiteDomain: "supertokens.io",
144+
},
145+
recipeList: [ThirdPartyRecipe.init()],
146+
});
147+
148+
await Multitenancy.createOrUpdateThirdPartyConfig("public", {
149+
thirdPartyId: "custom-fb",
150+
name: "Custom provider",
151+
clients: [
152+
{
153+
clientId: "...",
154+
clientSecret: "...",
155+
scope: ["profile", "email"],
156+
forcePKCE: true,
157+
},
158+
],
159+
oidcDiscoveryEndpoint: "https://facebook.com/.well-known/openid-configuration",
160+
userInfoMap: {
161+
fromIdTokenPayload: {
162+
userId: "id",
163+
email: "email",
164+
emailVerified: "email_verified",
165+
},
166+
},
167+
});
168+
169+
const providerInfo = await ThirdParty.getProvider("public", "custom-fb");
170+
assert.deepStrictEqual(providerInfo.config.codeChallengeMethodsSupported, undefined);
171+
172+
const authUrl = await providerInfo.getAuthorisationRedirectURL({
173+
redirectURIOnProviderDashboard: "http://localhost:3000/callback",
174+
});
175+
assert.ok(authUrl.pkceCodeVerifier);
176+
const authUrlObject = new URL(authUrl.urlWithQueryParams);
177+
const codeChallengeMethod = authUrlObject.searchParams.get("code_challenge_method");
178+
const codeChallenge = authUrlObject.searchParams.get("code_challenge");
179+
180+
// Check for existence of code_challenge and code_challenge_method
181+
assert.ok(codeChallenge);
182+
assert.ok(codeChallengeMethod);
183+
assert.strictEqual(codeChallengeMethod, "S256");
184+
});
94185
});

0 commit comments

Comments
 (0)