<commit_before>"""Django authentication backends.

For more information visit https://docs.djangoproject.com/en/dev/topics/auth/customizing/.
"""
import datetime
import json
import warnings
from calendar import timegm

import jwt
import six
from django.conf import settings
from django.dispatch import Signal
from jwkest.jwk import KEYS
from social_core.backends.oauth import BaseOAuth2
from social_core.backends.open_id_connect import OpenIdConnectAuth
from social_core.exceptions import AuthTokenError

PROFILE_CLAIMS_TO_DETAILS_KEY_MAP = {
    'preferred_username': 'username',
    'email': 'email',
    'name': 'full_name',
    'given_name': 'first_name',
    'family_name': 'last_name',
    'locale': 'language',
}


def _merge_two_dicts(x, y):
    """
    Given two dicts, merge them into a new dict as a shallow copy.

    Once Python 3.6+ only is supported, replace method with ``z = {**x, **y}``
    """
    z = x.copy()
    z.update(y)
    return z


# pylint: disable=abstract-method,missing-docstring
class EdXBackendMixin(object):
    # used by social-auth
    ACCESS_TOKEN_METHOD = 'POST'
    DEFAULT_SCOPE = ['profile', 'email']
    ID_KEY = 'preferred_username'

    # local only (not part of social-auth)
    CLAIMS_TO_DETAILS_KEY_MAP = PROFILE_CLAIMS_TO_DETAILS_KEY_MAP

    def get_user_details(self, response):
        details = self._map_user_details(response)

        # Limits the scope of languages we can use
        locale = response.get('locale')
        if locale:
            details['language'] = _to_language(response['locale'])

        # Set superuser bit if the provider determines the user is an administrator
        details['is_superuser'] = details['is_staff'] = response.get('administrator', False)

        return details

    def get_public_or_internal_url_root(self):
        return self.setting('PUBLIC_URL_ROOT') or self.setting('URL_ROOT')

    def _map_user_details(self, response):
        """Maps key/values from the response to key/values in the user model.

        Does not transfer any key/value that is empty or not present in the response.
        """
        dest = {}
        for source_key, dest_key in self.CLAIMS_TO_DETAILS_KEY_MAP.items():<commit_msg>Fix anomaly between staff and superuser<commit_after>"""Django authentication backends.

For more information visit https://docs.djangoproject.com/en/dev/topics/auth/customizing/.
"""
import datetime
import json
import warnings
from calendar import timegm

import jwt
import six
from django.conf import settings
from django.dispatch import Signal
from jwkest.jwk import KEYS
from social_core.backends.oauth import BaseOAuth2
from social_core.backends.open_id_connect import OpenIdConnectAuth
from social_core.exceptions import AuthTokenError

PROFILE_CLAIMS_TO_DETAILS_KEY_MAP = {
    'preferred_username': 'username',
    'email': 'email',
    'name': 'full_name',
    'given_name': 'first_name',
    'family_name': 'last_name',
    'locale': 'language',
}


def _merge_two_dicts(x, y):
    """
    Given two dicts, merge them into a new dict as a shallow copy.

    Once Python 3.6+ only is supported, replace method with ``z = {**x, **y}``
    """
    z = x.copy()
    z.update(y)
    return z


# pylint: disable=abstract-method,missing-docstring
class EdXBackendMixin(object):
    # used by social-auth
    ACCESS_TOKEN_METHOD = 'POST'
    DEFAULT_SCOPE = ['profile', 'email']
    ID_KEY = 'preferred_username'

    # local only (not part of social-auth)
    CLAIMS_TO_DETAILS_KEY_MAP = PROFILE_CLAIMS_TO_DETAILS_KEY_MAP

    def get_user_details(self, response):
        details = self._map_user_details(response)

        # Limits the scope of languages we can use
        locale = response.get('locale')
        if locale:
            details['language'] = _to_language(response['locale'])

        details['is_staff'] = response.get('administrator', False)
        details['is_superuser'] = response.get('superuser', False)

        return details

    def get_public_or_internal_url_root(self):
        return self.setting('PUBLIC_URL_ROOT') or self.setting('URL_ROOT')

    def _map_user_details(self, response):
        """Maps key/values from the response to key/values in the user model.

        Does not transfer any key/value that is empty or not present in the response.
        """
        dest = {}
        for source_key, dest_key in self.CLAIMS_TO_DETAILS_KEY_MAP.items():
{'fraction_non_alphanumeric': 0.09829738933030648, 'fraction_numerical': 0.001362088535754824, 'mean_word_length': 3.6575052854122623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1649418', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1337, 'n_words': 376}