简体   繁体   English

如何使用 oauth2 静默登录用户?

[英]How to login an user silently with oauth2?

I am building a web app for a booking platform and I am using oauth2 to use their API and authentificate users.我正在为预订平台构建一个网络应用程序,我正在使用 oauth2 来使用他们的API并验证用户。 My app is embedded in an other website named here 'X' that I am not in control.我的应用程序嵌入在此处名为“X”的其他网站中,我无法控制。 When the user click on my app, it loads https://localhost/?_account_id=12 in an iframe where _account_id=12 is its own account id in X website.当用户单击我的应用程序时,它会在 iframe 中加载https://localhost/?_account_id=12 ,其中_account_id=12是它自己在 X 网站中的帐户 ID。

  • I am using the oauth2 authorization code flow so the user approves my app.我正在使用 oauth2授权代码流,以便用户批准我的应用程序。
  • I am storing securely the refresh token provided by the authorization server.我正在安全地存储授权服务器提供的刷新令牌。
  • I am storing in session the account_id parameter.我在会话中存储account_id参数。
  • The authorization server doesn't support the famous prompt=none .授权服务器不支持著名的prompt=none
  • This is the first time I use oauth2 so maybe I didn't understand correctly how oauth2 works.这是我第一次使用 oauth2,所以也许我没有正确理解 oauth2 的工作原理。

Since I can't only rely on the _account_id parameter because of security, I need to authentificate the user each time it connects to my app.由于_account_id安全考虑,我不能只依赖_account_id参数,因此每次连接到我的应用程序时,我都需要对用户进行身份验证。 Doing so, each time the session expires, the app prompted the user to approve my app.这样做,每次会话到期时,应用程序都会提示用户批准我的应用程序。

With other apps available in X, I am never prompted to re-approve the apps.对于 X 中可用的其他应用程序,我从未被提示重新批准这些应用程序。 So when analysing them, the network developer tool of my browser lists this each time I connect to other apps :所以在分析它们时,我浏览器的网络开发工具每次连接到其他应用程序时都会列出这个:

204 https://other_app_provider/fr/admin?_account_id=12 204 https://other_app_provider/fr/admin?_account_id=12

302 https://other_app_provider/auth/?_account_id=12 302 https://other_app_provider/auth/?_account_id=12

302 https://authorization_server/oauth/authorize?_account_id=12&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&scope={SCOPES}&state={STATE} 302 https://authorization_server/oauth/authorize?_account_id=12&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&scope={SCOPES}&state={STATE}

302 https://other_app_provider/auth/bookingsync/callback?code={CODE}&state={STATE} 302 https://other_app_provider/auth/bookingsync/callback?code={CODE}&state={STATE}

200 https://other_app_provider/admin 200 https://other_app_provider/admin

I repeat that there is no approval screen between the authorization and the callback.我再说一遍,授权和回调之间没有批准屏幕。

My question : How to authentificate the user silently using the authorization code flow or the implicit flow ?我的问题:如何使用授权代码流隐式流静默验证用户? In other terms, how to imitate the behavior described above ?换句话说,如何模仿上述行为?

Thank you for answering !谢谢你的回答!

Could I ask a few questions first - to see if I'm understanding the scenario:我可以先问几个问题 - 看看我是否理解这个场景:

  • Does the user login to the host app X first and your app then gets loaded as a plug-in?用户是否先登录主机应用程序 X,然后您的应用程序作为插件加载?

  • Is this a federation scenario where different companies build the host and plug-in?这是不同公司构建主机和插件的联合方案吗?

  • Do the host and plug-in use the same authorization server or are they different login systems with different user credentials.主机和插件是否使用相同的授权服务器,或者它们是具有不同用户凭据的不同登录系统。

Avoiding the consent prompt may be as simple as switching this setting off in the OAuth Client entry for your app in the Authorizatiom Server避免同意提示可能就像在授权服务器中的应用程序的 OAuth 客户端条目中关闭此设置一样简单

It is worth being aware that OAuth logins in an iframe are often blocked due toclickjacking concerns and a more reliable option is to use a pop-up window for plug-in logins.值得注意的是,iframe 中的 OAuth 登录经常因点击劫持问题而被阻止,更可靠的选择是使用弹出窗口进行插件登录。

In some scenarios it can be very hard or impossible to achieve the desired behaviour of making the solution feel like a single integrated UI - but your requirement is becoming very common.在某些情况下,很难或不可能实现使解决方案感觉像单个集成 UI 的所需行为 - 但您的需求变得非常普遍。

I've found the solution :我找到了解决方案:

There was an option account_id in X authorization server not well documented : X 授权服务器中有一个选项account_id没有很好地记录:

Pre-Select The Account To Authorize预先选择要授权的帐户

When calling your Admin URL, we pass the parameter _account_id .在调用您的管理 URL 时,我们传递参数_account_id The value of this parameter can be used as the account_id parameter during the authorization process.该参数的值可以在授权过程中作为account_id参数使用。 Doing so will pre-select the account to authorize when using the Authorization Code Flow or Implicit Flow.这样做将在使用授权代码流或隐式流时预先选择要授权的帐户。

So I can imitate the behavior described in the question by passing account_id parameter in the authorization url.所以我可以通过在授权 url 中传递account_id参数来模仿问题中描述的行为。

Now, I understand the term Pre-Select by :现在,我通过以下方式理解“预选”一词:

Check if the account owner is connected to the host app, select its account and silently authentificate the account owner in the app, if it's an already approved account.检查帐户所有者是否已连接到主机应用程序,选择其帐户并在应用程序中静默验证帐户所有者(如果它是已批准的帐户)。

If I didn't see it in the network developer tool, it was because they were provided by X, so maybe X use cookie/session to Pre-Select the account to authorize.如果我在网络开发者工具里没有看到,那是因为它们是由X提供的,所以也许X使用cookie/session来预选账户进行授权。 I had to install third-party apps to see the account_id parameter in the authorization url.我必须安装第三方应用程序才能看到授权 url 中的account_id参数。

Thank you for your answer !谢谢您的回答 !

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM