如何允许移动应用使用Facebook和Google登录以访问GAE上的Web服务?

huangapple go评论76阅读模式
英文:

How to allow mobile apps to login with Facebook and Google to access web service on GAE?

问题

这与我关于是否需要创建Facebook应用程序的另一个问题有关。

我一直在阅读关于如何最好地为访问我在Google App Engine上运行的Web服务的移动应用程序用户(iOS和Android)提供登录的方法。我仍然不清楚如何最好地做到这一点,因为我希望提供使用Google和Facebook登录的选项。应用程序和Web服务除了我希望利用它们的登录功能外,与Facebook或Google无关。

只使用Google登录GAE非常容易,使用OpenIDConnect也是如此。不幸的是,Facebook不支持这一点。

阅读一个在SO上的旧问题,其中有人想做与我相同的事情,看起来应用程序应该使用Facebook登录,然后获取一个令牌,将其传递给我的后端,后端需要通过与Facebook联系来验证令牌。这是现在的做法吗?

我还发现了Google Identity Toolkit,它似乎是我需要的东西。然而,我没有网站,只有应用程序。我需要让应用程序进行Facebook登录,并以某种方式向我的Web服务提供一些内容,以便它可以验证登录信息。

稍后,应用程序用户应该能够使用随机的Facebook、Google和我的自定义用户名/密码进行登录。应用程序和Web服务应该知道用户已登录并授权其访问REST API。

我该如何实现这一点?顺便说一句,我在GAE上使用Go语言。

如果有人能够解释是否有几种选项可以实现这一点,以及各种方法的优缺点,并提供最佳方法的概述和需要完成的工作,我将非常感激。

非常感谢您对此的任何帮助!

更新

好的,非常感谢大家的帮助和指导。我已经成功运行了用于iOS的快速入门示例应用程序来连接我的GAE后端。基本上,我在GAE上创建了一个Facebook应用程序和权限凭据,以便示例iOS应用程序可以登录。

在我在数据存储中拥有经过身份验证的用户并能够授权后续API调用之前,还有一些差距。

目前的主要问题是:

  1. 在成功的Facebook或Google登录后,如何在iOS应用程序中获取gtoken?
  2. 我应该显式调用我的Web服务上的API来传递gtoken,还是这在启用Gitkit API时会自动完成?

感谢任何帮助!

更新

为了回答问题1和问题2,可以在应用引擎配置中提供一个“成功的登录URL”,以便应用程序知道在哪里使用gtoken进行调用。然后在那之后,就像答案中所解释的那样。

英文:

This is in relation to my other question about the need to create a Facebook app.

I've been reading a lot about how to best approach login for mobile apps users (iOS and Android) that access my web service running on Google App Engine. I'm still not clear how to best do it as I would like to offer login with both Google and Facebook. The app and the web service does nothing with Facebook or Google other than I would like to piggyback on their login.

Having only login with Google for GAE is very easy and the same goes for using OpenIDConnect. Facebook unfortunately does not support this.

Reading an old question here on SO where someone wanted to do the same as I it looks like the app should do Facebook Login and then get a token that it passes to my backend which needs to be validated by contacting Facebook. Is this how to do it today?

I also found Google Identity Toolkit, which seem to be what I need. However, I do not have a website or just apps. I would need to have the apps do the Facebook login and somehow provide my web service with something so it can validate the login info.

Later on an app user should be able to log in using randomly Facebook, Google and my custom username/password. The app and the web service should know the user is logged in and authorize it to access the REST API.

How do I accomplish this? BTW, I'm using Go on GAE.

I would really appreciate if someone could explain if there are several options how to do this, pros and cons, and provide an overview of the best approach and what needs to be done.

Many thanks for any help with this!

UPDATE

OK, thanks a lot everyone for the help and pointers. I have successfully run the quickstart sample app for iOS for my GAE backend. Basically, created a Facebook app and permissions credentials on my web service on GAE so that the sample iOS app can log in.

A bit of a gap still before I have an authenticated user in the datastore and can authorize successive API calls.

Main open questions at this point:

  1. how to get the gtoken in the iOS app after successful Facebook or Google login?
  2. should I explicitly call an API on my web service to pass in the gtoken or is this somehow automatic with Gitkit API enabled?

Thanks for any help!

UPDATE

To answer #1 and #2 myself, there's a "successful sign-in url" that can be given in the app engine config so the app knows where to call with the gtoken. Then after that it's like explained in the answers.

