import type { FieldNullability, InputFieldMap, InputShapeFromFields, MaybePromise, Merge, SchemaTypes, ShapeFromTypeParam, TypeParam, UnionToIntersection, } from '@pothos/core'; import type { GraphQLResolveInfo } from 'graphql'; import type RequestCache from './request-cache'; export interface ScopeAuthPluginOptions { unauthorizedError?: UnauthorizedForTypeErrorFn; cacheKey?: (value: unknown) => unknown; runScopesOnType?: boolean; treatErrorsAsUnauthorized?: boolean; authorizeOnSubscribe?: boolean; defaultStrategy?: Types['DefaultAuthStrategy']; authScopes: ScopeAuthInitializer; } export interface BuiltInScopes { $all?: true extends true ? AuthScopeMap : never; $any?: true extends true ? AuthScopeMap : never; $granted?: string; } export type AuthScopeMap = Merge< BuiltInScopes & Partial >; export type ScopeLoaderMap = { [K in keyof Types['AuthScopes']]: | boolean | ((param: Types['AuthScopes'][K]) => MaybePromise); }; export type ScopeAuthInitializer = ( context: Types['Context'], ) => MaybePromise>; export type TypeAuthScopesFunction = ( parent: Parent, context: Types['Context'], ) => MaybePromise | boolean>; export type TypeAuthScopes = | AuthScopeMap | TypeAuthScopesFunction; export type FieldAuthScopes = | AuthScopeMap | (( parent: Parent, args: Args, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise | boolean>); export type TypeGrantScopes = ( parent: Parent, context: Types['Context'], ) => MaybePromise; export type FieldGrantScopes = | string[] | (( parent: Parent, args: Args, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise); export enum AuthScopeFailureType { AuthScope = 'AuthScope', AuthScopeFunction = 'AuthScopeFunction', GrantedScope = 'GrantedScope', AnyAuthScopes = 'AnyAuthScopes', AllAuthScopes = 'AllAuthScopes', Unknown = 'Unknown', } export interface AuthScopeFailure { kind: AuthScopeFailureType.AuthScope; scope: string; parameter: unknown; error: Error | null; } export interface AuthScopeFunctionFailure { kind: AuthScopeFailureType.AuthScopeFunction; error: Error | null; } export interface UnknownAuthFailure { kind: AuthScopeFailureType.Unknown; } export interface AnyAuthScopesFailure { kind: AuthScopeFailureType.AnyAuthScopes; failures: AuthFailure[]; } export interface AllAuthScopesFailure { kind: AuthScopeFailureType.AllAuthScopes; failures: AuthFailure[]; } export interface GrantedScopeFailure { kind: AuthScopeFailureType.GrantedScope; scope: string; } export type AuthFailure = | AllAuthScopesFailure | AnyAuthScopesFailure | AuthScopeFailure | AuthScopeFunctionFailure | GrantedScopeFailure | UnknownAuthFailure; export interface ForbiddenResult { message: string; failure: AuthFailure; } export interface ResolveStep { run: ( cache: RequestCache, parent: unknown, args: Record, context: {}, info: GraphQLResolveInfo, setResolved: (val: unknown) => void, ) => MaybePromise; errorMessage: | string | (( parent: unknown, args: Record, context: {}, info: GraphQLResolveInfo, ) => string); } export type ContextForAuth< Types extends SchemaTypes, Scopes, > = 'any' extends Types['DefaultAuthStrategy'] ? ContextForAuthUnion : UnionToIntersection>; type ContextForAuthUnion = Scopes extends ( // biome-ignore lint/suspicious/noExplicitAny: this is fine ...args: any[] ) => infer R ? ContextForAuthUnion : Scopes extends boolean ? Types['Context'] : keyof Scopes extends infer Scope ? Scope extends keyof Types['AuthContexts'] ? Types['AuthContexts'][Scope] : Scope extends '$any' ? ContextForAuthUnion : Scope extends '$all' ? UnionToIntersection> : Types['Context'] : never; export type UnauthorizedResolver< Types extends SchemaTypes, ParentShape, Type extends TypeParam, Nullable extends FieldNullability, Args extends InputFieldMap, > = ( parent: ParentShape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, error: Error, ) => MaybePromise>; export type UnauthorizedErrorFn< Types extends SchemaTypes, ParentShape, Args extends InputFieldMap, > = ( parent: ParentShape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, result: ForbiddenResult, ) => Error | string; export type UnauthorizedForTypeErrorFn = ( parent: ParentShape, context: Types['Context'], info: GraphQLResolveInfo, result: ForbiddenResult, ) => Error | string; export interface UnauthorizedOptions< Types extends SchemaTypes, ParentShape, Type extends TypeParam, Nullable extends FieldNullability, Args extends InputFieldMap, > { unauthorizedError?: UnauthorizedErrorFn; unauthorizedResolver?: UnauthorizedResolver; } export type ReplaceContext = Omit< Types, 'Context' > & { Context: Context; };