我正在构建自定义帐户系统,当用户创建帐户时,我将其保存到数据存储中,但“已验证”字段默认设置为 false(在其已验证字段设置为 true 之前,无法使用帐户登录。)
class Account(ndb.Model):
username = ndb.StringProperty(required = True)
password = ndb.StringProperty(required = True)
email = ndb.StringProperty(required = True)
verified = ndb.BooleanProperty(default = False)
created = ndb.DateTimeProperty(auto_now_add = True)
我的应用程序中的“用户名”应该是唯一且重要的。我将实体的“id”设置为用户名,然后使用 get_by_id 方法在所有其他操作中获取帐户。
acc = Account( id=username,
username = username,
password = password,
email = email)
当我保存帐户时,我使用 uslsafe 将其 key 转换为字符串。
urlString = rev_key.urlsafe()
我使用此字符串构建 URL 并将其邮寄给用户,稍后根据该 URL 的请求,使用 uslString 构建我的 key 并将“已验证”字段设置为 true。
但这会暴露我的实际 key ,并且可能会暴露用户名。我对此有几个问题。
- key 如何存在于数据存储中。如果有人拥有“urlString”,任何人都可以构造我的 key 并获取我的实体吗?或者实体只能由创建它们的应用程序访问?
- 如何更安全地执行此操作?
- 我们可以在发送之前对字符串进行哈希处理。但是如何从中恢复我的 key 呢?我是否应该将哈希本身与其他字段一起保存并使用它来获取帐户进行身份验证。
- 哪种哈希方案适合于此?
最佳答案
这些 key 仅适用于您的应用程序,app-id
实际上是完整 key 的一部分;但正如您想象的那样,任何人都可以为不同的用户名构建 key 并验证它们,即使他们没有收到电子邮件。正如 @Paul 的评论中所指出的,您不应该相信 key 没有被篡改。
我建议您单独处理验证,例如:
import hashlib
import os
class Verification(ndb.Model):
account = ndb.KeyProperty()
@classmethod
def create(cls, account):
verification = cls(
id=hashlib.sha1(os.urandom(16)).hexdigest(),
account=account.key,
).put()
return verification.id()
@classmethod
def validate(cls, verification_id):
verification = cls.get_by_id(verification_id)
account = verification.account.get()
account.verified = True
account.put()
我们在这里所做的是创建一个不同的模型 Verification 来处理此功能;创建后,该实体会获得一个模糊的 ID,然后您可以使用它来验证正确的帐户。您可以在此基础上处理特定于应用程序的功能。
关于python - 使用电子邮件验证用户帐户 (Google App Engine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28727035/