如何将 OAuth 与单页应用程序集成? [英] How to integrate OAuth with a single page application?

查看:69
本文介绍了如何将 OAuth 与单页应用程序集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 OAuth (2) 时,我的应用程序中需要一个重定向端点,一旦我通过身份验证,OAuth 提供服务就可以重定向到该端点.

When using OAuth (2) I need a redirection endpoint in my application that the OAuth-offering service can redirect to, once I have been authenticated.

如何在单页应用程序中处理此问题?当然,重定向到 OAuth 提供服务在这里并不好,甚至可能无法重定向回来.

How do I handle this in a single page application? Of course, a redirect to the OAuth-offering service is not nice here, and it may not even be possible to redirect back.

我知道 OAuth 也支持基于用户名/密码的令牌生成.这与 AJAX 调用完美配合,但需要我的单页应用程序要求输入用户名和密码.

I know that OAuth also supports a username / password based token generation. This works perfectly with an AJAX call, but requires my single page application to ask for a username and password.

你通常如何处理这个问题?

How do you usually handle this?

推荐答案

在大多数情况下,即使对于 SPA,重定向也是可以的,因为用户不喜欢将他们的 X 服务凭证放在 X 之外的任何其他网站上.另一种选择将使用一个小的弹出窗口,您可以检查 Discourse 的作用.恕我直言,重定向比弹出窗口好.

Most of the time, a redirect is okay even for SPA because users don't like to put their X service credentials on any other website than X. An alternative will be to use an small popup window, you can check what Discourse does. IMHO a redirect is better than a popup.

Google 一些提供商支持 资源所有者流程,这就是您所描述的发送用户名和密码,但这并不好.这些是我看到的问题:

Google Some providers support the resource owner flow which is what you described as sending username and password, but this is not nice. These are the problems I see:

  1. 向您网站中的用户询问 Google 凭据对于某些用户来说是行不通的.
  2. 资源所有者流也需要 client_secret,这是您不能放入客户端 javascript 的内容.如果您从服务器端应用程序实例化资源所有者流,并且您的应用程序与用户所在的地理区域不同,则用户将收到警告嘿,有人正在尝试使用您的印度凭据进行访问".

OAuth 描述了一个名为 的客户端流程隐式流程.使用此流程,您不需要在服务器端进行任何交互,也不需要 client_secret.OAuth 提供程序使用#access_token=xx"重定向到您的应用程序.之所以称为隐式,是因为您无需为每个 访问令牌 交换授权码,而是直接获得一个 access_token.

OAuth describes a client-side flow called implicit flow. Using this flow you don't need any interaction in your server-side and you don't need the client_secret. The OAuth provider redirects to your application with a "#access_token=xx". It is called implicit because you don't need to exchange authorization code per access token, you get an access_token directly.

Google 实施隐式流程,请检查:为客户端应用使用 OAuth2.

Google implement the implicit flow, check: Using OAuth2 for Client-Side apps.

如果你想对一些不支持它的提供者使用隐式流,比如 Github,你可以使用像 Auth0 这样的身份验证代理.

If you want to use the implicit flow with some provider that doesn't support it like Github, you can use an authentication broker like Auth0.

免责声明:我为 Auth0 工作.

disclaimer: I work for Auth0.

这篇关于如何将 OAuth 与单页应用程序集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