<commit_before>import json

from keystone import config
from keystone import wsgi


CONF = config.CONF


# Header used to transmit the auth token
AUTH_TOKEN_HEADER = 'X-Auth-Token'


# Environment variable used to pass the request context
CONTEXT_ENV = 'openstack.context'


# Environment variable used to pass the request params
PARAMS_ENV = 'openstack.params'


class TokenAuthMiddleware(wsgi.Middleware):
  def process_request(self, request):
    token = request.headers.get(AUTH_TOKEN_HEADER)
    context = request.environ.get(CONTEXT_ENV, {})
    context['token_id'] = token
    request.environ[CONTEXT_ENV] = context


class AdminTokenAuthMiddleware(wsgi.Middleware):
  """A trivial filter that checks for a pre-defined admin token.

  Sets 'is_admin' to true in the context, expected to be checked by
  methods that are admin-only.

  """

  def process_request(self, request):
    token = request.headers.get(AUTH_TOKEN_HEADER)
    context = request.environ.get(CONTEXT_ENV, {})
    context['is_admin'] = (token == CONF.admin_token)
    request.environ[CONTEXT_ENV] = context


class PostParamsMiddleware(wsgi.Middleware):
  """Middleware to allow method arguments to be passed as POST parameters.

  Filters out the parameters `self`, `context` and anything beginning with
  an underscore.

  """

  def process_request(self, request):
    params_parsed = request.params
    params = {}
    for k, v in params_parsed.iteritems():
      if k in ('self', 'context'):
        continue
      if k.startswith('_'):
        continue
      params[k] = v

    request.environ[PARAMS_ENV] = params


class JsonBodyMiddleware(wsgi.Middleware):
  """Middleware to allow method arguments to be passed as serialized JSON.

  Accepting arguments as JSON is useful for accepting data that may be more
  complex than simple primitives.

  In this case we accept it as urlencoded data under the key 'json' as in
  json=<urlencoded_json> but this could be extended to accept raw JSON
  in the POST body.

  Filters out the parameters `self`, `context` and anything beginning with
  an underscore.

  """

  def process_request(self, request):
    #if 'json' not in request.params:
    #    return

    params_json = request.body
    if not params_json:
      return

    params_parsed = json.loads(params_json)
    params = {}
    for k, v in params_parsed.iteritems():
      if k in ('self', 'context'):
        continue
      if k.startswith('_'):
        continue
      params[k] = v

    request.environ[PARAMS_ENV] = params<commit_msg>re-indent<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

import json

from keystone import config
from keystone import wsgi


CONF = config.CONF


# Header used to transmit the auth token
AUTH_TOKEN_HEADER = 'X-Auth-Token'


# Environment variable used to pass the request context
CONTEXT_ENV = 'openstack.context'


# Environment variable used to pass the request params
PARAMS_ENV = 'openstack.params'


class TokenAuthMiddleware(wsgi.Middleware):
    def process_request(self, request):
        token = request.headers.get(AUTH_TOKEN_HEADER)
        context = request.environ.get(CONTEXT_ENV, {})
        context['token_id'] = token
        request.environ[CONTEXT_ENV] = context


class AdminTokenAuthMiddleware(wsgi.Middleware):
    """A trivial filter that checks for a pre-defined admin token.

    Sets 'is_admin' to true in the context, expected to be checked by
    methods that are admin-only.

    """

    def process_request(self, request):
        token = request.headers.get(AUTH_TOKEN_HEADER)
        context = request.environ.get(CONTEXT_ENV, {})
        context['is_admin'] = (token == CONF.admin_token)
        request.environ[CONTEXT_ENV] = context


class PostParamsMiddleware(wsgi.Middleware):
    """Middleware to allow method arguments to be passed as POST parameters.

    Filters out the parameters `self`, `context` and anything beginning with
    an underscore.

    """

    def process_request(self, request):
        params_parsed = request.params
        params = {}
        for k, v in params_parsed.iteritems():
            if k in ('self', 'context'):
                continue
            if k.startswith('_'):
                continue
            params[k] = v

        request.environ[PARAMS_ENV] = params


class JsonBodyMiddleware(wsgi.Middleware):
    """Middleware to allow method arguments to be passed as serialized JSON.

    Accepting arguments as JSON is useful for accepting data that may be more
    complex than simple primitives.

    In this case we accept it as urlencoded data under the key 'json' as in
    json=<urlencoded_json> but this could be extended to accept raw JSON
    in the POST body.

    Filters out the parameters `self`, `context` and anything beginning with
    an underscore.

    """

    def process_request(self, request):
        #if 'json' not in request.params:
        #    return

        params_json = request.body
        if not params_json:
            return

        params_parsed = json.loads(params_json)
        params = {}
        for k, v in params_parsed.iteritems():
            if k in ('self', 'context'):
                continue
            if k.startswith('_'):
                continue
            params[k] = v

        request.environ[PARAMS_ENV] = params
{'fraction_non_alphanumeric': 0.0845442536327609, 'fraction_numerical': 0.0005661445555765239, 'mean_word_length': 3.045801526717557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4715734', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1491, 'n_words': 504}