|
1 | 1 | import { |
2 | 2 | type CoreServiceConfig, |
3 | 3 | type TeamAndProjectResponse, |
| 4 | + type TeamResponse, |
4 | 5 | fetchTeamAndProject, |
5 | 6 | } from "../api.js"; |
6 | 7 | import { authorizeClient } from "./client.js"; |
@@ -129,6 +130,21 @@ export async function authorize( |
129 | 130 | errorCode: "INVALID_KEY", |
130 | 131 | }; |
131 | 132 | } |
| 133 | + // check if the service is maybe disabled for the team (usually due to a billing issue / exceeding the free plan limit) |
| 134 | + if ( |
| 135 | + !isServiceEnabledForTeam( |
| 136 | + serviceConfig.serviceScope, |
| 137 | + teamAndProjectResponse.team.capabilities, |
| 138 | + ) |
| 139 | + ) { |
| 140 | + return { |
| 141 | + authorized: false, |
| 142 | + status: 403, |
| 143 | + errorMessage: |
| 144 | + "You currently do not have access to this service. Please check if your subscription includes this service and is active.", |
| 145 | + errorCode: "SERVICE_TEMPORARILY_DISABLED", |
| 146 | + }; |
| 147 | + } |
132 | 148 | // now we can validate the key itself |
133 | 149 | const clientAuth = authorizeClient(authData, teamAndProjectResponse); |
134 | 150 |
|
@@ -161,3 +177,26 @@ export async function authorize( |
161 | 177 | authMethod: clientAuth.authMethod, |
162 | 178 | }; |
163 | 179 | } |
| 180 | + |
| 181 | +function isServiceEnabledForTeam( |
| 182 | + scope: CoreServiceConfig["serviceScope"], |
| 183 | + teamCapabilities: TeamResponse["capabilities"], |
| 184 | +): boolean { |
| 185 | + switch (scope) { |
| 186 | + case "rpc": |
| 187 | + return teamCapabilities.rpc.enabled; |
| 188 | + case "bundler": |
| 189 | + return teamCapabilities.bundler.enabled; |
| 190 | + case "storage": |
| 191 | + return teamCapabilities.storage.enabled; |
| 192 | + case "insight": |
| 193 | + return teamCapabilities.insight.enabled; |
| 194 | + case "nebula": |
| 195 | + return teamCapabilities.nebula.enabled; |
| 196 | + case "embeddedWallets": |
| 197 | + return teamCapabilities.embeddedWallets.enabled; |
| 198 | + default: |
| 199 | + // always return true for any legacy / un-named services |
| 200 | + return true; |
| 201 | + } |
| 202 | +} |
0 commit comments