#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
author lilx
created on 2016/8/6
 """
from __future__ import unicode_literals, absolute_import
from django.contrib.auth.backends import ModelBackend
from djdg_core.auth.models import User
from djdg_core.auth.models import OpenUser
from djdg_oauth.oauthcore import getSign


class TokenAuthenticationBackend(ModelBackend):
    """
    token方式验证逻辑
    """
    def authenticate(self, token):
        user = User(token=token)
        if user.is_authenticated():
            return user
        return None


class OpenAuthenticationBackend(ModelBackend):
    """
    oauth方式认证
    """
    def authenticate(self, body, appid, secret, signature):
        _signature = getSign(body, secret)
        if _signature != signature:
            return None
        user = OpenUser(appid, secret)
        return user
