Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-llamas-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/service-utils": patch
---

allow passing `null` to service scope, do not validate domains/bundleids when using secretKey auth method
9 changes: 6 additions & 3 deletions packages/service-utils/src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ export type PolicyResult = {

export type CoreServiceConfig = {
apiUrl: string;
serviceScope: ServiceName;
// if EXPLICITLY set to null, service will not be checked for authorization
// this is meant for services that are not possible to be turned off by users, such as "social" and "analytics"
serviceScope: ServiceName | null;
serviceApiKey: string;
serviceAction?: string;
useWalletAuth?: boolean;
includeUsage?: boolean;
};

export type TeamAndProjectResponse = {
authMethod: "secretKey" | "publishableKey" | "jwt" | "teamId";
team: TeamResponse;
project?: ProjectResponse | null;
};
Expand All @@ -42,11 +45,11 @@ export type TeamResponse = {
name: string;
slug: string;
image: string | null;
billingPlan: string;
billingPlan: "free" | "starter" | "growth" | "pro";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirming if you want pro or custom

createdAt: Date;
updatedAt: Date | null;
billingEmail: string | null;
billingStatus: string | null;
billingStatus: "noPayment" | "validPayment" | "invalidPayment" | null;
growthTrialEligible: boolean | null;
enabledScopes: ServiceName[];
};
Expand Down
9 changes: 8 additions & 1 deletion packages/service-utils/src/core/authorize/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ export function authorizeClient(
teamAndProjectResponse: TeamAndProjectResponse,
): AuthorizationResult {
const { origin, bundleId } = authOptions;
const { team, project } = teamAndProjectResponse;
const { team, project, authMethod } = teamAndProjectResponse;

const authResult: AuthorizationResult = {
authorized: true,
team,
project,
authMethod,
};

// if there's no project, we'll return the authResult (JWT or teamId auth)
if (!project) {
return authResult;
}

if (authMethod === "secretKey") {
// if the auth was done using secretKey, we do not want to enforce domains or bundleIds
return authResult;
}

// check for public restrictions
if (project.domains.includes("*")) {
return authResult;
Expand Down
1 change: 1 addition & 0 deletions packages/service-utils/src/core/authorize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ export async function authorize(
authorized: true,
team: teamAndProjectResponse.team,
project: teamAndProjectResponse.project,
authMethod: clientAuth.authMethod,
};
}
13 changes: 12 additions & 1 deletion packages/service-utils/src/core/authorize/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ export function authorizeService(
teamAndProjectResponse: TeamAndProjectResponse,
serviceConfig: CoreServiceConfig,
): AuthorizationResult {
const { team, project } = teamAndProjectResponse;
const { team, project, authMethod } = teamAndProjectResponse;

if (serviceConfig.serviceScope === null) {
// if explicitly set to null, we do not want to check for service level authorization
return {
authorized: true,
team,
authMethod,
};
}

if (!team.enabledScopes.includes(serviceConfig.serviceScope)) {
return {
Expand All @@ -21,6 +30,7 @@ export function authorizeService(
return {
authorized: true,
team,
authMethod,
};
}

Expand Down Expand Up @@ -57,5 +67,6 @@ export function authorizeService(
authorized: true,
team,
project,
authMethod,
};
}
3 changes: 2 additions & 1 deletion packages/service-utils/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ export const validTeamResponse: TeamResponse = {
updatedAt: new Date("2024-06-01"),
billingPlan: "free",
billingEmail: "[email protected]",
billingStatus: "noCustomer",
billingStatus: "noPayment",
growthTrialEligible: false,
enabledScopes: ["storage", "rpc", "bundler"],
};

export const validTeamAndProjectResponse: TeamAndProjectResponse = {
team: validTeamResponse,
project: validProjectResponse,
authMethod: "publishableKey",
};

export const validServiceConfig: CoreServiceConfig = {
Expand Down
Loading