简体   繁体   English

如何验证客户端 google+ 登录信息?

[英]How do I validate a client-side google+ login?

So, following the basic tutorial here ( https://developers.google.com/+/web/signin/javascript-flow ) you can easily add a client-side login for google accounts.因此,按照此处的基本教程 ( https://developers.google.com/+/web/signin/javascript-flow ),您可以轻松地为 google 帐户添加客户端登录。

By modifying the code to query /people/me, as follows:通过修改代码查询/people/me,如下:

function signinCallback(authResult) {
  if (authResult['status']['signed_in']) {
    gapi.auth.setToken(authResult);
    gapi.client.load('plus','v1', function(){
      var request = gapi.client.plus.people.get({
        'userId': 'me'
      });
      request.execute(function(resp) {
        console.log(resp);
      });
  });
}

You can gain access to the basic account information;您可以访问基本帐户信息; user id, image, name, etc.用户 ID、图像、名称等。

You also have a valid access token from the oauth login.您还有来自 oauth 登录名的有效访问令牌。

Now, unless you have an entirely client-side application, as some point you're going to have to notify the server that the user is logged in, to get application data for that user.现在,除非您有一个完全客户端的应用程序,否则您将不得不通知服务器用户已登录,以获取该用户的应用程序数据。

What I want to do is POST to /login/gauth {'access_token': ..., 'user_id': ....} which will respond with an auth cookie and redirect back;我想要做的是 POST 到 /login/gauth {'access_token': ..., 'user_id': ....} 它将响应一个 auth cookie 并重定向回来; except now we have a persistent local auth token which we can use to identify the user.除了现在我们有一个持久的本地身份验证令牌,我们可以用它来识别用户。

On the server I need to do is access the same REST api (/people/me) using the access token, and validate the user id it returns;在服务器上,我需要做的是使用访问令牌访问相同的 REST api (/people/me),并验证它返回的用户 ID; any other provided information is forge-able on the client side.任何其他提供的信息都可以在客户端伪造。

The problem is, I can't find any way of validating/using an access token on the server side.问题是,我在服务器端找不到任何验证/使用访问令牌的方法。

I can setup a new OAuth login on the server, and do a server only login process, but that's fairly involved, and seems wasteful considering I already have a valid access token.我可以在服务器上设置一个新的 OAuth 登录,并执行仅服务器登录过程,但这相当复杂,而且考虑到我已经有一个有效的访问令牌,这似乎很浪费。

How do I use it?我如何使用它?

You probably want to use the hybrid flow that is outlined at https://developers.google.com/+/web/signin/server-side-flow .您可能想要使用https://developers.google.com/+/web/signin/server-side-flow 中概述的混合流程。 In this flow, your client does roughly the same thing it does now, but your client also gets a short-lived "code" that it should pass to the server along a secure channel.在此流程中,您的客户端执行的操作与现在大致相同,但您的客户端还会获得一个短期“代码”,它应该通过安全通道将其传递给服务器。 The server then validates this against the server to get its own access (and possibly refresh) token.然后,服务器根据服务器验证这一点,以获得自己的访问(可能还有刷新)令牌。 This is safer than the client trying to send the access_token, since it reduces the window that a man-in-the-middle can exploit it and it can only be used once, reducing the opportunity.这比客户端尝试发送 access_token 更安全,因为它减少了中间人可以利用它的窗口,并且只能使用一次,从而减少了机会。

The details in the accepted answer are correct, but there's a much easier way of doing it that is only vaguely hinted at, rather than actually stated, but it turns up in the example code if you read carefully.接受的答案中的细节是正确的,但有一种更简单的方法,它只是含糊地暗示,而不是实际陈述,但如果您仔细阅读,它会出现在示例代码中。

The key is:关键是:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=TOKEN

When TOKEN is the client side access token.当 TOKEN 是客户端访问令牌时。 This will return a json block like this:这将返回一个像这样的 json 块:

{u'access_type': u'online',
 u'audience': u'...',
 u'expires_in': 3475,
 u'issued_to': u'...', <--- The client ID; use this to verify access token
 u'user_id': u'1234567890', <--- The unique google user id 
 u'scope': u'https://www.googleapis.com/auth/userinfo.profile')

Notice that this simply validates:请注意,这只是验证:

  • the token is valid (ie. user logged in)令牌有效(即用户登录)
  • the user_id <--> access_token binding is valid (not a client side hack to be a different user) user_id <--> access_token 绑定是有效的(不是客户端黑客成为不同的用户)
  • the application id is correct应用程序 ID 是正确的

If you want to query for additional details about a user, you need to use one of the OAuth libraries.如果要查询有关用户的其他详细信息,则需要使用 OAuth 库之一。

In many cases this may be sufficient to uniquely identify the user.在许多情况下,这可能足以唯一标识用户。

You can skip making a network request to the tokeninfo endpoint by verifying the authenticity of the users JWT id_token on your server.您可以通过验证服务器上用户 JWT id_token的真实性,跳过向tokeninfo端点发出网络请求。 This can easily be done by using one of Google's API Client Libraries.这可以通过使用 Google 的 API 客户端库之一轻松完成。

This also allows you to avoid needed to request a offline access code if not needed.这也使您可以避免在不需要时请求离线访问代码。

This is the method recommended in Google'sdocumentation .这是 Google文档中推荐的方法。

Check there for more detailed instructions.检查那里以获取更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用此命名约定验证表单客户端 - How can I validate form client-side with this naming convention 如何使用服务器上的 Node 从客户端 JavaScript 上传到 Google Cloud Storage? - How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server? 如何在Javascript中为WebDB或Google Gears等客户端数据库转义数据? - How do I escape data in Javascript for a client-side database like WebDB or Google Gears? 如何将客户端属性添加到FirebaseListObservable中的项目? - How do I add client-side attributes to items in a FirebaseListObservable? 如何为 GatsbyJS 制作仅客户端的组件? - How do I make a client-side only component for GatsbyJS? 如何做高效的客户端认证? - How to do efficient client-side authentication? 实现客户端ajax登录,使用ajax注销是否安全还是需要刷新? - Implementing client-side ajax login, is it safe to logout using ajax or do I need to refresh? 如何使用只读的HTML5验证字段客户端? - How can I validate a field client-side using HTML5 that is read only? 如何在客户端验证中验证二变量规则? - How can I validate two-variable rules in client-side validation? 条纹元素:如何验证卡片元素不是空白客户端? - Stripe elements: how can I validate a card element is not blank client-side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM