# Path: mineid/mcaccounts/authenticate.py
# def authenticate(username, password):
#     url = 'https://authserver.mojang.com/authenticate'
# 
#     payload = {
#         'agent': {
#             'name': 'Minecraft',
#             'version': 1,
#         },
#         'username': username,
#         'password': password,
#     }
# 
#     headers = {
#         'Content-Type': 'application/json',
#         'Accept': 'application/json',
#     }
# 
#     try:
#         request = urllib2.Request(url, json.dumps(payload), headers)
#         response = urllib2.urlopen(request)
# 
#         try:
#             response_payload = json.loads(response.read())
# 
#         finally:
#             response.close()  # always close socket
# 
#         return response_payload
# 
#     except urllib2.HTTPError as e:
#         raise MinecraftAuthenticationError(e)
# 
# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/utils.py
# def create_minecraft_user(username, profiles, primary):
#     try:
#         validate_email(username)
#     except ValidationError:
#         email = None
#     else:
#         email = username
# 
#     # This user can only log in through Minecraft
#     # authentication backend
#     user = User(username=username, email=email)
#     user.set_unusable_password()
# 
#     try:
#         user.save()
#     except IntegrityError:
#         raise UserAlreadyExists
# 
#     # Save profiles
#     for profile in profiles:
#         MinecraftAccount.objects.create(
#             user=user,
#             profile=profile.get('name'),
#             profile_id=profile.get('id'),
#             primary=primary,
#         )
# 
#     return user
# 
# Path: mineid/mcaccounts/exceptions.py
# class UserAlreadyExists(Exception):
#     pass


from django.contrib.auth import get_user_model
from ..authenticate import authenticate
from ..models import MinecraftAccount
from ..utils import create_minecraft_user
from ..exceptions import UserAlreadyExists



User = get_user_model()


class MinecraftBackend(object):
    def authenticate(self, username=None, password=None):
        try:

# Path: mineid/mcaccounts/authenticate.py
# def authenticate(username, password):
#     url = 'https://authserver.mojang.com/authenticate'
# 
#     payload = {
#         'agent': {
#             'name': 'Minecraft',
#             'version': 1,
#         },
#         'username': username,
#         'password': password,
#     }
# 
#     headers = {
#         'Content-Type': 'application/json',
#         'Accept': 'application/json',
#     }
# 
#     try:
#         request = urllib2.Request(url, json.dumps(payload), headers)
#         response = urllib2.urlopen(request)
# 
#         try:
#             response_payload = json.loads(response.read())
# 
#         finally:
#             response.close()  # always close socket
# 
#         return response_payload
# 
#     except urllib2.HTTPError as e:
#         raise MinecraftAuthenticationError(e)
# 
# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/utils.py
# def create_minecraft_user(username, profiles, primary):
#     try:
#         validate_email(username)
#     except ValidationError:
#         email = None
#     else:
#         email = username
# 
#     # This user can only log in through Minecraft
#     # authentication backend
#     user = User(username=username, email=email)
#     user.set_unusable_password()
# 
#     try:
#         user.save()
#     except IntegrityError:
#         raise UserAlreadyExists
# 
#     # Save profiles
#     for profile in profiles:
#         MinecraftAccount.objects.create(
#             user=user,
#             profile=profile.get('name'),
#             profile_id=profile.get('id'),
#             primary=primary,
#         )
# 
#     return user
# 
# Path: mineid/mcaccounts/exceptions.py
# class UserAlreadyExists(Exception):
#     pass


# Path: mineid/mcaccounts/backends/authentication.py
from django.contrib.auth import get_user_model
from ..authenticate import authenticate
from ..models import MinecraftAccount
from ..utils import create_minecraft_user
from ..exceptions import UserAlreadyExists




User = get_user_model()


class MinecraftBackend(object):
    def authenticate(self, username=None, password=None):
        try:

            response = authenticate(username, password)