如何在 Django 中发送带有用户 unique_id 和令牌的 url?

How can I send url with user's unique_id and token in Django?

我为大学创建了一个 Django 应用程序 ("Invigilator Management System")。我没有包括任何登录授权,因为它只会被大学管理员使用。在这个应用程序中,我还有一项任务,我必须通过邮件向数据库中的所有用户发送可用性表格,要求他们根据我必须存储的回复填写表格,无论他们当天是否有空他们在数据库中的可用性状态。如何通过邮件发送表格? 或者我应该给他们发一个 link 表格吗? 如果我必须发送 link,我怎样才能阻止一个用户通过更改他们得到的 link 来访问其他用户的表单,知道如何添加令牌以及他们的唯一 ID 吗?

发送带有唯一令牌的电子邮件。

生成令牌:

使用用户字段作为盐生成随机令牌。 可以是任何一个或多个字段,如名字、姓氏、用户 ID 等。 好像你的用户群不多,重复token的概率比较小。

如何生成令牌?

有很多包可以帮助您生成随机令牌。 根据我的经验,我发现 uuidsecretshashlib 很有帮助,当然这些都是 python 内置库。

示例:

# recommended, uuid guarantees uniqueness.

>>> import uuid
>>> print(uuid.uuid4().hex)
'772d4c80-3668-4980-a014-aae59e34b9b9'

# others way, some sort of salt combination

>>> import secrets, hashlib
>>> first_name = user.first_name  # pull user first name, a salt.
>>> salt = secrets.token_hex(8) + first_name
>>> hashlib.sha256(salt.encode('utf-8')).hexdigest()
'f8b2e14fe3496de336017687089fb3c49bcab889ac80545fc087289c5b1a3850'

生成随机令牌非常简单,接下来。

现在将您的随机令牌存储在您的数据库中,具体取决于所使用的令牌生成方法,我们可能很少会出现重复令牌。确保定期清理过期令牌。

当你使用盐组合方法时,我建议你检查令牌是否存在并重新生成。

唯一代币模型示例:

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class UserUniqueToken(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE)
    token = models.CharField(max_length=100)
    datetime = models.DateField(default=timezone.now)  # for token expiration

如何检查令牌有效性?

views.py:

from .models import UserUniqueToken
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from django.utils import timezone
from datetime import timedelta

@login_required
def user_form(request, token):
    user_token = get_object_or_404(UserUniqueToken, token=token)  # get object or throw 404
    if not user_token.user_id == request.user.id:  # check token belongs to the user 
        # token does not belongs to the current user, do something 
    time_now = timezone.now()  # current time
    if user_token.datetime > (time_now - timedelta(hours=2)):  # check if stored time exceeds 2 hours
          # do something with expired token here.
    return render(.............)  # successful, render or throw success response

urls.py:

from .views import user_form

urlpatterns = [
        path('form/<string:token>', user_form, name='user-form'),
]

您也可以考虑以天为间隔删除过期的令牌,因为过期的令牌会随着时间的推移而过时。