javascript PhoneGap 和 OAuth2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9791138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:51:44  来源:igfitidea点击:

PhoneGap and OAuth2

javascriptcordovaoauth-2.0

提问by whirlwin

I am developing a PhoneGap application and require my users to sign in through Google with OAuth2. Is this possible directly through JavaScript? I have almost no experience with JavaScript (and jQuery).

我正在开发 PhoneGap 应用程序并要求我的用户使用 OAuth2 通过 Google 登录。这可以直接通过 JavaScript 实现吗?我几乎没有使用 JavaScript(和 jQuery)的经验。

What are the options? I have thought of a cumbersome alternative which involves posting the username/password to an application I have hosted on a server which then takes care of logging in. Then the PhoneGap app will have to ask the server if the authentication was successful. However, I was hoping there would be a simpler way.

有什么选择?我想到了一个繁琐的替代方案,它涉及将用户名/密码发布到我托管在服务器上的应用程序,然后负责登录。然后 PhoneGap 应用程序将不得不询问服务器身份验证是否成功。但是,我希望有一种更简单的方法。

What is the best way signing in through Google on a PhoneGap app?

在 PhoneGap 应用上通过 Google 登录的最佳方式是什么?

回答by whirlwin

I have managed to get it working! I'm posting my thoughts here because I had a hard time finding the answer after hours of searching the web.

我已经设法让它工作了!我在这里发表我的想法是因为我在网上搜索了几个小时后很难找到答案。

The important steps are:

重要的步骤是:

  • Make sure ChildBrowser works properly
  • Setup a function that will listen to page changes window.plugins.childBrowser.onLocationChange = function(fooUrl) { console.log(fooUrl); }
  • Build the URL with the query string as described in this tutorial
  • Point ChildBrowser to the URL
  • When the user logs in, you will be able to extract the session token from fooUrl
  • 确保 ChildBrowser 正常工作
  • 设置一个监听页面变化的函数 window.plugins.childBrowser.onLocationChange = function(fooUrl) { console.log(fooUrl); }
  • 本教程中所述,使用查询字符串构建 URL
  • 将 ChildBrowser 指向 URL
  • 当用户登录时,您将能够从中提取会话令牌 fooUrl

If you still don't know how to do this, have a look at this Android app. (There is a lotof code, and it might seem overwhelming, so I suggest only going for this as a last resort)

如果您仍然不知道如何执行此操作,请查看此 Android 应用程序。(有很多代码,看起来可能会让人不知所措,所以我建议只把它作为最后的手段)

回答by Andreas ?kre Solberg

Google will not allow you to perform direct authentication by handling the user credentials directly. Instead Google wants you to perform an authentication protocol, typically OAuth 2.0. Other popular authentication protocols you may hear about is OpenID 1.0, 2.0, OpenID Connect, SAML 2.0, ID-FF, etc. These protocols will redirect the user to the Identity Provider (Google, in this case), and send you back with an assertionthat you may use to trust the user. With APIs, like Google, you would make use of the authorization functionality of OAuth, which provides you with a token that you may use with all Google APIs after authentication.

Google 不允许您通过直接处理用户凭据来执行直接身份验证。相反,Google 希望您执行身份验证协议,通常是 OAuth 2.0。您可能听说过的其他流行身份验证协议是 OpenID 1.0、2.0、OpenID Connect、SAML 2.0、ID-FF 等。这些协议会将用户重定向到身份提供者(在本例中为 Google),并向您发送回您可以用来信任用户的断言。对于 API,例如 Google,您可以利用 OAuth 的授权功能,它为您提供一个令牌,您可以在身份验证后与所有 Google API 一起使用。

With PhoneGap and mobile apps, things are a bit different than the typical OAuth setup.

对于 PhoneGap 和移动应用程序,情况与典型的 OAuth 设置略有不同。

In your case, the browseris in a controlled environment, your app, and you may

在您的情况下,浏览器处于受控环境中,您的应用程序,您可以

  • select to redirect the user to Google Authorization endpoint using the main view,
  • select to open a ChildBrowserwith the Google Authorization endpoint, to not lose any state on your app.
  • to somehow open Safari or another browser with the authorization endpoint, and register a custom schema handler, to redirect the user back to your app after authentication.
  • 选择使用主视图将用户重定向到 Google 授权端点,
  • 选择使用 Google 授权端点打开ChildBrowser,以免在您的应用程序上丢失任何状态。
  • 以某种方式使用授权端点打开 Safari 或其他浏览器,并注册自定义架构处理程序,以在身份验证后将用户重定向回您的应用程序。

These examples are vaguely mentioned in the OAuth 2.0 specifications, but there are no aid in what is the best or optimal in a specific use case. Often the best possible option is not perfect (from a user perspective).

OAuth 2.0 规范中含糊地提到了这些示例,但没有帮助说明在特定用例中什么是最好的或最优的。通常最好的选择并不完美(从用户的角度来看)。

I recently wrote a tutorial on how to make this work with Phonegap and ChildBrowser for iOS.

我最近写了一篇关于如何使用 iOS 的 Phonegap 和 ChildBrowser 进行这项工作的教程。