MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / resolveBearerAuth

Function resolveBearerAuth

apps/cloud/src/auth/workos-auth-provider.ts:196–257  ·  view source on GitHub ↗
(
  request: Request,
  jwt: JwtBearerConfig | null = null,
)

Source from the content-addressed store, hash-verified

194 * resolved (mirrored on first read) for its name and slug.
195 */
196export const resolveBearerAuth = (
197 request: Request,
198 jwt: JwtBearerConfig | null = null,
199): Effect.Effect<
200 BearerAuth,
201 Unauthorized | NoOrganization | Unavailable | UserStoreError | WorkOSError,
202 WorkOSClient | ApiKeyService | UserStoreService
203> =>
204 Effect.gen(function* () {
205 const authHeader = request.headers.get("authorization");
206 if (!authHeader) return null;
207
208 if (!authHeader.startsWith(BEARER_PREFIX)) {
209 return yield* new Unauthorized(INVALID_AUTHORIZATION_HEADER);
210 }
211
212 const value = authHeader.slice(BEARER_PREFIX.length).trim();
213 if (!value) return yield* new Unauthorized(INVALID_API_KEY);
214
215 if (jwt && looksLikeJwt(value)) return yield* resolveJwtPrincipal(value, jwt);
216
217 const apiKeys = yield* ApiKeyService;
218 const owner = yield* apiKeys
219 .validate(value)
220 .pipe(
221 Effect.catchTag("ApiKeyValidationError", () =>
222 Effect.fail(new Unavailable(API_KEY_VALIDATION_UNAVAILABLE)),
223 ),
224 );
225
226 if (!owner) return yield* new Unauthorized(INVALID_API_KEY);
227
228 if (owner.scope === "org") {
229 const org = yield* resolveOrganization(owner.organizationId);
230 return {
231 kind: "platform",
232 organizationId: org.id,
233 organizationName: org.name,
234 ...(org.slug === undefined || org.slug === null ? {} : { organizationSlug: org.slug }),
235 keyId: owner.keyId,
236 } satisfies PlatformAuth;
237 }
238
239 // A `"user"` key always carries an accountId (see `ownerFromApiKey`); the
240 // guard keeps the narrowing honest rather than asserting.
241 if (owner.accountId == null) return yield* new Unauthorized(INVALID_API_KEY);
242
243 const org = yield* authorizeOrganization(owner.accountId, owner.organizationId);
244 if (!org) return yield* new NoOrganization(NO_ORGANIZATION_IN_API_KEY);
245
246 return {
247 kind: "member",
248 accountId: owner.accountId,
249 organizationId: org.id,
250 organizationName: org.name,
251 organizationSlug: org.slug,
252 email: "",
253 name: null,

Callers 3

authorizeTenantFunction · 0.90
resolveApiKeyPrincipalFunction · 0.85

Calls 5

resolveOrganizationFunction · 0.90
authorizeOrganizationFunction · 0.90
resolveJwtPrincipalFunction · 0.85
looksLikeJwtFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected