<commit_before>"""
Auth module that contains all code needed for authentication/authorization
policies setup.

In particular:
    :includeme: Function that actually creates routes listed above and
        connects view to them
    :create_admin_user: Function that creates system/admin user
    :_setup_ticket_policy: Setup Pyramid AuthTktAuthenticationPolicy
    :_setup_apikey_policy: Setup nefertari.ApiKeyAuthenticationPolicy
    :setup_auth_policies: Runs generation of particular auth policy
"""
import logging

from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy

from nefertari.utils import dictset
from nefertari.json_httpexceptions import *
from nefertari.authentication.policies import ApiKeyAuthenticationPolicy
from nefertari.authentication.views import (
    TicketAuthenticationView, TokenAuthenticationView)

log = logging.getLogger(__name__)


def _setup_ticket_policy(config, params):
    """ Setup Pyramid AuthTktAuthenticationPolicy.

    Notes:
      * Initial `secret` params value is considered to be a name of config
        param that represents a cookie name.
      * `auth_model.groups_by_userid` is used as a `callback`.
      * Special processing is applied to boolean params to convert string
        values like 'True', 'true' to booleans. This is done because pyraml
        parser currently does not support setting value being a boolean.
      * Also connects basic routes to perform authn actions.

    Arguments:
        :config: Pyramid Configurator instance.
        :params: Nefertari dictset which contains security scheme `settings`.
    """
    log.info('Configuring Pyramid Ticket Authn policy')
    if 'secret' not in params:
        raise ValueError(
            'Missing required security scheme settings: secret')
    bool_keys = ('secure', 'include_ip', 'http_only', 'wild_domain', 'debug',
                 'parent_domain')
    for key in bool_keys:
        params[key] = params.asbool(key)

    params['secret'] = config.registry.settings[params['secret']]

    auth_model = config.registry.auth_model
    params['callback'] = auth_model.groups_by_userid

    config.add_request_method(
        auth_model.authuser_by_userid, 'user', reify=True)

    policy = AuthTktAuthenticationPolicy(**params)

    class RamsesTicketAuthenticationView(TicketAuthenticationView):
        _model_class = config.registry.auth_model

    config.add_route('login', '/auth/login')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='login', attr='login', request_method='POST')

    config.add_route('logout', '/auth/logout')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='logout', attr='logout')

    config.add_route('register', '/auth/register')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='register', attr='register', request_method='POST')

    return policy


def _setup_apikey_policy(config, params):
    """ Setup `nefertari.ApiKeyAuthenticationPolicy`.

    Notes:
      * User may provide model name in :params['user_model']: do define
        the name of the user model.
      * `auth_model.groups_by_token` is used to perform username & token check
      * `auth_model.token_credentials` is used to get username and token from
        userid
      * Also connects basic routes to perform authn actions.

    Arguments:
        :config: Pyramid Configurator instance.
        :params: Nefertari dictset which contains security scheme `settings`.
    """
    log.info('Configuring ApiKey Authn policy')

    auth_model = config.registry.auth_model
    params['check'] = auth_model.groups_by_token
    params['credentials_callback'] = auth_model.token_credentials
    params['user_model'] = auth_model
    config.add_request_method(
        auth_model.authuser_by_name, 'user', reify=True)

    policy = ApiKeyAuthenticationPolicy(**params)

    class RamsesTokenAuthenticationView(TokenAuthenticationView):
        _model_class = config.registry.auth_model

    config.add_route('token', '/auth/token')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='token', attr='claim_token', request_method='POST')

    config.add_route('token_reset', '/auth/token_reset')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='token_reset', attr='token_reset', request_method='POST')

    config.add_route('register', '/auth/register')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='register', attr='register', request_method='POST')

    return policy


""" Map of `security_scheme_type`: `generator_function`, where:

  * `security_scheme_type`: String that represents RAML security scheme type
    name that should be used to apply a particular authentication system.
  * `generator_function`: Function that receives instance of Pyramid
    Configurator instance and dictset of security scheme settings and returns
    generated Pyramid authentication policy instance.

"""
AUTHENTICATION_POLICIES = {
    'x-ApiKey': _setup_apikey_policy,
    'x-Ticket': _setup_ticket_policy,
}


