<commit_before>==============
Authentication
==============

Starting from version 0.5, django-modern-rpc supports authentication. It is possible to restrict access to any
RPC method depending on conditions names "predicate".

Basics
======

To provide authentication features, django-modern-rpc introduces the concept of "predicate". It is a python function
taking a request as argument and returning a boolean:

.. code:: python

    def forbid_bots_access(request):
        forbidden_bots = [
            'Googlebot',  # Google
            'Bingbot',  # Microsoft
            'Slurp',  # Yahoo
            'DuckDuckBot',  # DuckDuckGo
            'Baiduspider',  # Baidu
            'YandexBot',  # Yandex
            'facebot',  # Facebook
        ]
        incoming_UA = request.meta.get('HTTP_USER_AGENT')
        if not incoming_UA:
            return False

        for bot_ua in forbidden_bots:
            # If we detect the caller is one of the bots listed above...
            if bot_ua.lower() in incoming_UA.lower():
                # ... forbid access
                return False

        # In all other cases, allow access
        return True

It is associated with RPC method using ``@set_authentication_predicate`` decorator.

.. code:: python

    from modernrpc.core import rpc_method
    from modernrpc.auth import set_authentication_predicate
    from myproject.myapp.auth import forbid_bots_access

    @rpc_method<commit_msg>Fixed typo in docs example<commit_after>==============
Authentication
==============

Starting from version 0.5, django-modern-rpc supports authentication. It is possible to restrict access to any
RPC method depending on conditions names "predicate".

Basics
======

To provide authentication features, django-modern-rpc introduces the concept of "predicate". It is a python function
taking a request as argument and returning a boolean:

.. code:: python

    def forbid_bots_access(request):
        forbidden_bots = [
            'Googlebot',  # Google
            'Bingbot',  # Microsoft
            'Slurp',  # Yahoo
            'DuckDuckBot',  # DuckDuckGo
            'Baiduspider',  # Baidu
            'YandexBot',  # Yandex
            'facebot',  # Facebook
        ]
        incoming_UA = request.META.get('HTTP_USER_AGENT')
        if not incoming_UA:
            return False

        for bot_ua in forbidden_bots:
            # If we detect the caller is one of the bots listed above...
            if bot_ua.lower() in incoming_UA.lower():
                # ... forbid access
                return False

        # In all other cases, allow access
        return True

It is associated with RPC method using ``@set_authentication_predicate`` decorator.

.. code:: python

    from modernrpc.core import rpc_method
    from modernrpc.auth import set_authentication_predicate
    from myproject.myapp.auth import forbid_bots_access

    @rpc_method
{'fraction_non_alphanumeric': 0.08994164092001374, 'fraction_numerical': 0.0013731548232063166, 'mean_word_length': 2.432273262661955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6069084', 'n_tokens_mistral': 812, 'n_tokens_neox': 768, 'n_words': 265}