首页 > 解决方案 > Can Oauth be used securely from the client side for a back-end service?

问题描述

I have a web app (C# / Javascript) where I want to call a back-end service that I own. I want to secure that service so only my app(s) can call it. Seems like if I use Oauth on the client side, the secret is exposed? All the docs I read about Oauth give solutions when the user of the app owns the resource, not when the app itself owns it. I was looking at how google apis work, and the JavaScript libraries seem to expose the key on the client side, no?

标签: oauthoauth-2.0

解决方案


是的,您可以使用 OAuth 完成此操作,但您如何执行此操作取决于如何组织通过后端服务访问的数据的详细信息。如果数据实际上是特定于用户的,那么您的用户确实是资源所有者。在这种情况下,您将使用典型的授权代码授予。使用此授权类型,您可以向 OAuth 身份验证服务器注册客户端并接收 client_id 和 client_secret。client_id 确实在浏览器中公开,但 client_secret 位于您的 Web 应用程序服务器中并且永远不会公开。它仅通过反向通道(非浏览器)请求发送到您的身份验证服务器。这里的主要内容是只有您自己的客户端 Web 应用程序会被注册并接收 client_id/client_secret。您只需要不提供任何公共注册端点,因此其他客户端无法注册。使用这种授权类型,

另一方面,如果您在后端服务上访问的数据不是特定于用户的,那么您可以使用 OAuth 客户端凭据授权。使用这种授权类型,您可以像以前一样注册客户端并接收 client_id 和 client_secret。该密钥安全地存储在您的 Web 应用服务器上,并且仅在反向通道请求中传递。您将避免允许任何其他客户注册。然后,您的 Web 应用程序可以获得对后端服务的授权,甚至不需要浏览器中的任何用户授权。


推荐阅读