class_docstr: A base class for [`APIFlask`][apiflask.app.APIFlask] and
  [`APIBlueprint`][apiflask.blueprint.APIBlueprint].
  
  This class contains the route shortcut decorators (i.e. `get`, `post`, etc.) and
  API-related decorators (i.e. `auth_required`, `input`, `output`, `doc`).
  
  *Version added: 1.0*
docstr: Protect a view with provided authentication settings.
  
  > Be sure to put it under the routes decorators (i.e., `app.route`, `app.get`,
  `app.post`, etc.).
  
  Examples:
  
  ```python
  from apiflask import APIFlask, HTTPTokenAuth
  
  app = APIFlask(__name__)
  auth = HTTPTokenAuth()
  
  @app.get('/')
  @app.auth_required(auth)
  def hello():
      return 'Hello'!
  ```
  
  Arguments:
      auth: The `auth` object, an instance of
          [`HTTPBasicAuth`][apiflask.security.HTTPBasicAuth]
          or [`HTTPTokenAuth`][apiflask.security.HTTPTokenAuth].
      roles: The selected roles to allow to visit this view, accepts a list of role names.
          See [Flask-HTTPAuth's documentation][_role]{target:_blank} for more details.
          [_role]: https://flask-httpauth.readthedocs.io/en/latest/#user-roles
      optional: Set to `True` to allow the view to execute even the authentication
          information is not included with the request, in which case the attribute
          `auth.current_user` will be `None`.
  
  *Version changed: 2.0.0*
  
  - Remove the deprecated `role` parameter.
  
  *Version changed: 1.0.0*
  
  - The `role` parameter is deprecated.
  
  *Version changed: 0.12.0*
  
  - Move to `APIFlask` and `APIBlueprint` classes.
  
  *Version changed: 0.4.0*
  
  - Add parameter `roles`.
docstr_tok: ["Protect","a","view","with","provided","authentication","settings",".",">","Be","sure","to","put","it","under","the","routes","decorators","(","i.e.",",","`","app.route","`",",","`","app.get","`",",","`","app.post","`",",","etc.",")",".","Examples",":","``","`","python","from","apiflask","import","APIFlask",",","HTTPTokenAuth","app","=","APIFlask","(","__name__",")","auth","=","HTTPTokenAuth","(",")","@","app.get","(","'\/","'",")","@","app.auth_required","(","auth",")","def","hello","(",")",":","return","'Hello","'","!","``","`","Arguments",":","auth",":","The","`","auth","`","object",",","an","instance","of","[","`","HTTPBasicAuth","`","]","[","apiflask.security.HTTPBasicAuth","]","or","[","`","HTTPTokenAuth","`","]","[","apiflask.security.HTTPTokenAuth","]",".","roles",":","The","selected","roles","to","allow","to","visit","this","view",",","accepts","a","list","of","role","names",".","See","[","Flask-HTTPAuth","'s","documentation","]","[","_role","]","{","target",":","_blank","}","for","more","details",".","[","_role","]",":","https",":","\/\/flask-httpauth.readthedocs.io\/en\/latest\/","#","user-roles","optional",":","Set","to","`","True","`","to","allow","the","view","to","execute","even","the","authentication","information","is","not","included","with","the","request",",","in","which","case","the","attribute","`","auth.current_user","`","will","be","`","None","`",".","*","Version","changed",":","2.0.0","*","-","Remove","the","deprecated","`","role","`","parameter",".","*","Version","changed",":","1.0.0","*","-","The","`","role","`","parameter","is","deprecated",".","*","Version","changed",":","0.12.0","*","-","Move","to","`","APIFlask","`","and","`","APIBlueprint","`","classes",".","*","Version","changed",":","0.4.0","*","-","Add","parameter","`","roles","`","."]
code: def auth_required(
      self,
      auth: HTTPAuthType,
      roles: t.Optional[list] = None,
      optional: t.Optional[str] = None,
  ) -> t.Callable[[DecoratedType], DecoratedType]:
      """Protect a view with provided authentication settings.
  
      > Be sure to put it under the routes decorators (i.e., `app.route`, `app.get`,
      `app.post`, etc.).
  
      Examples:
  
      ```python
      from apiflask import APIFlask, HTTPTokenAuth
  
      app = APIFlask(__name__)
      auth = HTTPTokenAuth()
  
      @app.get('/')
      @app.auth_required(auth)
      def hello():
          return 'Hello'!
      ```
  
      Arguments:
          auth: The `auth` object, an instance of
              [`HTTPBasicAuth`][apiflask.security.HTTPBasicAuth]
              or [`HTTPTokenAuth`][apiflask.security.HTTPTokenAuth].
          roles: The selected roles to allow to visit this view, accepts a list of role names.
              See [Flask-HTTPAuth's documentation][_role]{target:_blank} for more details.
              [_role]: https://flask-httpauth.readthedocs.io/en/latest/#user-roles
          optional: Set to `True` to allow the view to execute even the authentication
              information is not included with the request, in which case the attribute
              `auth.current_user` will be `None`.
  
      *Version changed: 2.0.0*
  
      - Remove the deprecated `role` parameter.
  
      *Version changed: 1.0.0*
  
      - The `role` parameter is deprecated.
  
      *Version changed: 0.12.0*
  
      - Move to `APIFlask` and `APIBlueprint` classes.
  
      *Version changed: 0.4.0*
  
      - Add parameter `roles`.
      """
  
      def decorator(f):
          f = _ensure_sync(f)
          _annotate(f, auth=auth, roles=roles or [])
          return auth.login_required(role=roles, optional=optional)(f)
  
      return decorator
  
imports: import typing
  from collections.abc import Mapping
  from functools import wraps
  from flask import current_app
  from flask import jsonify
  from flask import Response
  from marshmallow import ValidationError
  from webargs.flaskparser import FlaskParser
  from webargs.multidictproxy import MultiDictProxy
  from .exceptions import _ValidationError
  from .helpers import _sentinel
  from .schemas import EmptySchema
  from .schemas import Schema
  from .types import DecoratedType
  from .types import DictSchemaType
  from .types import HTTPAuthType
  from .types import OpenAPISchemaType
  from .types import RequestType
  from .types import ResponseReturnValueType
  from .types import ResponsesType
  from .types import SchemaType
  from .views import MethodView
label_desc: Use image node_id 7 for calling the APIScaffold obj's underlying member method code with example usage: obj.auth_required(auth, roles, optional) and returns: decorator, auth