Skip to content

Commit cfb4506

Browse files
committed
allow passing null to service scope, do not check domains on secretKey auth
1 parent 90a16da commit cfb4506

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ export type PolicyResult = {
1616

1717
export type CoreServiceConfig = {
1818
apiUrl: string;
19-
serviceScope: ServiceName;
19+
// if EXPLICTLY set to null, service will not be checked for authorization
20+
// this is meant for services that are not possible to be turned off by users, such as "social" and "analytics"
21+
serviceScope: ServiceName | null;
2022
serviceApiKey: string;
2123
serviceAction?: string;
2224
useWalletAuth?: boolean;
2325
includeUsage?: boolean;
2426
};
2527

2628
export type TeamAndProjectResponse = {
29+
authMethod: "secretKey" | "publishableKey" | "jwt" | "teamId";
2730
team: TeamResponse;
2831
project?: ProjectResponse | null;
2932
};
@@ -42,11 +45,11 @@ export type TeamResponse = {
4245
name: string;
4346
slug: string;
4447
image: string | null;
45-
billingPlan: string;
48+
billingPlan: "free" | "starter" | "growth" | "pro";
4649
createdAt: Date;
4750
updatedAt: Date | null;
4851
billingEmail: string | null;
49-
billingStatus: string | null;
52+
billingStatus: "noPayment" | "validPayment" | "invalidPayment" | null;
5053
growthTrialEligible: boolean | null;
5154
enabledScopes: ServiceName[];
5255
};

packages/service-utils/src/core/authorize/client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ export function authorizeClient(
1212
teamAndProjectResponse: TeamAndProjectResponse,
1313
): AuthorizationResult {
1414
const { origin, bundleId } = authOptions;
15-
const { team, project } = teamAndProjectResponse;
15+
const { team, project, authMethod } = teamAndProjectResponse;
1616

1717
const authResult: AuthorizationResult = {
1818
authorized: true,
1919
team,
2020
project,
21+
authMethod,
2122
};
2223

24+
// if there's no project, we'll return the authResult (JWT or teamId auth)
2325
if (!project) {
2426
return authResult;
2527
}
2628

29+
if (authMethod === "secretKey") {
30+
// if the auth was done using secreKey, we do not want to enforce domains or bundleIds
31+
return authResult;
32+
}
33+
2734
// check for public restrictions
2835
if (project.domains.includes("*")) {
2936
return authResult;

packages/service-utils/src/core/authorize/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,6 @@ export async function authorize(
148148
authorized: true,
149149
team: teamAndProjectResponse.team,
150150
project: teamAndProjectResponse.project,
151+
authMethod: clientAuth.authMethod,
151152
};
152153
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ export function authorizeService(
55
teamAndProjectResponse: TeamAndProjectResponse,
66
serviceConfig: CoreServiceConfig,
77
): AuthorizationResult {
8-
const { team, project } = teamAndProjectResponse;
8+
const { team, project, authMethod } = teamAndProjectResponse;
9+
10+
if (serviceConfig.serviceScope === null) {
11+
// if explicitly set to null, we do not want to check for service level authorization
12+
return {
13+
authorized: true,
14+
team,
15+
authMethod,
16+
};
17+
}
918

1019
if (!team.enabledScopes.includes(serviceConfig.serviceScope)) {
1120
return {
@@ -21,6 +30,7 @@ export function authorizeService(
2130
return {
2231
authorized: true,
2332
team,
33+
authMethod,
2434
};
2535
}
2636

@@ -57,5 +67,6 @@ export function authorizeService(
5767
authorized: true,
5868
team,
5969
project,
70+
authMethod,
6071
};
6172
}

packages/service-utils/src/mocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ export const validTeamResponse: TeamResponse = {
4343
updatedAt: new Date("2024-06-01"),
4444
billingPlan: "free",
4545
billingEmail: "test@example.com",
46-
billingStatus: "noCustomer",
46+
billingStatus: "noPayment",
4747
growthTrialEligible: false,
4848
enabledScopes: ["storage", "rpc", "bundler"],
4949
};
5050

5151
export const validTeamAndProjectResponse: TeamAndProjectResponse = {
5252
team: validTeamResponse,
5353
project: validProjectResponse,
54+
authMethod: "publishableKey",
5455
};
5556

5657
export const validServiceConfig: CoreServiceConfig = {

0 commit comments

Comments
 (0)