<commit_before># -*- coding: utf-8 -*-

import logging

from six.moves import http_client

from werkzeug.security import pbkdf2_hex

from flask import g, current_app
from flask_restplus import abort
from click import secho

from drift.core.extensions import jwt

from driftbase.models.db import User, CorePlayer, UserIdentity, UserRole
from driftbase.utils import UserCache

log = logging.getLogger(__name__)


def abort_unauthorized(description):
    """Raise an Unauthorized exception.
    """
    secho("FUDGE {}".format(description), fg="red")
    abort(http_client.UNAUTHORIZED, description=description)


def authenticate_with_provider(auth_info):

    provider_details = auth_info.get('provider_details')
    automatic_account_creation = auth_info.get("automatic_account_creation", True)

    if auth_info['provider'] in ['device_id', 'user+pass', 'uuid', 'unit_test']:
        # Authenticate using access key, secret key pair
        # (or username, password pair)
        identity = authenticate(auth_info['username'],
                                auth_info['password'],
                                automatic_account_creation)
    elif auth_info['provider'] == "gamecenter":
        from driftbase.auth.gamecenter import validate_gamecenter_token
        identity_id = validate_gamecenter_token(provider_details)
        # The GameCenter user_id cannot be stored in plain text, so let's
        # give it one cycle of hashing.
        username = "gamecenter:" + pbkdf2_hex(identity_id, "staticsalt",
                                              iterations=1)
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "steam":
        from driftbase.auth.steam import validate_steam_ticket
        identity_id = validate_steam_ticket()
        username = "steam:" + identity_id
        identity = authenticate(username, "", True or automatic_account_creation)
    elif auth_info['provider'] == "oculus" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "oculus:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "oculus":
        from driftbase.auth.oculus import validate_oculus_ticket
        identity_id = validate_oculus_ticket()
        username = "oculus:" + identity_id
        identity = authenticate(username, "", True or automatic_account_creation)
    elif auth_info['provider'] == "viveport" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "viveport:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "hypereal" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "hypereal:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "googleplay" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "googleplay:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, automatic_account_creation)
    elif auth_info['provider'] == "googleplay":
        from driftbase.auth.googleplay import validate_googleplay_token
        identity_id = validate_googleplay_token()
        username = "googleplay:" + identity_id
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "psn":
        from driftbase.auth.psn import validate_psn_ticket
        identity_id = validate_psn_ticket()
        username = "psn:" + identity_id
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "7663":
        username = "7663:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    else:
        abort_unauthorized("Bad Request. Unknown provider '%s'." %
                           auth_info['provider'])

    return identity


# LEGACY STUFF HERE. BAD WAY OF INJECTING
jwt.authenticate_with_provider = authenticate_with_provider


def authenticate(username, password, automatic_account_creation=True):
    """basic authentication"""
    identity_type = ""<commit_msg>Add registration callback for jwt authentication providers<commit_after># -*- coding: utf-8 -*-

import logging

from six.moves import http_client

from werkzeug.security import pbkdf2_hex

from flask import g, current_app
from flask_restplus import abort
from click import secho

from drift.core.extensions import jwt

from driftbase.models.db import User, CorePlayer, UserIdentity, UserRole
from driftbase.utils import UserCache

log = logging.getLogger(__name__)

def drift_init_extension(app, api, **kwds):
    jwt.register_auth_provider(app, "default", authenticate_with_provider)


def abort_unauthorized(description):
    """Raise an Unauthorized exception.
    """
    secho("FUDGE {}".format(description), fg="red")
    abort(http_client.UNAUTHORIZED, description=description)


def authenticate_with_provider(auth_info):

    provider_details = auth_info.get('provider_details')
    automatic_account_creation = auth_info.get("automatic_account_creation", True)

    if auth_info['provider'] in ['device_id', 'user+pass', 'uuid', 'unit_test']:
        # Authenticate using access key, secret key pair
        # (or username, password pair)
        identity = authenticate(auth_info['username'],
                                auth_info['password'],
                                automatic_account_creation)
    elif auth_info['provider'] == "gamecenter":
        from driftbase.auth.gamecenter import validate_gamecenter_token
        identity_id = validate_gamecenter_token(provider_details)
        # The GameCenter user_id cannot be stored in plain text, so let's
        # give it one cycle of hashing.
        username = "gamecenter:" + pbkdf2_hex(identity_id, "staticsalt",
                                              iterations=1)
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "steam":
        from driftbase.auth.steam import validate_steam_ticket
        identity_id = validate_steam_ticket()
        username = "steam:" + identity_id
        identity = authenticate(username, "", True or automatic_account_creation)
    elif auth_info['provider'] == "oculus" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "oculus:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "oculus":
        from driftbase.auth.oculus import validate_oculus_ticket
        identity_id = validate_oculus_ticket()
        username = "oculus:" + identity_id
        identity = authenticate(username, "", True or automatic_account_creation)
    elif auth_info['provider'] == "viveport" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "viveport:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "hypereal" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "hypereal:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    elif auth_info['provider'] == "googleplay" and provider_details.get('provisional', False):
        if len(provider_details['username']) < 1:
            abort_unauthorized("Bad Request. 'username' cannot be an empty string.")
        username = "googleplay:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, automatic_account_creation)
    elif auth_info['provider'] == "googleplay":
        from driftbase.auth.googleplay import validate_googleplay_token
        identity_id = validate_googleplay_token()
        username = "googleplay:" + identity_id
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "psn":
        from driftbase.auth.psn import validate_psn_ticket
        identity_id = validate_psn_ticket()
        username = "psn:" + identity_id
        identity = authenticate(username, "", automatic_account_creation)
    elif auth_info['provider'] == "7663":
        username = "7663:" + provider_details['username']
        password = provider_details['password']
        identity = authenticate(username, password, True or automatic_account_creation)
    else:
        abort_unauthorized("Bad Request. Unknown provider '%s'." %
                           auth_info['provider'])

    return identity


def authenticate(username, password, automatic_account_creation=True):
    """basic authentication"""
    identity_type = ""
{'fraction_non_alphanumeric': 0.1013996280708623, 'fraction_numerical': 0.0031320348438876383, 'mean_word_length': 3.6508875739644973, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6014164', 'n_tokens_mistral': 2750, 'n_tokens_neox': 2642, 'n_words': 674}