<commit_before>from time import time
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url
from django.dispatch import receiver
from django.contrib.auth import logout
from django.contrib.auth.views import redirect_to_login
from django.contrib.auth.signals import user_logged_in
from . import signals

LOGIN_TIME_SESSION_KEY = 'hoover.contrib.twofactor.login_time'

@receiver(user_logged_in)
def on_login_success(sender, request, **kwargs):
    request.session[LOGIN_TIME_SESSION_KEY] = time()

class AutoLogout:
    def process_request(self, request):
        if not settings.HOOVER_TWOFACTOR_AUTOLOGOUT:
            return
        user = request.user
        if user.is_authenticated():
            login_time = request.session.get(LOGIN_TIME_SESSION_KEY) or 0
            if time() - login_time > settings.HOOVER_TWOFACTOR_AUTOLOGOUT:
                signals.auto_logout.send(AutoLogout,
                    username=user.get_username())
                logout(request)
                return HttpResponseRedirect(settings.HOOVER_BASE_URL)

class RequireAuth:
    def process_request(self, request):
        for prefix in ['/accounts/', '/invitation/']:
            if request.path.startswith(prefix):
                return

        user = request.user
        if user.is_verified():
            return

        resolved_login_url = resolve_url(settings.LOGIN_URL)
        return redirect_to_login(request.path, resolved_login_url, 'next')<commit_msg>use regexps for url whitelist<commit_after>from time import time
import re
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url
from django.dispatch import receiver
from django.contrib.auth import logout
from django.contrib.auth.views import redirect_to_login
from django.contrib.auth.signals import user_logged_in
from . import signals

LOGIN_TIME_SESSION_KEY = 'hoover.contrib.twofactor.login_time'

@receiver(user_logged_in)
def on_login_success(sender, request, **kwargs):
    request.session[LOGIN_TIME_SESSION_KEY] = time()

class AutoLogout:
    def process_request(self, request):
        if not settings.HOOVER_TWOFACTOR_AUTOLOGOUT:
            return
        user = request.user
        if user.is_authenticated():
            login_time = request.session.get(LOGIN_TIME_SESSION_KEY) or 0
            if time() - login_time > settings.HOOVER_TWOFACTOR_AUTOLOGOUT:
                signals.auto_logout.send(AutoLogout,
                    username=user.get_username())
                logout(request)
                return HttpResponseRedirect(settings.HOOVER_BASE_URL)

class RequireAuth:
    def process_request(self, request):
        WHITELIST = [
            r'^/accounts/',
            r'^/invitation/',
            r'^/static/',
            r'^/_ping$',
        ]
        for pattern in WHITELIST:
            if re.match(pattern, request.path):
                return

        user = request.user
        if user.is_verified():
            return

        resolved_login_url = resolve_url(settings.LOGIN_URL)
        return redirect_to_login(request.path, resolved_login_url, 'next')
{'fraction_non_alphanumeric': 0.07755102040816327, 'fraction_numerical': 0.0006279434850863422, 'mean_word_length': 2.914004914004914, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1198398', 'n_tokens_mistral': 929, 'n_tokens_neox': 897, 'n_words': 198}