def setup_auth_policies(config, raml_data):<commit_msg>Rename methods/functions to be prefixed by verbs<commit_after>"""
Auth module that contains all code needed for authentication/authorization
policies setup.

In particular:
    :includeme: Function that actually creates routes listed above and
        connects view to them
    :create_admin_user: Function that creates system/admin user
    :_setup_ticket_policy: Setup Pyramid AuthTktAuthenticationPolicy
    :_setup_apikey_policy: Setup nefertari.ApiKeyAuthenticationPolicy
    :setup_auth_policies: Runs generation of particular auth policy
"""
import logging

from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy

from nefertari.utils import dictset
from nefertari.json_httpexceptions import *
from nefertari.authentication.policies import ApiKeyAuthenticationPolicy
from nefertari.authentication.views import (
    TicketAuthenticationView, TokenAuthenticationView)

log = logging.getLogger(__name__)


def _setup_ticket_policy(config, params):
    """ Setup Pyramid AuthTktAuthenticationPolicy.

    Notes:
      * Initial `secret` params value is considered to be a name of config
        param that represents a cookie name.
      * `auth_model.get_groups_by_userid` is used as a `callback`.
      * Special processing is applied to boolean params to convert string
        values like 'True', 'true' to booleans. This is done because pyraml
        parser currently does not support setting value being a boolean.
      * Also connects basic routes to perform authn actions.

    Arguments:
        :config: Pyramid Configurator instance.
        :params: Nefertari dictset which contains security scheme `settings`.
    """
    log.info('Configuring Pyramid Ticket Authn policy')
    if 'secret' not in params:
        raise ValueError(
            'Missing required security scheme settings: secret')
    bool_keys = ('secure', 'include_ip', 'http_only', 'wild_domain', 'debug',
                 'parent_domain')
    for key in bool_keys:
        params[key] = params.asbool(key)

    params['secret'] = config.registry.settings[params['secret']]

    auth_model = config.registry.auth_model
    params['callback'] = auth_model.get_groups_by_userid

    config.add_request_method(
        auth_model.get_authuser_by_userid, 'user', reify=True)

    policy = AuthTktAuthenticationPolicy(**params)

    class RamsesTicketAuthenticationView(TicketAuthenticationView):
        _model_class = config.registry.auth_model

    config.add_route('login', '/auth/login')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='login', attr='login', request_method='POST')

    config.add_route('logout', '/auth/logout')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='logout', attr='logout')

    config.add_route('register', '/auth/register')
    config.add_view(
        view=RamsesTicketAuthenticationView,
        route_name='register', attr='register', request_method='POST')

    return policy


def _setup_apikey_policy(config, params):
    """ Setup `nefertari.ApiKeyAuthenticationPolicy`.

    Notes:
      * User may provide model name in :params['user_model']: do define
        the name of the user model.
      * `auth_model.get_groups_by_token` is used to perform username & token
        check
      * `auth_model.get_token_credentials` is used to get username and token
        from userid
      * Also connects basic routes to perform authn actions.

    Arguments:
        :config: Pyramid Configurator instance.
        :params: Nefertari dictset which contains security scheme `settings`.
    """
    log.info('Configuring ApiKey Authn policy')

    auth_model = config.registry.auth_model
    params['check'] = auth_model.get_groups_by_token
    params['credentials_callback'] = auth_model.get_token_credentials
    params['user_model'] = auth_model
    config.add_request_method(
        auth_model.get_authuser_by_name, 'user', reify=True)

    policy = ApiKeyAuthenticationPolicy(**params)

    class RamsesTokenAuthenticationView(TokenAuthenticationView):
        _model_class = config.registry.auth_model

    config.add_route('token', '/auth/token')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='token', attr='claim_token', request_method='POST')

    config.add_route('reset_token', '/auth/reset_token')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='reset_token', attr='reset_token', request_method='POST')

    config.add_route('register', '/auth/register')
    config.add_view(
        view=RamsesTokenAuthenticationView,
        route_name='register', attr='register', request_method='POST')

    return policy


""" Map of `security_scheme_type`: `generator_function`, where:

  * `security_scheme_type`: String that represents RAML security scheme type
    name that should be used to apply a particular authentication system.
  * `generator_function`: Function that receives instance of Pyramid
    Configurator instance and dictset of security scheme settings and returns
    generated Pyramid authentication policy instance.

"""
AUTHENTICATION_POLICIES = {
    'x-ApiKey': _setup_apikey_policy,
    'x-Ticket': _setup_ticket_policy,
}


def setup_auth_policies(config, raml_data):
{'fraction_non_alphanumeric': 0.0839550476910001, 'fraction_numerical': 0.0, 'mean_word_length': 4.038058991436727, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5646459', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2868, 'n_words': 777}