2018-11-09 06:54:38 +00:00
|
|
|
# coding:utf-8
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-02-28 03:58:48 +00:00
|
|
|
from .utils import new_client
|
|
|
|
from .models import OIDT_ACCESS_TOKEN
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
UserModel = get_user_model()
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2019-02-28 03:58:48 +00:00
|
|
|
client = new_client()
|
2018-11-09 06:54:38 +00:00
|
|
|
|
2019-02-28 03:58:48 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'OpenIDAuthorizationCodeBackend', 'OpenIDAuthorizationPasswordBackend',
|
|
|
|
]
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseOpenIDAuthorizationBackend(object):
|
|
|
|
@staticmethod
|
|
|
|
def user_can_authenticate(user):
|
|
|
|
"""
|
|
|
|
Reject users with is_active=False. Custom user models that don't have
|
|
|
|
that attribute are allowed.
|
|
|
|
"""
|
|
|
|
is_active = getattr(user, 'is_active', None)
|
|
|
|
return is_active or is_active is None
|
|
|
|
|
|
|
|
def get_user(self, user_id):
|
|
|
|
try:
|
|
|
|
user = UserModel._default_manager.get(pk=user_id)
|
|
|
|
except UserModel.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return user if self.user_can_authenticate(user) else None
|
|
|
|
|
|
|
|
|
|
|
|
class OpenIDAuthorizationCodeBackend(BaseOpenIDAuthorizationBackend):
|
|
|
|
def authenticate(self, request, **kwargs):
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authentication OpenID code backend')
|
2018-11-09 06:54:38 +00:00
|
|
|
code = kwargs.get('code')
|
|
|
|
redirect_uri = kwargs.get('redirect_uri')
|
|
|
|
if not code or not redirect_uri:
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate failed: No code or No redirect uri')
|
2018-11-09 06:54:38 +00:00
|
|
|
return None
|
|
|
|
try:
|
|
|
|
oidt_profile = client.update_or_create_from_code(
|
2019-07-01 03:04:15 +00:00
|
|
|
code=code, redirect_uri=redirect_uri
|
2019-03-07 10:41:42 +00:00
|
|
|
)
|
2018-11-09 06:54:38 +00:00
|
|
|
except Exception as e:
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate failed: get oidt_profile: {}'.format(e))
|
2019-07-01 03:04:15 +00:00
|
|
|
return None
|
2018-11-09 06:54:38 +00:00
|
|
|
else:
|
|
|
|
# Check openid user single logout or not with access_token
|
|
|
|
request.session[OIDT_ACCESS_TOKEN] = oidt_profile.access_token
|
|
|
|
user = oidt_profile.user
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate success: user -> {}'.format(user))
|
2018-11-09 06:54:38 +00:00
|
|
|
return user if self.user_can_authenticate(user) else None
|
|
|
|
|
|
|
|
|
|
|
|
class OpenIDAuthorizationPasswordBackend(BaseOpenIDAuthorizationBackend):
|
|
|
|
def authenticate(self, request, username=None, password=None, **kwargs):
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authentication OpenID password backend')
|
2019-07-01 03:04:15 +00:00
|
|
|
if not username:
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate failed: Not username')
|
2018-11-09 06:54:38 +00:00
|
|
|
return None
|
|
|
|
try:
|
|
|
|
oidt_profile = client.update_or_create_from_password(
|
|
|
|
username=username, password=password
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2019-07-01 03:04:15 +00:00
|
|
|
logger.error(e, exc_info=True)
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate failed: get oidt_profile: {}'.format(e))
|
2019-07-01 03:04:15 +00:00
|
|
|
return None
|
2018-11-09 06:54:38 +00:00
|
|
|
else:
|
|
|
|
user = oidt_profile.user
|
2019-03-07 10:41:42 +00:00
|
|
|
logger.info('Authenticate success: user -> {}'.format(user))
|
2018-11-09 06:54:38 +00:00
|
|
|
return user if self.user_can_authenticate(user) else None
|
|
|
|
|