LCOV - code coverage report
Current view: top level - src/platform/auth - authRejection.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 29.2 % 24 7
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { AuthTypeNotAllowedError } from '@comfyorg/ingest-types'
       2              : import {
       3              :   zAuthTypeNotAllowedError,
       4              :   zErrorResponse
       5              : } from '@comfyorg/ingest-types/zod'
       6              : 
       7            1 : export const AUTH_REJECTION_STATUSES = [401, 403] as const
       8              : export type AuthRejectionStatus = (typeof AUTH_REJECTION_STATUSES)[number]
       9              : 
      10            1 : export function isAuthRejectionStatus(
      11              :   status: number
      12              : ): status is AuthRejectionStatus {
      13            1 :   return AUTH_REJECTION_STATUSES.some((rejection) => rejection === status)
      14              : }
      15              : 
      16            1 : const OTHER = 'other'
      17              : type Other = typeof OTHER
      18              : 
      19              : /**
      20              :  * Backend values are reported only when they are on these lists, and anything
      21              :  * else is `other`: the backend text is uncontrolled and can carry user,
      22              :  * workspace or key identifiers, and an open value space would make the tags
      23              :  * unbounded in cardinality. The generated ingest types describe these codes in
      24              :  * prose only, so this is the one place they are listed.
      25              :  */
      26            1 : export const KNOWN_AUTH_REFUSAL_CODES = [
      27              :   'UNAUTHORIZED',
      28              :   'FORBIDDEN',
      29              :   'csrf_invalid',
      30              :   'workspace_access_denied',
      31              :   'workspace_id_invalid',
      32              :   'origin_not_allowed',
      33              :   'cross_site_request'
      34              : ] as const
      35            1 : export const KNOWN_ACCEPTED_AUTH_METHODS = ['bearer_jwt', 'x_api_key'] as const
      36              : 
      37              : /** Message phrases for the plain `{ error: string }` shape, which has no code. */
      38            1 : const REASON_PHRASES = [
      39              :   {
      40              :     phrase: 'authentication method not allowed',
      41              :     reason: 'auth_method_not_allowed'
      42              :   }
      43              : ] as const
      44              : 
      45              : export const MAX_REASON_TEXT_LENGTH = 500
      46              : export const MAX_ACCEPTED_METHODS = 8
      47              : 
      48              : type AuthRefusalCode = (typeof KNOWN_AUTH_REFUSAL_CODES)[number] | Other
      49              : type AcceptedAuthMethod = (typeof KNOWN_ACCEPTED_AUTH_METHODS)[number] | Other
      50              : type AuthRejectionReason = (typeof REASON_PHRASES)[number]['reason'] | Other
      51              : 
      52              : interface BackendFields {
      53              :   reason: AuthRejectionReason
      54              :   errorType: AuthTypeNotAllowedError['error']['type']
      55              :   errorCode: AuthRefusalCode
      56              : }
      57              : 
      58              : /** Tags describing the backend's refusal: `backendReason`, `backendErrorType`, ... */
      59              : export type AuthRejectionTags = {
      60              :   [K in keyof BackendFields as `backend${Capitalize<K>}`]?: BackendFields[K]
      61              : } & { acceptedMethods?: string }
      62              : 
      63            0 : function isOneOf<T extends string>(
      64              :   known: readonly T[],
      65              :   value: string
      66              : ): value is T {
      67            0 :   return known.some((entry) => entry === value)
      68              : }
      69              : 
      70            0 : function reasonOf(text: string): AuthRejectionReason | undefined {
      71            0 :   const bounded = text.slice(0, MAX_REASON_TEXT_LENGTH).toLowerCase()
      72            0 :   if (bounded.trim().length === 0) return undefined
      73            0 :   return (
      74              :     REASON_PHRASES.find(({ phrase }) => bounded.includes(phrase))?.reason ??
      75              :     OTHER
      76              :   )
      77              : }
      78              : 
      79            0 : function codeOf(code: string): AuthRefusalCode {
      80            0 :   return isOneOf(KNOWN_AUTH_REFUSAL_CODES, code) ? code : OTHER
      81              : }
      82              : 
      83            0 : function acceptedMethodsOf(accepted: readonly string[]): string | undefined {
      84            0 :   const methods = accepted
      85              :     .slice(0, MAX_ACCEPTED_METHODS)
      86              :     .map((method): AcceptedAuthMethod =>
      87              :       isOneOf(KNOWN_ACCEPTED_AUTH_METHODS, method) ? method : OTHER
      88              :     )
      89            0 :   return methods.length > 0 ? [...new Set(methods)].join(',') : undefined
      90              : }
      91              : 
      92              : /**
      93              :  * The tags that tell an auth refusal's causes apart, taken from the ingest
      94              :  * error shapes (`AuthTypeNotAllowedError`, `ErrorResponse`) or, for a body in
      95              :  * neither, from the already-extracted message. Pure and total: any input
      96              :  * yields a (possibly empty) tag set, and no value is copied from the response.
      97              :  */
      98            0 : export function authRejectionTags(
      99              :   body: unknown,
     100              :   message: string
     101              : ): AuthRejectionTags {
     102            0 :   const notAllowed = zAuthTypeNotAllowedError.safeParse(body).data
     103            0 :   const refusal = zErrorResponse.safeParse(body).data
     104            0 :   const reason = reasonOf(refusal?.message ?? message)
     105            0 :   const acceptedMethods = notAllowed && acceptedMethodsOf(notAllowed.accepted)
     106            0 :   return {
     107              :     ...(reason && { backendReason: reason }),
     108              :     ...(notAllowed && { backendErrorType: notAllowed.error.type }),
     109              :     ...(refusal && { backendErrorCode: codeOf(refusal.code) }),
     110              :     ...(acceptedMethods && { acceptedMethods })
     111              :   }
     112              : }
        

Generated by: LCOV version 2.0-1