GAE:将用户从用户服务转换为oauth2

时间:2015-08-25 13:42:57

标签: python google-app-engine oauth-2.0 google-oauth

我目前使用" Google Accounts API"允许用户登录我的GAE应用。所以我使用users.create_login_url和users.get_current_user并将ndb.UserProperty添加到我自己的用户实体,以便我可以为该用户检索数据。

我现在正在切换到oauth2(使用authomatic)。

我需要将所有现有用户帐户转换为oauth2,并且我希望为用户提供尽可能简单的功能。这是我目前的计划:

  1. 将用户服务的登录名更改为oauth2。

  2. 用户登录后,它看起来像一个新帐户,用户将看不到他或她以前的数据。

  3. 我会添加一条醒目的消息,要求用户使用旧用户服务登录。

  4. 然后我将旧用户服务帐户与oauth2帐户合并。

  5. 这应该可行,但对用户来说会有点混乱。有没有更好的方法呢?

1 个答案:

答案 0 :(得分:1)

我将解释我最终是如何帮助他人的。

我打电话给我的用户经理,每个用户都有一个Manager实体:

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未处于活动状态,则我在上述管理器页面中有一个链接,要求用户将旧帐户与新帐户相关联。当用户使用旧帐户登录时,会将其重定向到上述管理员页面,并自动链接该帐户。