# Path: locksmith/hub/models.py
# class Key(models.Model):
#     '''
#         API key to be handed out to Apis
#     '''
#     user = models.OneToOneField(User, null=True, blank=True, related_name='api_key')
# 
#     key = models.CharField(max_length=32, db_index=True)
#     email = models.EmailField(unique=True)
#     alternate_email = models.EmailField(blank=True, null=True) #
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     name = models.CharField('Name', max_length=100, blank=True, null=True,
#                             db_index=True)
#     org_name = models.CharField('Organization Name', max_length=100,
#                                 blank=True, null=True, db_index=True)
#     org_url = models.CharField('Organization URL', blank=True, null=True,
#                                max_length=200, db_index=True)
#     usage = models.TextField('Intended Usage', blank=True, null=True)
# 
#     promotable = models.BooleanField(default=False)
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def __unicode__(self):
#         return '%s %s [%s]' % (self.key, self.email, self.status)
# 
#     def mark_for_update(self):
#         '''
#             Note that a change has been made so all Statuses need update
#         '''
#         self.pub_statuses.exclude(status=UNPUBLISHED).update(status=NEEDS_UPDATE)
#         push_key.delay(self)
# 
#     class Meta:
#         db_table = 'locksmith_hub_key'


from django.contrib.auth.models import User
from locksmith.hub.models import Key

class LocksmithBackend:
    """
    Authenticate with email address/key.

    * Try to log in with email+password
    * If no user exists try and create one with key as password
    """

    def authenticate(self, username=None, password=None):
        try:
            # if user exists with this email try and log in
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user

        except User.DoesNotExist:
            # if no user exists try and create one based on the key
            try:
                key = Key.objects.get(email=username)
                if key.key == password:
                    return User.objects.create_user(username[:30], username,
                                                    password)

# Path: locksmith/hub/models.py
# class Key(models.Model):
#     '''
#         API key to be handed out to Apis
#     '''
#     user = models.OneToOneField(User, null=True, blank=True, related_name='api_key')
# 
#     key = models.CharField(max_length=32, db_index=True)
#     email = models.EmailField(unique=True)
#     alternate_email = models.EmailField(blank=True, null=True) #
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     name = models.CharField('Name', max_length=100, blank=True, null=True,
#                             db_index=True)
#     org_name = models.CharField('Organization Name', max_length=100,
#                                 blank=True, null=True, db_index=True)
#     org_url = models.CharField('Organization URL', blank=True, null=True,
#                                max_length=200, db_index=True)
#     usage = models.TextField('Intended Usage', blank=True, null=True)
# 
#     promotable = models.BooleanField(default=False)
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def __unicode__(self):
#         return '%s %s [%s]' % (self.key, self.email, self.status)
# 
#     def mark_for_update(self):
#         '''
#             Note that a change has been made so all Statuses need update
#         '''
#         self.pub_statuses.exclude(status=UNPUBLISHED).update(status=NEEDS_UPDATE)
#         push_key.delay(self)
# 
#     class Meta:
#         db_table = 'locksmith_hub_key'


# Path: locksmith/hub/auth_backend.py
from django.contrib.auth.models import User
from locksmith.hub.models import Key


class LocksmithBackend:
    """
    Authenticate with email address/key.

    * Try to log in with email+password
    * If no user exists try and create one with key as password
    """

    def authenticate(self, username=None, password=None):
        try:
            # if user exists with this email try and log in
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user

        except User.DoesNotExist:
            # if no user exists try and create one based on the key
            try:
                key = Key.objects.get(email=username)
                if key.key == password:
                    return User.objects.create_user(username[:30], username,
                                                    password)

            except Key.DoesNotExist: