<commit_before>from social.backends.oauth import BaseOAuth2


class HastexoOAuth2(BaseOAuth2):
    """Hastexo OAuth2 authentication backend"""

    name = 'hastexo'
    BASE_URL = 'https://store.hastexo.com'
    ACCESS_TOKEN_METHOD = 'POST'
    SCOPE_SEPARATOR = ' '

    def __init__(self, *args, **kwargs):
        """
        Construct URLs dynamically.
        """
        self.AUTHORIZATION_URL = '{0}/o/authorize/'.format(self.BASE_URL)
        self.ACCESS_TOKEN_URL = '{0}/o/token/'.format(self.BASE_URL)
        super(HastexoOAuth2, self).__init__(*args, **kwargs)

    def get_user_details(self, response):
        """Return user details from hastexo account"""

        fullname, first_name, last_name = self.get_user_names(
            first_name = response.get('first_name'),
            last_name = response.get('last_name'),
        )

        return {
            'username': response.get('username'),
            'email': response.get('email', ''),
            'fullname': fullname,
            'first_name': first_name,
            'last_name': last_name
        }

    def user_data(self, access_token, *args, **kwargs):
        """Loads user data from service"""
        return self.get_json('{0}/api/login/'.format(self.BASE_URL),
            headers={'Authorization': 'Bearer {0}'.format(access_token)}
        )<commit_msg>Validate username<commit_after>from social.backends.oauth import BaseOAuth2
import re

class HastexoOAuth2(BaseOAuth2):
    """Hastexo OAuth2 authentication backend"""

    name = 'hastexo'
    BASE_URL = 'https://store.hastexo.com'
    ACCESS_TOKEN_METHOD = 'POST'
    SCOPE_SEPARATOR = ' '

    def __init__(self, *args, **kwargs):
        """
        Construct URLs dynamically.
        """
        self.AUTHORIZATION_URL = '{0}/o/authorize/'.format(self.BASE_URL)
        self.ACCESS_TOKEN_URL = '{0}/o/token/'.format(self.BASE_URL)
        super(HastexoOAuth2, self).__init__(*args, **kwargs)

    def get_user_details(self, response):
        """Return user details from hastexo account"""

        # Check if username is an email, and if so, remove the domain.  Also,
        # if we didn't get an email and the username looks like one, set the
        # email explicitly.
        username = response.get('username', '')
        email = response.get('email', '')
        if username:
            matches = re.match(r'([^@]+)@[^@]+\.[^@]+', username)
            if matches:
                email = email or username
                username = matches.group(1)

        # Parse names
        fullname, first_name, last_name = self.get_user_names(
            first_name = response.get('first_name'),
            last_name = response.get('last_name'),
        )

        return {
            'username': username,
            'email': email,
            'fullname': fullname,
            'first_name': first_name,
            'last_name': last_name
        }

    def user_data(self, access_token, *args, **kwargs):
        """Loads user data from service"""
        return self.get_json('{0}/api/login/'.format(self.BASE_URL),
            headers={'Authorization': 'Bearer {0}'.format(access_token)}
        )
{'fraction_non_alphanumeric': 0.13544303797468354, 'fraction_numerical': 0.006012658227848101, 'mean_word_length': 2.4247020585048755, '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': '943981', 'n_tokens_mistral': 982, 'n_tokens_neox': 961, 'n_words': 199}