GAE:将用户从用户服务转换为 oauth2
GAE: converting users from users service to oauth2
我目前使用 "Google Accounts API" 允许用户登录我的 GAE 应用程序。所以我使用 users.create_login_url 和 users.get_current_user 并将 ndb.UserProperty 添加到我自己的用户实体,以便我可以检索该用户的数据。
我现在正在切换到 oauth2(使用自动)。
我需要将我现有的所有用户帐户转换为 oauth2,并且我希望这对我的用户来说尽可能简单。这是我目前的计划:
将登录从用户服务更改为 oauth2。
用户登录后,看起来就像一个新帐户,用户将看不到他或她以前的数据。
我会添加一条醒目的消息,要求用户使用旧用户服务登录。
然后我会将旧用户服务帐户与 oauth2 帐户合并。
这应该可行,但会让用户有点困惑。有更好的方法吗?
我将解释我最终是如何做到这一点的,以防它对其他人有所帮助。
我打电话给我的用户经理,每个用户都有一个经理实体:
class Manager(ndb.Model):
user_account = ndb.StructuredProperty(UserAccount))
linked = ndb.BooleanProperty(default=False)
user = ndb.UserProperty()
user
属性是老用户服务账号,我要去掉。 user_account
属性 存储用于识别 Oauth2 帐户的信息:
class UserAccount(ndb.Model):
provider = ndb.StringProperty(required=True)
id = ndb.StringProperty(required=True)
name = ndb.StringProperty()
email = ndb.StringProperty()
本质上,对于每个经理,我想为 user_account
(Oauth2 登录)设置一个值并删除 user
(旧用户帐户)。我想以最小的负担来做到这一点。
当用户最近使用旧用户帐户登录时,该 cookie 仍将处于活动状态。但是,现在用户正在使用 Oauth2 帐户登录。使用 Oauth2 登录后,我们检查旧用户帐户 cookie 是否仍然有效。如果是这样,我们会自动合并帐户。这是处理程序的草图。
class ManagerPage(webapp2.RequestHandler):
def get(self):
# This returns a Manager entity after the user has logged in with
# Oauth2. If the user is logging in for the first time, this will
# be a blank Manager entity.
self.get_manager()
# Temporary processing to link accounts. If the user is still logged
# as a Google user (because that cookie hasn't expired), then we
# automatically transfer their old information to the new Manager
# entity. In doing the conversion below, manager.linked is set to
# True so this can't happen more than once. Now that the Manager
# entity has been updated, redirect back to the same page.
gae_user = users.get_current_user()
if not manager.linked and gae_user:
manager.convert_old_manager(gae_user)
self.redirect("/manager")
# Present info to the manager
...
template = JINJA_ENVIRONMENT.get_template("manager.html")
self.response.write(template.render(template_values))
如果旧用户帐户 cookie 未激活,那么我在上面的管理器页面中有一个 link,要求用户使用新帐户 link 旧帐户。当用户使用旧帐户登录时,他们将被重定向到上面的管理器页面,并且该帐户会自动 linked.
我目前使用 "Google Accounts API" 允许用户登录我的 GAE 应用程序。所以我使用 users.create_login_url 和 users.get_current_user 并将 ndb.UserProperty 添加到我自己的用户实体,以便我可以检索该用户的数据。
我现在正在切换到 oauth2(使用自动)。
我需要将我现有的所有用户帐户转换为 oauth2,并且我希望这对我的用户来说尽可能简单。这是我目前的计划:
将登录从用户服务更改为 oauth2。
用户登录后,看起来就像一个新帐户,用户将看不到他或她以前的数据。
我会添加一条醒目的消息,要求用户使用旧用户服务登录。
然后我会将旧用户服务帐户与 oauth2 帐户合并。
这应该可行,但会让用户有点困惑。有更好的方法吗?
我将解释我最终是如何做到这一点的,以防它对其他人有所帮助。
我打电话给我的用户经理,每个用户都有一个经理实体:
class Manager(ndb.Model):
user_account = ndb.StructuredProperty(UserAccount))
linked = ndb.BooleanProperty(default=False)
user = ndb.UserProperty()
user
属性是老用户服务账号,我要去掉。 user_account
属性 存储用于识别 Oauth2 帐户的信息:
class UserAccount(ndb.Model):
provider = ndb.StringProperty(required=True)
id = ndb.StringProperty(required=True)
name = ndb.StringProperty()
email = ndb.StringProperty()
本质上,对于每个经理,我想为 user_account
(Oauth2 登录)设置一个值并删除 user
(旧用户帐户)。我想以最小的负担来做到这一点。
当用户最近使用旧用户帐户登录时,该 cookie 仍将处于活动状态。但是,现在用户正在使用 Oauth2 帐户登录。使用 Oauth2 登录后,我们检查旧用户帐户 cookie 是否仍然有效。如果是这样,我们会自动合并帐户。这是处理程序的草图。
class ManagerPage(webapp2.RequestHandler):
def get(self):
# This returns a Manager entity after the user has logged in with
# Oauth2. If the user is logging in for the first time, this will
# be a blank Manager entity.
self.get_manager()
# Temporary processing to link accounts. If the user is still logged
# as a Google user (because that cookie hasn't expired), then we
# automatically transfer their old information to the new Manager
# entity. In doing the conversion below, manager.linked is set to
# True so this can't happen more than once. Now that the Manager
# entity has been updated, redirect back to the same page.
gae_user = users.get_current_user()
if not manager.linked and gae_user:
manager.convert_old_manager(gae_user)
self.redirect("/manager")
# Present info to the manager
...
template = JINJA_ENVIRONMENT.get_template("manager.html")
self.response.write(template.render(template_values))
如果旧用户帐户 cookie 未激活,那么我在上面的管理器页面中有一个 link,要求用户使用新帐户 link 旧帐户。当用户使用旧帐户登录时,他们将被重定向到上面的管理器页面,并且该帐户会自动 linked.