答案1

得分: 6

看起来你有一个在GAE上的应用程序和后端。
如果你正在使用Google身份工具包(Identity Toolkit),它将允许用户使用Facebook、Google和电子邮件/密码进行登录。

当用户成功使用身份工具包登录到你的应用程序时,你的服务器应该接收到一个gtoken。在这里,你有两个选择:

  1. 将gtoken传递给你的应用程序并在那里保存。当你的应用程序对后端进行API调用时,应该将gtoken附加到每个请求中。你的后端应该对每个需要授权的API验证gtoken(https://developers.google.com/identity/toolkit/web/required-endpoints)。
  2. 验证gtoken,生成一个后端可以识别/标识用户的令牌。然后将令牌传递给你的应用程序,其他步骤与选项1相同。

如果你不想使用身份工具包,你可以在你的应用程序/后端上实现Facebook登录,并使用Facebook令牌在应用程序和后端之间进行通信。

无论你做出什么决定,使用你的API的应用程序应该向你传递一些后端可以识别/授权用户的内容。

英文:

Looks like you have an app and a backend on GAE.
If you are using google identity toolkit, it will allow you to signin with Facebook, Google, and email/password.

When user successfully signs in to your app using identity toolkit, your server should receive a gtoken. You have two options here:

  1. Pass the gtoken to your app and save it there. When your app makes API calls to your backend, you app should attach the gtoken to every request. Your backend should verify the gtoken(https://developers.google.com/identity/toolkit/web/required-endpoints) for every API that needs authorization.
  2. Verify the gtoken, generate a token that your backend can recognize/identify the user. Then pass the token to your app and everything else is the same as option 1.

If you do not want to use identity toolkit, you can implement facebook login on your app/backend and use facebook token to communicate between your app and backend.

Whatever your decision is, apps that use your API should pass you something that your backend can recognize/authorize the user.

答案2

得分: 3

答案涉及使用Google Identity Toolkit(GIT)。GIT本身是一个身份提供者,可以与您的应用程序和后端集成。流程如下:

  • 您的应用程序通过其GIT API请求登录
  • GIT将与Facebook或其他第三方提供商执行联合登录(对您的应用程序透明),并将GIT令牌返回给应用程序(表示来自您的端到端系统角度的唯一用户,即应用程序加上后端)
  • 应用程序向后端发出请求,并传递GIT令牌
  • 后端验证GIT令牌的有效性(例如使用此go GIT客户端API),并从中提取用户的身份,从而验证请求

您可以在后端端点文档中找到有关后端令牌验证的更详细信息,特别是查找以下部分:

  • 了解Identity Toolkit cookie/token
  • 获取用户信息

现在,后端上的实际令牌验证可能需要几秒钟,因此对于应用程序的每个REST请求进行验证可能*不太实际。如果是这样,您需要以某种方式:

  • 在您的应用程序和后端之间管理的“会话”中保存特定应用程序实例的用户已验证信息
  • 将后端接收到的特定REST请求映射到特定的已验证“会话”

但是我不确定在应用程序+后端上下文中如何实现这种“会话”功能,因为我还没有编写过任何应用程序。

英文:

The answer is about using Google Identity Toolkit (GIT). GIT itself is an identity provider, which would be integrated with your apps and backend. The flow works along these lines:

  • your app requests login via its GIT API
  • GIT will perform the federated login with Facebook or other 3rd party provider (transparent to your app) and returns a GIT token to the app (representing a unique user from your end-to-end system perspective, i.e. apps plus backend)
  • the app makes a request to the backend in which it passes the GIT token
  • the backend verifies the GIT token validity (using this go GIT client API, for example) and from it can extract the identity of the user and thus validate the request

You can find more detailed info about the backend token validation in the backent endpoint doc, look for these sections in particular:

  • Understanding the Identity Toolkit cookie/token
  • Getting information for users

Now the actual token validation on the backend may take a few seconds, so it might not be practical to do it for each and every REST request from the app. If so you'd need to somehow:

  • save the info that the user of that specific app instance is
    authenticated in something like a "session" managed between your app and the backend
  • map a specific REST request received by the backend to a specific such authenticated "session"

But I'm not sure how exactly is this "session" functionality done in the apps+backend context, I didn't write any apps yet.

huangapple
  • 本文由 发表于 2015年8月26日 15:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/32220196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定