Skip to content

Commit 9425e9e

Browse files
committed
Winston/don't use iframe for jwt or custom auth endpoint validation (#5229)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR consolidates authentication methods in the `thirdweb` package by introducing a common endpoint for custom JWT and custom auth, enhancing code organization and reducing redundancy. ### Detailed summary - Changed `getSessionHeaders` to no longer export it. - Introduced `ecosystem` parameter in various functions. - Replaced direct `fetch` calls with `getClientFetch` for consistency. - Simplified the `loginWithAuthToken` method to accept an optional `recoveryCode`. - Unified handling of authentication strategies in `InAppWebConnector`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 556e113 commit 9425e9e

File tree

7 files changed

+66
-58
lines changed

7 files changed

+66
-58
lines changed

.changeset/three-dancers-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Consolidate custom jwt and custom auth endpoint through common endpoint

packages/thirdweb/src/wallets/in-app/core/authentication/authEndpoint.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import type { ThirdwebClient } from "../../../../client/client.js";
2-
import { getSessionHeaders } from "../../native/helpers/api/fetchers.js";
2+
import { getClientFetch } from "../../../../utils/fetch.js";
33
import { ROUTE_AUTH_ENDPOINT_CALLBACK } from "../../native/helpers/constants.js";
44
import { createErrorMessage } from "../../native/helpers/errors.js";
5-
import type { ClientScopedStorage } from "./client-scoped-storage.js";
5+
import type { Ecosystem } from "../wallet/types.js";
66
import type { AuthStoredTokenWithCookieReturnType } from "./types.js";
77

