B
    `7                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZmZ d dlmZ d dlmZ G d	d
 d
ZeeG dd dZdS )    N)implementer)action_method)LegacySessionCSRFStoragePolicy)ConfigurationError)PHASE1_CONFIGPHASE2_CONFIGIAuthenticationPolicyIAuthorizationPolicyICSRFStoragePolicyIDefaultCSRFOptionsIDefaultPermissionISecurityPolicy)LegacySecurityPolicy)as_sorted_tuplec               @   sf   e Zd Zdd Zedd Zedd Zedd Zed	d
 Zdd Z	edddZ
edd ZdS )SecurityConfiguratorMixinc             C   s   |  t  d S )N)set_csrf_storage_policyr   )self r   _/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/config/security.pyadd_default_security   s    z.SecurityConfiguratorMixin.add_default_securityc                sN    fdd}   dd d} |d< jt|t|fd dS )a  Override the :app:`Pyramid` :term:`security policy` in the current
        configuration.  The ``policy`` argument must be an instance
        of a security policy or a :term:`dotted Python name`
        that points at an instance of a security policy.

        .. note::

           Using the ``security_policy`` argument to the
           :class:`pyramid.config.Configurator` constructor can be used to
           achieve the same purpose.

        c                  s   j  t d S )N)registryregisterUtilityr   r   )policyr   r   r   register(   s    z?SecurityConfiguratorMixin.set_security_policy.<locals>.registerzsecurity policyNr   )orderintrospectables)maybe_dottedintrospectableobject_descriptionactionr   r   )r   r   r   intrr   )r   r   r   set_security_policy   s    
z-SecurityConfiguratorMixin.set_security_policyc                s^   t jdtdd  fdd}  dd d} |d< jt|t|fd	 dS )
a  
        .. deprecated:: 2.0

            Authentication policies have been replaced by security policies.
            See :ref:`upgrading_auth_20` for more information.

        Override the :app:`Pyramid` :term:`authentication policy` in the
        current configuration.  The ``policy`` argument must be an instance
        of an authentication policy or a :term:`dotted Python name`
        that points at an instance of an authentication policy.

        .. note::

           Using the ``authentication_policy`` argument to the
           :class:`pyramid.config.Configurator` constructor can be used to
           achieve the same purpose.

        zAuthentication and authorization policies have been deprecated in favor of security policies.  See "Upgrading Authentication/Authorization" in "What's New in Pyramid 2.0" of the documentation for more information.   )
stacklevelc                 sV   j  t j td kr&tdj td k	r>tdt } j | t d S )NzCannot configure an authentication policy without also configuring an authorization policy (use the set_authorization_policy method)z]Cannot configure an authentication and authorizationpolicy with a configured security policy.)r   r   r   queryUtilityr	   r   r   r   )Zsecurity_policy)r   r   r   r   r   W   s    zESecurityConfiguratorMixin.set_authentication_policy.<locals>.registerzauthentication policyNr   )r   r   )	warningswarnDeprecationWarningr   r   r   r   r   r   )r   r   r   r    r   )r   r   r   set_authentication_policy:   s"    
z3SecurityConfiguratorMixin.set_authentication_policyc                sv   t jdtdd  fdd}fdd}  dd	 d} |d
< jt|t|fd d	| d	S )a  
        .. deprecated:: 2.0

            Authentication policies have been replaced by security policies.
            See :ref:`upgrading_auth_20` for more information.

        Override the :app:`Pyramid` :term:`authorization policy` in the
        current configuration.  The ``policy`` argument must be an instance
        of an authorization policy or a :term:`dotted Python name` that points
        at an instance of an authorization policy.

        .. note::

           Using the ``authorization_policy`` argument to the
           :class:`pyramid.config.Configurator` constructor can be used to
           achieve the same purpose.

        zAuthentication and authorization policies have been deprecated in favor of security policies.  See "Upgrading Authentication/Authorization" in "What's New in Pyramid 2.0" of the documentation for more information.r"   )r#   c                  s   j  t d S )N)r   r   r	   r   )r   r   r   r   r      s    zDSecurityConfiguratorMixin.set_authorization_policy.<locals>.registerc                  s&    j r
d S  jtd kr"tdd S )NzCannot configure an authorization policy without also configuring an authentication policy (use the set_authorization_policy method))Z
autocommitr   r$   r   r   r   )r   r   r   ensure   s
    zBSecurityConfiguratorMixin.set_authorization_policy.<locals>.ensurezauthorization policyNr   )r   r   )	r%   r&   r'   r   r   r   r   r	   r   )r   r   r   r)   r    r   )r   r   r   set_authorization_policyw   s&    


z2SecurityConfiguratorMixin.set_authorization_policyc                sX    fdd} dd d} |d<  d  d} |d< jt|t||fd dS )	a  
        Set the default permission to be used by all subsequent
        :term:`view configuration` registrations.  ``permission``
        should be a :term:`permission` string to be used as the
        default permission.  An example of a permission
        string:``'view'``.  Adding a default permission makes it
        unnecessary to protect each view configuration with an
        explicit permission, unless your application policy requires
        some exception for a particular view.

        If a default permission is *not* set, views represented by
        view configuration registrations which do not explicitly
        declare a permission will be executable by entirely anonymous
        users (any authorization policy is ignored).

        Later calls to this method override will conflict with earlier calls;
        there can be only one default permission active at a time within an
        application.

        .. warning::

          If a default permission is in effect, view configurations meant to
          create a truly anonymously accessible view (even :term:`exception
          view` views) *must* use the value of the permission importable as
          :data:`pyramid.security.NO_PERMISSION_REQUIRED`.  When this string
          is used as the ``permission`` for a view configuration, the default
          permission is ignored, and the view is registered, making it
          available to all callers regardless of their credentials.

        .. seealso::

            See also :ref:`setting_a_default_permission`.

        .. note::

           Using the ``default_permission`` argument to the
           :class:`pyramid.config.Configurator` constructor can be used to
           achieve the same purpose.
        c                  s   j  t d S )N)r   r   r   r   )
permissionr   r   r   r      s    zBSecurityConfiguratorMixin.set_default_permission.<locals>.registerzdefault permissionNvaluepermissionsr+   )r   r   )r   r   r   r   )r   r+   r   r    Z	perm_intrr   )r+   r   r   set_default_permission   s    *z0SecurityConfiguratorMixin.set_default_permissionc             C   s,   |  d||d}||d< | jd|fd dS )a  
        A configurator directive which registers a free-standing
        permission without associating it with a view callable.  This can be
        used so that the permission shows up in the introspectable data under
        the ``permissions`` category (permissions mentioned via ``add_view``
        already end up in there).  For example::

          config = Configurator()
          config.add_permission('view')
        r-   r+   r,   N)r   )r   r   )r   Zpermission_namer    r   r   r   add_permission   s    z(SecurityConfiguratorMixin.add_permissionT
csrf_tokenX-CSRF-TokenGETHEADOPTIONSTRACEFNc       
   	      s   t |||||||d  fdd}dd d}	||	d< ||	d< ||	d< t||	d	< ||	d
< ||	d< ||	d< jt|t|	fd dS )a  
        Set the default CSRF options used by subsequent view registrations.

        ``require_csrf`` controls whether CSRF checks will be automatically
        enabled on each view in the application. This value is used as the
        fallback when ``require_csrf`` is left at the default of ``None`` on
        :meth:`pyramid.config.Configurator.add_view`.

        ``token`` is the name of the CSRF token used in the body of the
        request, accessed via ``request.POST[token]``. Default: ``csrf_token``.

        ``header`` is the name of the header containing the CSRF token,
        accessed via ``request.headers[header]``. Default: ``X-CSRF-Token``.

        If ``token`` or ``header`` are set to ``None`` they will not be used
        for checking CSRF tokens.

        ``safe_methods`` is an iterable of HTTP methods which are expected to
        not contain side-effects as defined by RFC2616. Safe methods will
        never be automatically checked for CSRF tokens.
        Default: ``('GET', 'HEAD', 'OPTIONS', TRACE')``.

        ``check_origin`` is a boolean. If ``False``, the ``Origin`` and
        ``Referer`` headers will not be validated as part of automated
        CSRF checks.

        ``allow_no_origin`` is a boolean.  If ``True``, a request lacking both
        an ``Origin`` and ``Referer`` header will pass the CSRF check. This
        option has no effect if ``check_origin`` is ``False``.

        If ``callback`` is set, it must be a callable accepting ``(request)``
        and returning ``True`` if the request should be checked for a valid
        CSRF token. This callback allows an application to support
        alternate authentication methods that do not rely on cookies which
        are not subject to CSRF attacks. For example, if a request is
        authenticated using the ``Authorization`` header instead of a cookie,
        this may return ``False`` for that request so that clients do not
        need to send the ``X-CSRF-Token`` header. The callback is only tested
        for non-safe methods as defined by ``safe_methods``.

        .. versionadded:: 1.7

        .. versionchanged:: 1.8
           Added the ``callback`` option.

        .. versionchanged:: 2.0
           Added the ``allow_no_origin`` and ``check_origin`` options.

        )require_csrftokenheadersafe_methodscheck_originallow_no_origincallbackc                  s   j  t d S )N)r   r   r   r   )optionsr   r   r   r   G  s    zDSecurityConfiguratorMixin.set_default_csrf_options.<locals>.registerzdefault csrf view optionsNr7   r8   r9   r:   r;   r<   r=   )r   r   )DefaultCSRFOptionsr   r   r   r   r   )
r   r7   r8   r9   r:   r;   r<   r=   r   r    r   )r>   r   r   set_default_csrf_options  s4    <z2SecurityConfiguratorMixin.set_default_csrf_optionsc                s<    fdd} dd d} |d< jt||fd dS )a  
        Set the :term:`CSRF storage policy` used by subsequent view
        registrations.

        ``policy`` is a class that implements the
        :meth:`pyramid.interfaces.ICSRFStoragePolicy` interface and defines
        how to generate and persist CSRF tokens.

        c                  s   j  t d S )N)r   r   r
   r   )r   r   r   r   r   k  s    zCSecurityConfiguratorMixin.set_csrf_storage_policy.<locals>.registerzcsrf storage policyNr   )r   )r   r   r
   )r   r   r   r    r   )r   r   r   r   _  s
    z1SecurityConfiguratorMixin.set_csrf_storage_policy)Tr0   r1   r2   TFN)__name__
__module____qualname__r   r   r!   r(   r*   r.   r/   r@   r   r   r   r   r   r      s   !=<=      Ur   c               @   s   e Zd Zdd ZdS )r?   c             C   s2   || _ || _|| _t|| _|| _|| _|| _d S )N)r7   r8   r9   	frozensetr:   r;   r<   r=   )r   r7   r8   r9   r:   r;   r<   r=   r   r   r   __init__w  s    

zDefaultCSRFOptions.__init__N)rA   rB   rC   rE   r   r   r   r   r?   u  s   r?   )r%   Zzope.interfacer   Zpyramid.config.actionsr   Zpyramid.csrfr   Zpyramid.exceptionsr   Zpyramid.interfacesr   r   r   r	   r
   r   r   r   Zpyramid.securityr   Zpyramid.utilr   r   r?   r   r   r   r   <module>   s   (
  b