Django REST auth - 存储在外部服务中的用户

时间:2017-05-13 17:31:24

标签: django authentication django-rest-framework soa

我一直想知道处理面向服务架构中使用Django的最佳方法,因此各个Django应用程序不会维护自己的用户存储。一个服务维护用户存储,而其他服务必须调用它以检索用户信息。

到目前为止的例子,我在考虑如何在Django REST中构建自定义身份验证类来处理这个问题:

class SOAAuthentication(authentication.BaseAuthentication):

    def authenticate(self, request):
        token = request.get_token_from_auth_header()

        if not remote_auth_service.is_token_valid(token):
            raise AuthFailed('Token is invalid')

        user_properties = remote_users_service.get_user(token):
        # user_properties is a dict of properties

        if not user_properties:
            raise AuthFailed('User does not exist.')

        user = MyCustomUserClass(**user_properties)
        return (user, 'soa')

因此,Django应用程序的数据库中不会保留用户信息,并且权限类可以查询MyCustomUserClass的实例以确定用户所属的组。

这种方法是否适用于简单的基于组的授权?我认为我不需要对象级权限,因此不需要在Django数据库中为我的用户创建行。

0 个答案:

没有答案
相关问题