2019-01-07 11:20:39 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
from radiusauth.backends import RADIUSBackend, RADIUSRealmBackend
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
2019-11-11 12:10:49 +00:00
|
|
|
|
from pyrad.packet import AccessRequest
|
|
|
|
|
|
2019-01-07 11:20:39 +00:00
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CreateUserMixin:
|
2020-07-16 09:07:40 +00:00
|
|
|
|
def get_django_user(self, username, password=None, *args, **kwargs):
|
2019-01-07 11:20:39 +00:00
|
|
|
|
if isinstance(username, bytes):
|
|
|
|
|
username = username.decode()
|
|
|
|
|
try:
|
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
if '@' in username:
|
|
|
|
|
email = username
|
|
|
|
|
else:
|
|
|
|
|
email_suffix = settings.EMAIL_SUFFIX
|
|
|
|
|
email = '{}@{}'.format(username, email_suffix)
|
|
|
|
|
user = User(username=username, name=username, email=email)
|
|
|
|
|
user.source = user.SOURCE_RADIUS
|
|
|
|
|
user.save()
|
|
|
|
|
return user
|
|
|
|
|
|
2020-07-16 09:07:40 +00:00
|
|
|
|
def authenticate(self, *args, **kwargs):
|
|
|
|
|
# 校验用户时,会传入public_key参数,父类authentication中不接受public_key参数,所以要pop掉
|
|
|
|
|
# TODO:需要优化各backend的authenticate方法,django进行调用前会检测各authenticate的参数
|
|
|
|
|
kwargs.pop('public_key', None)
|
2020-07-20 10:49:05 +00:00
|
|
|
|
return super().authenticate(*args, **kwargs)
|
2020-07-16 09:07:40 +00:00
|
|
|
|
|
2019-01-07 11:20:39 +00:00
|
|
|
|
|
|
|
|
|
class RadiusBackend(CreateUserMixin, RADIUSBackend):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RadiusRealmBackend(CreateUserMixin, RADIUSRealmBackend):
|
|
|
|
|
pass
|