Django 补充外源认证

Django complementary external source authentication

我正在尝试建立一个主要由大学生维护和使用的Django 网站。我需要限制某些获得批准的学生访问几个页面,但如果我需要为每个想要登录的学生创建一个新的 Django 用户,这将是非常难以维护的。幸运的是,大学提供了一个 API 来检查 username/password 组合是否正确。所以我有了创建一个与 Django 模型互补的身份验证模型的想法,其中用户的大学帐户可以得到管理员的批准,之后它是查看某些页面的有效登录。

所以基本上,一些用户可能会使用 Django 帐户(如果他们负责网站的内容),而其他用户可能只是使用他们的 uni 帐户登录以查看某些页面。对于 uni 帐户,应该存储最少的信息量(换句话说,只有用户名才能批准某些用户)。

我似乎无法弄清楚如何在 Django 中构建这样一个系统。我无法使用 User object because it stores data that is completely redundant, and I cannot substitute the user model because that would only make things incredibly complex. It seems reasonable to forget the User model altogether, but Authenticate 需要 return 有效用户的标准。这让我想知道,我是否可以使用尽可能少的信息(除了用户名之外的虚拟数据)创建常规 Django 用户,然后使用 API 对他们进行身份验证?可能吧,但这似乎不是个好主意。

要使用大学 API 对用户进行身份验证,您只需编写 an authentication backend。然后,您可以在这些 un​​i 用户首次登录时为其创建一个本地用户,因为只有两个必填字段:用户名和密码。您可以使用 set_unusable_password(),因此此用户的 check_password() 永远不会 return 正确。

The Django admin system is tightly coupled to the Django User object described at the beginning of this document. For now, the best way to deal with this is to create a Django User object for each user that exists for your backend (e.g., in your LDAP directory, your external SQL database, etc.) You can either write a script to do this in advance, or your authenticate method can do it the first time a user logs in.