Setu-TS
    Preparing search index...

    Function requirePolicy

    • Guard that requires a policy ability. Returns 501 when no authorization policy service is registered, 401 when the ability denies an anonymous request, 403 when it denies a signed-in principal; otherwise calls next(). On every refusal the handler and later middleware do not run.

      The policy is evaluated BY NAME through the registered IAuthorizationPolicyService, so a replacement provider serves the guard. AuthPlugin refuses to start when a route's guard names a policy or ability that is not registered — or whose registered ability disagrees with this object on anonymous, which is how a different policy registered under the same name is caught. Not scanned: a route added after start(), and a guard added as global middleware; an unknown name there rejects per request (fail closed).

      An anonymous request to an ability that needs a principal is refused 401 BEFORE the extractor runs, so an unauthenticated caller can neither cost a record lookup nor learn from the extractor whether a record exists. A target extractor's throw propagates unchanged, so errorHandler answers a database outage 503 rather than a misleading 403; the handler never runs.

      Type Parameters

      • A extends string
      • T

      Parameters

      • policy: PolicyDefinition<A, T>

        The policy object (from definePolicy)

      • ability: A

        One of the policy's ability names

      • Optionaltarget: PolicyTarget<T>

        A fixed target, or an extractor called per request

      Returns MiddlewareFunction

      Middleware function, branded for OpenAPI deriveSecurity and for the startup scan

      When the ability is not one of the policy object's own abilities

      app.router.patch('/posts/:id', {
      middleware: [requirePolicy(postPolicy, 'update', (ctx) => posts.find(ctx.params.id))],
      handler,
      });

      0.9.0