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.js'; 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>; /** * Passed to type `authScopes` and `grantScopes` functions. A subset of `GraphQLResolveInfo`, so * helpers typed with it also accept a field's `info`. */ export interface TypeScopeInfo { /** * The object type being checked. For scopes defined on an interface, this is the implementing * type. */ readonly parentType: GraphQLResolveInfo['parentType']; readonly schema: GraphQLResolveInfo['schema']; } export type TypeAuthScopesFunction = ( parent: Parent, context: Types['Context'], info: TypeScopeInfo, ) => 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'], info: TypeScopeInfo, ) => 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 { key?: string; /** Whether the step reads the parent, which it then gets from `pothosScopeAuthParent`. */ readsParent?: boolean; 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 : ContextForAuthAll; type ContextForAuthAll = Scopes extends ( // biome-ignore lint/suspicious/noExplicitAny: this is fine ...args: any[] ) => infer R ? ContextForAuthAll : '$any' extends keyof Scopes ? IntersectContexts>> & ContextForAuthUnion : UnionToIntersection>; type IntersectContexts = [Contexts] extends [never] ? unknown : 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' ? // A nested `$any`'s contexts are reported as guaranteed when only one of them holds. 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; }; /** * A function a plugin sets as the `pothosScopeAuthParent` extension of an object type. It gets the * parent of the type's fields, or the value its `isTypeOf` checks, and returns the parent that * scopes, grants and unauthorized errors see. Resolvers and `isTypeOf` still get the original. */ export type ScopeAuthParentLoader = ( parent: unknown, context: Context, info: GraphQLResolveInfo, ) => MaybePromise;