我有一个 iOS,它使用 OAuth 和 OAuth2 提供程序(Facebook、google、twitter 等)来验证用户并提供访问 token 。除了姓名和电子邮件地址等最少数据外,该应用不会将这些服务用于身份验证之外的任何事情。
然后应用程序将访问 token 发送到服务器以指示用户已通过身份验证。
服务器是用 Node.js 编写的,在执行任何操作之前,它需要根据正确的 OAuth* 服务验证提供的访问 token 。
我一直在环顾四周,但到目前为止,我发现的所有 node.js 身份验证模块似乎都是用于通过服务器提供的网页进行登录和身份验证的。
有谁知道任何可以对提供的访问 token 进行简单验证的 node.js 模块?
最佳答案
据我所知(并且据我通过阅读规范可知),OAuth 和 OAuth 2 规范没有为访问 token 验证指定单个端点。这意味着您将需要为每个提供者自定义代码来仅验证访问 token 。
我查找了如何处理您指定的端点:
好像others已使用图形 API 的 'me' endpoint让 Facebook 检查 token 是否有效。基本上,请求:
https://graph.facebook.com/me?access_token={accessToken}
谷歌
Google 有一个 dedicated debugging endpoint用于获取访问 token 信息,使用 nice documentation , 也。基本上,请求:
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={accessToken}
但是,他们建议您不要在生产环境中这样做:
The
tokeninfo
endpoint is useful for debugging but for production purposes, retrieve Google's public keys from the keys endpoint and perform the validation locally. You should retrieve the keys URI from the Discovery document using the jwks_uri metadata value. Requests to the debugging endpoint may be throttled or otherwise subject to intermittent errors.Since Google changes its public keys only infrequently, you can cache them using the cache directives of the HTTP response and, in the vast majority of cases, perform local validation much more efficiently than by using the tokeninfo endpoint. This validation requires retrieving and parsing certificates, and making the appropriate cryptographic calls to check the signature. Fortunately, there are well-debugged libraries available in a wide variety of languages to accomplish this (see jwt.io).
推特
Twitter 似乎没有一个非常明显的方法来做到这一点。我怀疑是因为 account settings data是相当静态的,这可能是最好的验证方式(获取推文可能会有更高的延迟?),因此您可以请求(使用适当的 OAuth 签名等):
https://api.twitter.com/1.1/account/settings.json
请注意,此 API 的速率限制为每个窗口 15 次。
总而言之,这似乎比最初看起来更棘手。在服务器上实现某种 session /身份验证支持可能是一个更好的主意。基本上,您可以验证一次获得的外部 OAuth token ,然后为用户分配一些您自己的 session token ,您使用该 token 在您自己的服务器上使用用户 ID(电子邮件、FB id 等)进行身份验证,而不是继续制作为您自己收到的每个请求向 OAuth 提供者发出请求。
希望有帮助!
关于node.js - iOS & node.js : how to verify passed access token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894506/