Django - 没有模型的登录用户

Pro*_*e85 7 python django django-authentication

我正在尝试实现 SSO 登录,从 saml 响应中获取所有授权权限:

class SAMLServiceProviderBackend(object):

    def authenticate(self, saml_authentication=None):
        if not saml_authentication:  # Using another authentication method
            return None

        if saml_authentication.is_authenticated():
            attributes = saml_authentication.get_attributes()
            user = User(username=saml_authentication.get_nameid())
            user.first_name = attributes['givenName']
            user.last_name = attributes['sn']

        return None
Run Code Online (Sandbox Code Playgroud)

在视图中我得到了类似的东西

...
user = authenticate(saml_authentication=auth)
login(self.request, user)
...
Run Code Online (Sandbox Code Playgroud)

login由于缺少save()方法而失败。唯一的方法是继承User并重写该save方法。尝试这个,我得到了下一个错误is_authenticatedget_and_delete_messages等等

有没有一种简单的方法可以将用户对象插入到会话中,而不将用户保存到数据库中?

就像是:

request.session['user'] = authenticate(saml_authentication=auth)
Run Code Online (Sandbox Code Playgroud)

Jam*_*Lin 1

我想应该有一些限制,例如。如果用户是 FK,则无法保存数据。

我自己尝试过,我怀疑您可以在方法中动态创建用户实例,authenticate()只是不要调用user.save(),或者覆盖该save()方法而不执行任何操作。

您可能还需要在请求之间连接用户记录,因此您可能需要为用户编写自己的序列化程序,并从会话中加载构造用户实例。