<commit_before>#
# Copyright (C) 2007-2013 by frePPLe bvba
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import base64
import jwt
import time

from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import Permission
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.db import DEFAULT_DB_ALIAS
from django.db.models import Q
from django.http import HttpResponse

from freppledb.common.models import User, Scenario

import logging
logger = logging.getLogger(__name__)


class MultiDBBackend(ModelBackend):
  '''
  This customized authentication is based on the Django ModelBackend
  with the following extensions:
    - We allow a user to log in using either their user name or
      their email address.
    - Permissions are scenario-specific.

  This authentication backend relies on the MultiDBMiddleware class
  to assure that the user object refers to the correct database.
  '''
  def authenticate(self, username=None, password=None):
    try:
      validate_email(username)
      # The user name looks like an email address
      user = User.objects.get(email=username)
      if user.check_password(password):
        return user
    except User.DoesNotExist:
      # Run the default password hasher once to reduce the timing
      # difference between an existing and a non-existing user.
      # See django ticket #20760
      User().set_password(password)
    except (ValidationError, User.MultipleObjectsReturned):
      # The user name isn't an email address
      try:
        user = User.objects.get(username=username)
        if user.check_password(password):
          return user
      except User.DoesNotExist:
        # Run the default password hasher once to reduce the timing
        # difference between an existing and a non-existing user.
        # See django ticket #20760
        User().set_password(password)
<commit_msg>make user authentication case-insentitive<commit_after>#
# Copyright (C) 2007-2013 by frePPLe bvba
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import base64
import jwt
import time

from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import Permission
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.db import DEFAULT_DB_ALIAS
from django.db.models import Q
from django.http import HttpResponse

from freppledb.common.models import User, Scenario

import logging
logger = logging.getLogger(__name__)


class MultiDBBackend(ModelBackend):
  '''
  This customized authentication is based on the Django ModelBackend
  with the following extensions:
    - We allow a user to log in using either their user name or
      their email address.
    - Permissions are scenario-specific.

  This authentication backend relies on the MultiDBMiddleware class
  to assure that the user object refers to the correct database.
  '''
  def authenticate(self, request, username=None, password=<PASSWORD>):
    try:
      validate_email(username)
      # The user name looks like an email address
      user = User.objects.get(email__iexact=username)
      if user.check_password(password):
        return user
    except User.DoesNotExist:
      # Run the default password hasher once to reduce the timing
      # difference between an existing and a non-existing user.
      # See django ticket #20760
      User().set_password(password)
    except (ValidationError, User.MultipleObjectsReturned):
      # The user name isn't an email address
      try:
        user = User.objects.get(username__iexact=username)
        if user.check_password(password):
          return user
      except User.DoesNotExist:
        # Run the default password hasher once to reduce the timing
        # difference between an existing and a non-existing user.
        # See django ticket #20760
        User().set_password(password)

{'fraction_non_alphanumeric': 0.05733558178752108, 'fraction_numerical': 0.007869589657110737, 'mean_word_length': 3.698943661971831, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1325369', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1346, 'n_words': 606}