88
export async function authEndpoint(args: {
99
payload: string;
1010
client: ThirdwebClient;
11-
storage: ClientScopedStorage;
11+
ecosystem?: Ecosystem;
1212
}): Promise<AuthStoredTokenWithCookieReturnType> {
13-
const resp = await fetch(ROUTE_AUTH_ENDPOINT_CALLBACK, {
13+
const clientFetch = getClientFetch(args.client, args.ecosystem);
14+
15+
const res = await clientFetch(ROUTE_AUTH_ENDPOINT_CALLBACK, {
1416
method: "POST",
1517
headers: {
16-
...getSessionHeaders(),
18+
"Content-Type": "application/json",
1719
},
1820
body: JSON.stringify({
1921
payload: args.payload,
2022
developerClientId: args.client.clientId,
2123
}),
2224
});
23-
if (!resp.ok) {
24-
const error = await resp.json();
25+
26+
if (!res.ok) {
27+
const error = await res.json();
2528
throw new Error(
2629
`Custom auth endpoint authentication error: ${error.message}`,
2730
);
2831
}
2932

3033
try {
31-
const { verifiedToken } = await resp.json();
34+
const { verifiedToken } = await res.json();
3235

3336
return { storedToken: verifiedToken };
3437
} catch (e) {

packages/thirdweb/src/wallets/in-app/core/authentication/guest.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,22 @@ export async function guestAuthenticate(args: {
2929
}
3030

3131
const clientFetch = getClientFetch(args.client, args.ecosystem);
32-
const authResult = await (async () => {
33-
const path = getLoginCallbackUrl({
34-
authOption: "guest",
35-
client: args.client,
36-
ecosystem: args.ecosystem,
37-
});
38-
const res = await clientFetch(`${path}`, {
39-
method: "POST",
40-
headers: {
41-
"Content-Type": "application/json",
42-
},
43-
body: JSON.stringify({
44-
sessionId,
45-
}),
46-
});
32+
const path = getLoginCallbackUrl({
33+
authOption: "guest",
34+
client: args.client,
35+
ecosystem: args.ecosystem,
36+
});
37+
const res = await clientFetch(`${path}`, {
38+
method: "POST",
39+
headers: {
40+
"Content-Type": "application/json",
41+
},
42+
body: JSON.stringify({
43+
sessionId,
44+
}),
45+
});
4746

48-
if (!res.ok) throw new Error("Failed to generate guest account");
47+
if (!res.ok) throw new Error("Failed to generate guest account");
4948

50-
return (await res.json()) satisfies AuthStoredTokenWithCookieReturnType;
51-
})();
52-
return authResult;
49+
return (await res.json()) satisfies AuthStoredTokenWithCookieReturnType;
5350
}

packages/thirdweb/src/wallets/in-app/core/authentication/jwt.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import type { ThirdwebClient } from "../../../../client/client.js";
2-
import { getSessionHeaders } from "../../native/helpers/api/fetchers.js";
2+
import { getClientFetch } from "../../../../utils/fetch.js";
33
import { ROUTE_AUTH_JWT_CALLBACK } from "../../native/helpers/constants.js";
44
import { createErrorMessage } from "../../native/helpers/errors.js";
5-
import type { ClientScopedStorage } from "./client-scoped-storage.js";
5+
import type { Ecosystem } from "../wallet/types.js";
66
import type { AuthStoredTokenWithCookieReturnType } from "./types.js";
77

88
export async function customJwt(args: {
99
jwt: string;
1010
client: ThirdwebClient;
11-
storage: ClientScopedStorage;
11+
ecosystem?: Ecosystem;
1212
}): Promise<AuthStoredTokenWithCookieReturnType> {
13-
const resp = await fetch(ROUTE_AUTH_JWT_CALLBACK, {
13+
const clientFetch = getClientFetch(args.client, args.ecosystem);
14+
15+
const res = await clientFetch(ROUTE_AUTH_JWT_CALLBACK, {
1416
method: "POST",
1517
headers: {
16-
...getSessionHeaders(),
18+
"Content-Type": "application/json",
1719
},
1820
body: JSON.stringify({
1921
jwt: args.jwt,
2022
developerClientId: args.client.clientId,
2123
}),
2224
});
2325

24-
if (!resp.ok) {
25-
const error = await resp.json();
26+
if (!res.ok) {
27+
const error = await res.json();
2628
throw new Error(`JWT authentication error: ${error.message}`);
2729
}
2830

2931
try {
30-
const { verifiedToken } = await resp.json();
32+
const { verifiedToken } = await res.json();
33+
3134
return { storedToken: verifiedToken };
3235
} catch (e) {
3336
throw new Error(

packages/thirdweb/src/wallets/in-app/native/helpers/api/fetchers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const ECOSYSTEM_PARTNER_ID_HEADER = "x-ecosystem-partner-id";
1919

2020
let sessionNonce: Hex | undefined = undefined;
2121

22-
export function getSessionHeaders() {
22+
function getSessionHeaders() {
2323
if (!sessionNonce) {
2424
sessionNonce = randomBytesHex(16);
2525
}

packages/thirdweb/src/wallets/in-app/native/native-connector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ export class InAppNativeConnector implements InAppConnector {
199199
return customJwt({
200200
jwt: params.jwt,
201201
client: this.client,
202-
storage: this.storage,
202+
ecosystem: this.ecosystem,
203203
});
204204
case "auth_endpoint":
205205
return authEndpoint({
206206
payload: params.payload,
207207
client: this.client,
208-
storage: this.storage,
208+
ecosystem: this.ecosystem,
209209
});
210210
default:
211211
throw new Error(`Unsupported authentication type: ${strategy}`);

packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { webLocalStorage } from "../../../../utils/storage/webStorage.js";
44
import type { SocialAuthOption } from "../../../../wallets/types.js";
55
import type { Account } from "../../../interfaces/wallet.js";
66
import { getUserStatus } from "../../core/actions/get-enclave-user-status.js";
7+
import { authEndpoint } from "../../core/authentication/authEndpoint.js";
78
import { ClientScopedStorage } from "../../core/authentication/client-scoped-storage.js";
89
import { guestAuthenticate } from "../../core/authentication/guest.js";
10+
import { customJwt } from "../../core/authentication/jwt.js";
911
import {
1012
getLinkedProfilesInternal,
1113
linkAccount,
@@ -268,8 +270,11 @@ export class InAppWebConnector implements InAppConnector {
268270
});
269271
}
270272

271-
async loginWithAuthToken(authResult: AuthStoredTokenWithCookieReturnType) {
272-
return this.auth.loginWithAuthToken(authResult);
273+
async loginWithAuthToken(
274+
authResult: AuthStoredTokenWithCookieReturnType,
275+
recoveryCode?: string,
276+
) {
277+
return this.auth.loginWithAuthToken(authResult, recoveryCode);
273278
}
274279

275280
/**
@@ -292,20 +297,22 @@ export class InAppWebConnector implements InAppConnector {
292297
client: this.client,
293298
ecosystem: this.ecosystem,
294299
});
300+
case "auth_endpoint": {
301+
return authEndpoint({
302+
payload: args.payload,
303+
client: this.client,
304+
ecosystem: this.ecosystem,
305+
});
306+
}
295307
case "jwt":
296-
return this.auth.authenticateWithCustomJwt({
308+
return customJwt({
297309
jwt: args.jwt,
298-
encryptionKey: args.encryptionKey,
310+
client: this.client,
311+
ecosystem: this.ecosystem,
299312
});
300313
case "passkey": {
301314
return this.passkeyAuth(args);
302315
}
303-
case "auth_endpoint": {
304-
return this.auth.authenticateWithCustomAuthEndpoint({
305-
payload: args.payload,
306-
encryptionKey: args.encryptionKey,
307-
});
308-
}
309316
case "iframe_email_verification": {
310317
return this.auth.authenticateWithIframe({
311318
email: args.email,
@@ -359,17 +366,10 @@ export class InAppWebConnector implements InAppConnector {
359366
): Promise<AuthLoginReturnType> {
360367
const strategy = args.strategy;
361368
switch (strategy) {
369+
case "auth_endpoint":
362370
case "jwt": {
363-
return this.auth.loginWithCustomJwt({
364-
jwt: args.jwt,
365-
encryptionKey: args.encryptionKey,
366-
});
367-
}
368-
case "auth_endpoint": {
369-
return this.auth.loginWithCustomAuthEndpoint({
370-
payload: args.payload,
371-
encryptionKey: args.encryptionKey,
372-
});
371+
const authToken = await this.authenticate(args);
372+
return await this.loginWithAuthToken(authToken, args.encryptionKey);
373373
}
374374
case "iframe_email_verification": {
375375
return this.auth.loginWithIframe({

0 commit comments

Comments
 (0)