如何让AccountManager(authtoken)和OpenID一起工作(没有AppEngine)?

发布于 2024-11-14 20:01:23 字数 426 浏览 2 评论 0原文

我正在制作一个 Android 应用程序,它应该能够从网络服务(不属于 GAE 的一部分)获取数据。用户可以使用 OpenId 通过浏览器登录 Web 服务(仅允许 Google 帐户)。

AccountManager 可以给我 authtoken。我可以将此 authtoken 与用户的 google 帐户名(电子邮件)一起保存在我的服务器上,然后使用此帐户名将他的 openid 登录与应用程序注册连接起来。

但这并不能解决任何问题,因为我无法根据用户的 OpenID 信息验证此令牌...或者我可以吗?我想我可以使用用户的身份验证令牌“以某种方式”将他的 Android 帐户链接到网络帐户。

这看起来越来越像是一种错误的处理方式,但我不想在 SharedPreferences 中保存用户 Google 的用户名/密码并使用这些数据进行登录。

有什么创意吗?谢谢

I am making an Android app which should be able to get data from a web service (which is not part of GAE). Users are able to log in to web service through their browser by using OpenId (only Google accounts are allowed).

AccountManager can give me authtoken. I could save this authtoken on my server, together with user's google account name (email) and then use this account name to connect his openid login with app registration.

But this does not solve anything because I have no way to verify this token against user's OpenID information... Or do I? I thought I could use user's authtoken to "somehow" link his android account to the web account.

This looks more and more like a wrong way to handle this but I do not want to save users Google's username/password in SharedPreferences and use these data for login.

Any creative ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-11-21 20:01:23

我为我的应用程序推送操作解决了这个问题 - http://www.pushactions.com。对于我的解决方案,我最终使用了 GAE,但只是为了验证令牌。实际的 Push Actions 应用程序托管在 Heroku 上。这是我的流程:

  1. Android 应用程序生成 GAE 令牌,然后发布令牌并发送给 GAE 令牌。帐户的电子邮件
    运行推送操作的地址
    Heroku
  2. 推送操作将令牌发布到我在 GAE 上运行的服务
  3. GAE 服务获取令牌,确定其是否有效,并返回对
  4. 推送操作有效的电子邮件地址 将 GAE 返回的电子邮件地址与 Android 中的电子邮件地址进行比较app,如果它们匹配,则令牌对于该电子邮件地址有效。这意味着该令牌可以安全地与 google 用户的 openid 电子邮件地址关联。

据我所知,这是验证 AccountManager 生成的令牌是否有效的唯一方法。确实,它确实需要使用 GAE,但仅适用于整个应用程序的一小部分。我的 GAE 服务最终就像 1 个类和几行代码,所以并不多。您可以将 GAE 部分视为 google 提供的用于验证令牌的服务。

以下是我的 GAE 服务的代码:

package com.pushactions;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class PushActionsAuthServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(HttpServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user != null) {
            req.setAttribute("user", user);
            try {
                req.getRequestDispatcher("/logged_in.jsp").forward(req, resp);
            } catch (ServletException e) {
                log.severe("Couldn't redirect to logged_in.jsp");
            }
        } else {
            try {
                req.getRequestDispatcher("/logged_out.jsp").forward(req, resp);
            } catch (ServletException e) {
                log.severe("Couldn't redirect to logged_out.jsp");
            }
        }
    }
}

logged_in.jsp:

<%@ page import="com.google.appengine.api.users.User" %>
<% User user = (User)request.getAttribute("user"); %>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>ok</status>
<user_name><%= user.getEmail() %></user_name>
</result>

logged_out.jsp:

<%@ page import="com.google.appengine.api.users.User" %>
<% User user = (User)request.getAttribute("user"); %>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>error</status>
<message>not logged in</message>
</result>

I solved this exact issue for my app Push Actions - http://www.pushactions.com. For my solution, I did end up using GAE, but only to authenticate the token. The actual Push Actions app is hosted on Heroku. Here's my flow:

  1. Android app generates GAE token then posts the token & the account's email
    address to Push Actions running on
    Heroku
  2. Push Actions posts the token to my service running on GAE
  3. The GAE service takes the token, determines if it is valid, and returns the email address it is valid for
  4. Push Actions compares the email address returned by GAE with the email address from the android app, if they match, the token is valid for that email address. This means the token can be safely associated with the google user's openid email address.

As far as I know, this is the ONLY way to verify if a token generated by AccountManager is valid. True, it does require the use of GAE, but only for one small piece of your overall app. My GAE service ended up being like 1 class and a few lines of code, so it isn't much. You can think of the GAE piece as a service provided by google for authenticating tokens.

Here is the code for my GAE service:

package com.pushactions;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class PushActionsAuthServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(HttpServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user != null) {
            req.setAttribute("user", user);
            try {
                req.getRequestDispatcher("/logged_in.jsp").forward(req, resp);
            } catch (ServletException e) {
                log.severe("Couldn't redirect to logged_in.jsp");
            }
        } else {
            try {
                req.getRequestDispatcher("/logged_out.jsp").forward(req, resp);
            } catch (ServletException e) {
                log.severe("Couldn't redirect to logged_out.jsp");
            }
        }
    }
}

logged_in.jsp:

<%@ page import="com.google.appengine.api.users.User" %>
<% User user = (User)request.getAttribute("user"); %>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>ok</status>
<user_name><%= user.getEmail() %></user_name>
</result>

logged_out.jsp:

<%@ page import="com.google.appengine.api.users.User" %>
<% User user = (User)request.getAttribute("user"); %>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>error</status>
<message>not logged in</message>
</result>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文