# -*-coding:utf-8 -*-
from django.contrib.auth.models import User
from libs.hashers import *
from models import User

class customAuthentification(object):
    """
    Use the login name and a hash of the password. For example:

    """

    def authenticate(self, username=None, password=None):
        print "======= on essaye custom authentification avec %s et %s" % (username,password)
        if username:
            try:
                user = User.objects.get(username=username)
                encoder = SHA256PasswordHasher()
                if SHA256PasswordHasher.verify(encoder,password,user.password):
                    print "PASSWORD OK"
                    return user
                else:
                    print "PASSWORD KO"
                    return  None
            except User.DoesNotExist:
                print "User not exists"
            return None
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None