Skip to content

Commit 2eadf4f

Browse files
committed
Separate authToken from secretKey in ThirdwebClient
1 parent 8fb8a75 commit 2eadf4f

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

apps/dashboard/src/@/constants/thirdweb.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export function getThirdwebClient(
7979

8080
return createThirdwebClient({
8181
teamId: options?.teamId,
82-
secretKey: options?.jwt ? options.jwt : DASHBOARD_THIRDWEB_SECRET_KEY,
82+
secretKey: DASHBOARD_THIRDWEB_SECRET_KEY,
83+
authToken: options?.jwt ?? undefined,
8384
clientId: DASHBOARD_THIRDWEB_CLIENT_ID,
8485
config: {
8586
storage: {

packages/thirdweb/src/client/client.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ describe("client", () => {
2929
it("should accept a jwt being passed", () => {
3030
const client = createThirdwebClient({
3131
clientId: "foo",
32-
secretKey: "bar.baz.qux",
32+
authToken: "bar.baz.qux",
3333
});
3434
expect(client.clientId).toBe("foo");
35-
expect(client.secretKey).toBe("bar.baz.qux");
35+
expect(client.authToken).toBe("bar.baz.qux");
36+
expect(client.secretKey).toBeUndefined();
3637
});
37-
it("should throw if clientId is missing with JWT input", () => {
38+
39+
it("should throw an error if authToken is passed as secretKey", () => {
3840
expect(() =>
3941
createThirdwebClient({ secretKey: "bar.baz.qux" }),
40-
).toThrowError(/clientId must be provided when using a JWT secretKey/);
42+
).toThrowError(/have to pass authToken directly/);
43+
});
44+
45+
it("should throw if clientId is missing with JWT input", () => {
46+
expect(() =>
47+
createThirdwebClient({ authToken: "bar.baz.qux", secretKey: "foo" }),
48+
).toThrowError(/have to pass clientId when passing authToken/);
4149
});
4250
});
4351
});

packages/thirdweb/src/client/client.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ export type CreateThirdwebClientOptions = Prettify<
6262
| {
6363
clientId: string;
6464
secretKey?: string;
65+
authToken?: string;
6566
}
6667
| {
6768
clientId?: string;
6869
secretKey: string;
70+
authToken?: string;
6971
}
7072
) &
7173
ClientOptions
@@ -74,6 +76,11 @@ export type CreateThirdwebClientOptions = Prettify<
7476
export type ThirdwebClient = {
7577
readonly clientId: string;
7678
readonly secretKey: string | undefined;
79+
/**
80+
* The auth token for thirdweb dashboard usage.
81+
* @hidden
82+
*/
83+
readonly authToken: string | undefined;
7784
} & Readonly<ClientOptions>;
7885

7986
/**
@@ -109,16 +116,18 @@ export type ThirdwebClient = {
109116
export function createThirdwebClient(
110117
options: CreateThirdwebClientOptions,
111118
): ThirdwebClient {
112-
const { clientId, secretKey, ...rest } = options;
119+
const { clientId, secretKey, authToken, ...rest } = options;
113120

114121
let realClientId: string | undefined = clientId;
115122

123+
if (authToken && !clientId) {
124+
// always HAVE to also pass clientId when passing auth token
125+
throw new Error("have to pass clientId when passing authToken");
126+
}
127+
116128
if (secretKey) {
117129
if (isJWT(secretKey)) {
118-
// when passing a JWT as secret key we HAVE to also have a clientId
119-
if (!clientId) {
120-
throw new Error("clientId must be provided when using a JWT secretKey");
121-
}
130+
throw new Error("have to pass authToken directly");
122131
} else {
123132
// always PREFER the clientId if provided, only compute it from the secretKey if we don't have a clientId passed explicitly
124133
realClientId = clientId ?? computeClientIdFromSecretKey(secretKey);
@@ -132,6 +141,7 @@ export function createThirdwebClient(
132141

133142
return {
134143
...rest,
144+
authToken,
135145
clientId: realClientId,
136146
secretKey,
137147
} as const;

packages/thirdweb/src/utils/fetch.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
detectPlatform,
99
} from "./detect-platform.js";
1010
import { getServiceKey } from "./domains.js";
11-
import { isJWT } from "./jwt/is-jwt.js";
1211
import { IS_DEV } from "./process.js";
1312

1413
const DEFAULT_REQUEST_TIMEOUT = 60000;
@@ -45,39 +44,28 @@ export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
4544
if (!headers) {
4645
headers = new Headers();
4746
}
48-
// auth token if secret key === jwt
49-
const authToken =
50-
useAuthToken && client.secretKey && isJWT(client.secretKey)
51-
? client.secretKey
52-
: undefined;
53-
// secret key if secret key !== jwt
54-
const secretKey =
55-
client.secretKey && !isJWT(client.secretKey)
56-
? client.secretKey
57-
: undefined;
58-
const clientId = client.clientId;
5947

6048
// if we have an auth token set, use that (thirdweb dashboard sets this for the user)
6149
// pay urls should never send the auth token, because we always want the "developer" to be the one making the request, not the "end user"
6250
if (
63-
authToken &&
51+
client.authToken &&
6452
!isPayUrl(urlString) &&
6553
!isInAppWalletUrl(urlString) &&
6654
!isBundlerUrl(urlString)
6755
) {
68-
headers.set("authorization", `Bearer ${authToken}`);
56+
headers.set("authorization", `Bearer ${client.authToken}`);
6957
// if we have a specific teamId set, add it to the request headers
7058
if (client.teamId) {
7159
headers.set("x-team-id", client.teamId);
7260
}
7361
}
74-
75-
if (secretKey) {
76-
headers.set("x-secret-key", secretKey);
62+
// never set BOTH auth header and secret key header at the same time, auth header takes precedence
63+
else if (client.secretKey) {
64+
headers.set("x-secret-key", client.secretKey);
7765
}
7866

79-
if (clientId) {
80-
headers.set("x-client-id", clientId);
67+
if (client.clientId) {
68+
headers.set("x-client-id", client.clientId);
8169
}
8270

8371
if (ecosystem) {

0 commit comments

Comments
 (0)