<commit_before>"""
Locarise OAuth 2.0 support
"""
import json

from urllib2 import Request

from social_auth.backends import OAuthBackend, BaseOAuth2
from social_auth.utils import dsa_urlopen


# Locarise OAuth2 base configuration
LOCARISE_OAUTH2_SERVER = 'accounts.locarise.com'
LOCARISE_OATUH2_AUTHORIZATION_URL = 'https://accounts.locarise.com/oauth2/authorize'

# scope for user email, specify extra scopes in settings, for example:
LOCARISE_OAUTH2_SCOPE = ['read']
LOCARISE_API_PROFILE = 'https://accounts.locarise.com/userinfo.json'


# Backends
class LocariseOAuth2Backend(OAuthBackend):

    """Locarise OAuth authentication backend"""
    name = 'locarise-oauth2'
    EXTRA_DATA = [
        ('refresh_token', 'refresh_token', True),
        ('expires_in', 'expires'),
        ('token_type', 'token_type', True)
    ]
    ID_KEY = 'uid'

    def get_user_details(self, response):
        email = response.get('email', '')
        return {'username': email.split('@', 1)[0],
                'email': email,
                'fullname': response.get('first_name', '') + ' ' + response.get('last_name', ''),
                'first_name': response.get('first_name', ''),
                'last_name': response.get('last_name', ''),
                'is_staff': response.get('is_staff', ''),
                'locale': response.get('locale', ''),
                'membership_set': response.get('membership_set', ''),
                'uid': response.get('uid', '')}


class LocariseOAuth2(BaseOAuth2):

    """Locarise OAuth2 support"""
    AUTH_BACKEND = LocariseOAuth2Backend
    AUTHORIZATION_URL = 'https://accounts.locarise.com/oauth2/authorize'
    ACCESS_TOKEN_URL = 'https://accounts.locarise.com/oauth2/access_token'
    SETTINGS_KEY_NAME = 'LOCARISE_OAUTH2_CLIENT_ID'
    SETTINGS_SECRET_NAME = 'LOCARISE_OAUTH2_CLIENT_SECRET'
    SCOPE_VAR_NAME = 'LOCARISE_OAUTH_EXTRA_SCOPE'
    DEFAULT_SCOPE = LOCARISE_OAUTH2_SCOPE
    REDIRECT_STATE = False

    def user_data(self, access_token, *args, **kwargs):
        """Return user data from Locarise API"""
        return locariseapis_profile(LOCARISE_API_PROFILE, access_token)


def locariseapis_profile(url, access_token):
    """
    Loads user data from Locarise Sane Account Managemt Service, such as name,
    first_name, last_name, etc. as it's described in:<commit_msg>Enable the use of environ variables for oauth2<commit_after>"""
Locarise OAuth 2.0 support
"""
import os
import json

from urllib2 import Request

from social_auth.backends import OAuthBackend, BaseOAuth2
from social_auth.utils import dsa_urlopen


# Locarise OAuth2 base configuration
LOCARISE_OAUTH2_SERVER = os.environ.get('LOCARISE_OAUTH2_SERVER', 'accounts.locarise.com')
LOCARISE_OAUTH2_TOKEN_URL = os.environ.get('LOCARISE_OAUTH2_TOKEN_URL', 'https://accounts.locarise.com/oauth2/access_token')
LOCARISE_OAUTH2_AUTHORIZATION_URL = os.environ.get('LOCARISE_OAUTH2_AUTHORIZATION_URL', 'https://accounts.locarise.com/oauth2/authorize')

# scope for user email, specify extra scopes in settings, for example:
LOCARISE_OAUTH2_SCOPE = ['read']
LOCARISE_API_PROFILE = os.environ.get('LOCARISE_API_PROFILE', 'https://accounts.locarise.com/userinfo.json')


# Backends
class LocariseOAuth2Backend(OAuthBackend):

    """Locarise OAuth authentication backend"""
    name = 'locarise-oauth2'
    EXTRA_DATA = [
        ('refresh_token', 'refresh_token', True),
        ('expires_in', 'expires'),
        ('token_type', 'token_type', True)
    ]
    ID_KEY = 'uid'

    def get_user_details(self, response):
        email = response.get('email', '')
        return {'username': email.split('@', 1)[0],
                'email': email,
                'fullname': response.get('first_name', '') + ' ' + response.get('last_name', ''),
                'first_name': response.get('first_name', ''),
                'last_name': response.get('last_name', ''),
                'is_staff': response.get('is_staff', ''),
                'locale': response.get('locale', ''),
                'membership_set': response.get('membership_set', ''),
                'uid': response.get('uid', '')}


class LocariseOAuth2(BaseOAuth2):

    """Locarise OAuth2 support"""
    AUTH_BACKEND = LocariseOAuth2Backend
    AUTHORIZATION_URL = LOCARISE_OAUTH2_AUTHORIZATION_URL
    ACCESS_TOKEN_URL = LOCARISE_OAUTH2_TOKEN_URL
    SETTINGS_KEY_NAME = 'LOCARISE_OAUTH2_CLIENT_ID'
    SETTINGS_SECRET_NAME = 'LOCARISE_OAUTH2_CLIENT_SECRET'
    SCOPE_VAR_NAME = 'LOCARISE_OAUTH_EXTRA_SCOPE'
    DEFAULT_SCOPE = LOCARISE_OAUTH2_SCOPE
    REDIRECT_STATE = False

    def user_data(self, access_token, *args, **kwargs):
        """Return user data from Locarise API"""
        return locariseapis_profile(LOCARISE_API_PROFILE, access_token)


def locariseapis_profile(url, access_token):
    """
    Loads user data from Locarise Sane Account Managemt Service, such as name,
    first_name, last_name, etc. as it's described in:
{'fraction_non_alphanumeric': 0.12307067424857839, 'fraction_numerical': 0.010154346060113728, 'mean_word_length': 3.8617966436327738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 7, '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': '4260454', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1674, 'n_words': 306}