# 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/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)


from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import MinecraftAccount
from .authenticate import authenticate
                'You entered invalid credentials. '
                'Username or password are incorrect.'))
        else:
            selected_profile = response.get('selectedProfile')

            # Add selected profile
            profile_id = selected_profile.get('id')

            try:
                MinecraftAccount.objects.get(profile_id=profile_id)
            except MinecraftAccount.DoesNotExist:
                self.profiles.append(selected_profile)
            else:
                raise forms.ValidationError(_(
                    'This Minecraft Profile is already being used '
                    'by another account.'))

            # Append other profiles
            for other_profile in response.get('availableProfiles'):
                if other_profile.get('id') != profile_id:
                    self.profiles.append(other_profile)

        return cleaned_data

    def save(self, user):
        is_first_profile = not user.minecraft_accounts.exists()

        # Save profiles
        for order, profile in enumerate(self.profiles):
            primary = is_first_profile and order == 0

# 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/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/forms.py
from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import MinecraftAccount
from .authenticate import authenticate

                'You entered invalid credentials. '
                'Username or password are incorrect.'))
        else:
            selected_profile = response.get('selectedProfile')

            # Add selected profile
            profile_id = selected_profile.get('id')

            try:
                MinecraftAccount.objects.get(profile_id=profile_id)
            except MinecraftAccount.DoesNotExist:
                self.profiles.append(selected_profile)
            else:
                raise forms.ValidationError(_(
                    'This Minecraft Profile is already being used '
                    'by another account.'))

            # Append other profiles
            for other_profile in response.get('availableProfiles'):
                if other_profile.get('id') != profile_id:
                    self.profiles.append(other_profile)

        return cleaned_data

    def save(self, user):
        is_first_profile = not user.minecraft_accounts.exists()

        # Save profiles
        for order, profile in enumerate(self.profiles):
            primary = is_first_profile and order == 0

            MinecraftAccount.objects.create(