Skip to content

Commit 4c1b5f1

Browse files
committed
Simplify generics in authenticateRequest
1 parent 0444571 commit 4c1b5f1

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

apps/webapp/app/services/apiAuth.server.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ type AuthenticationMethod = "personalAccessToken" | "organizationAccessToken" |
334334
type AllowedAuthenticationMethods = Record<AuthenticationMethod, boolean> &
335335
({ personalAccessToken: true } | { organizationAccessToken: true } | { apiKey: true });
336336

337+
const defaultAllowedAuthenticationMethods: AllowedAuthenticationMethods = {
338+
personalAccessToken: true,
339+
organizationAccessToken: true,
340+
apiKey: true,
341+
};
342+
337343
type FilteredAuthenticationResult<
338344
T extends AllowedAuthenticationMethods = AllowedAuthenticationMethods
339345
> =
@@ -350,7 +356,8 @@ type FilteredAuthenticationResult<
350356
*
351357
* Supports personal access tokens, organization access tokens, and API keys.
352358
* Returns the appropriate authentication result based on the token type found.
353-
* The return type is conditionally filtered to only include authentication methods that are enabled.
359+
*
360+
* This method currently only allows private keys for the `apiKey` authentication method.
354361
*
355362
* @template T - The allowed authentication methods configuration type
356363
* @param request - The incoming HTTP request containing authentication headers
@@ -373,18 +380,16 @@ export async function authenticateRequest<
373380
T extends AllowedAuthenticationMethods = AllowedAuthenticationMethods
374381
>(
375382
request: Request,
376-
allowedAuthenticationMethods: T = {
377-
personalAccessToken: true,
378-
organizationAccessToken: true,
379-
apiKey: true,
380-
} satisfies AllowedAuthenticationMethods as T
383+
allowedAuthenticationMethods?: T
381384
): Promise<FilteredAuthenticationResult<T> | undefined> {
385+
const allowedMethods = allowedAuthenticationMethods ?? defaultAllowedAuthenticationMethods;
386+
382387
const { apiKey, branchName } = getApiKeyFromRequest(request);
383388
if (!apiKey) {
384389
return;
385390
}
386391

387-
if (allowedAuthenticationMethods.personalAccessToken && isPersonalAccessToken(apiKey)) {
392+
if (allowedMethods.personalAccessToken && isPersonalAccessToken(apiKey)) {
388393
const result = await authenticateApiRequestWithPersonalAccessToken(request);
389394

390395
if (!result) {
@@ -400,7 +405,7 @@ export async function authenticateRequest<
400405
> as FilteredAuthenticationResult<T>;
401406
}
402407

403-
if (allowedAuthenticationMethods.organizationAccessToken && isOrganizationAccessToken(apiKey)) {
408+
if (allowedMethods.organizationAccessToken && isOrganizationAccessToken(apiKey)) {
404409
const result = await authenticateApiRequestWithOrganizationAccessToken(request);
405410

406411
if (!result) {
@@ -416,7 +421,7 @@ export async function authenticateRequest<
416421
> as FilteredAuthenticationResult<T>;
417422
}
418423

419-
if (allowedAuthenticationMethods.apiKey) {
424+
if (allowedMethods.apiKey) {
420425
const result = await authenticateApiKey(apiKey, { allowPublicKey: false, branchName });
421426

422427
if (!result) {

0 commit comments

Comments
 (0)