Skip to content

Commit 8655147

Browse files
committed
chore: optional serviceApiKey
1 parent 6378f37 commit 8655147

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

.changeset/good-swans-bet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/service-utils": patch
3+
---
4+
5+
fix: make service api key optional to allow services to pass through auth

packages/service-utils/src/core/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export type CoreServiceConfig = {
2020
// if EXPLICITLY set to null, service will not be checked for authorization
2121
// this is meant for services that are not possible to be turned off by users, such as "social" and "analytics"
2222
serviceScope: ServiceName | null;
23-
serviceApiKey: string;
23+
// Optional. Some services pass through user-provided authentication (e.g. analytics) and should not have any authed access on their own.
24+
serviceApiKey?: string;
2425
serviceAction?: string;
2526
useWalletAuth?: boolean;
2627
/**

packages/service-utils/src/core/get-auth-headers.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,14 @@ describe("getAuthHeaders", () => {
111111
Authorization: "Bearer test-jwt",
112112
});
113113
});
114+
115+
it("should return empty headers if no auth method and no serviceApiKey is provided", () => {
116+
const headers = getAuthHeaders(defaultAuthData);
117+
expect(headers).toEqual({});
118+
});
119+
120+
it("should return empty headers if serviceApiKey is undefined and no other auth method is provided", () => {
121+
const headers = getAuthHeaders(defaultAuthData, undefined);
122+
expect(headers).toEqual({});
123+
});
114124
});

packages/service-utils/src/core/get-auth-headers.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,40 @@ import type { AuthorizationInput } from "./authorize/index.js";
99
*/
1010
export function getAuthHeaders(
1111
authData: AuthorizationInput,
12-
serviceApiKey: string,
13-
): Record<string, string> {
12+
serviceApiKey?: string,
13+
): HeadersInit {
1414
const { teamId, clientId, jwt, secretKey, incomingServiceApiKey } = authData;
1515

1616
switch (true) {
1717
// 1. if we have a secret key, we'll use it
1818
case !!secretKey:
1919
return {
2020
"x-secret-key": secretKey,
21-
} as Record<string, string>;
21+
};
2222

2323
// 2. if we have a JWT AND either a teamId or clientId, we'll use the JWT for auth
2424
case !!(jwt && (teamId || clientId)):
2525
return {
2626
Authorization: `Bearer ${jwt}`,
27-
} as Record<string, string>;
27+
};
2828

2929
// 3. if we have an incoming service api key, we'll use it
3030
case !!incomingServiceApiKey: {
3131
return {
3232
"x-service-api-key": incomingServiceApiKey,
33-
} as Record<string, string>;
33+
};
3434
}
3535

36-
// 4. if nothing else is present, we'll use the service api key
37-
default: {
36+
// 4. if we have a service api key provided by the service, use it
37+
case !!serviceApiKey: {
3838
return {
3939
"x-service-api-key": serviceApiKey,
4040
};
4141
}
42+
43+
// 5. otherwise leave auth headers empty
44+
default: {
45+
return {};
46+
}
4247
}
4348
}

0 commit comments

Comments
 (0)