我可以从其他网站呈现表单吗
我在网站 A 上有一个用户,我需要让他登录到网站 B(不在我的控制之下),但又不能危及他在网站 B 上的密码。网站 B 没有 API,这使得事情变得比应有的更加复杂。
我的第一个选择是在网站 A 上呈现我自己的表单,用户在我的表单中输入他的网站 B 密码,然后我以某种方式安全地将他的网站 B 密码传递到网站 B 以让他登录。这意味着我必须首先安全地传递密码从客户端到我的服务器,然后再次将其安全地从我的服务器传递到最终网站。这就是我猜我必须做的,但我没有计划如何安全地实现这两个跃点,所以我担心我可能会以某种方式暴露用户的密码。
所以我想到了第二种选择,即将同一个网站B从网站B渲染到我的网站上。但是B网站上的表单是一个更大页面的一部分,那么这样可以吗?
- 的整页代码隔离开来
- 我如何将表单本身的代码与如何在我的网站上呈现表单 。我希望用户看到它。我想到了 iframe,但从未使用过它
- ,当用户单击提交按钮时,表单仍然有效,或者在我的网站上渲染表单以某种方式使其无效,
这是我想到的两种不同的解决方案。我欢迎他们每个人的答案,也欢迎提出可能更容易的替代第三种方法的答案。
I have a user on website A and I need to log him to website B (not under my control) but without jeopardizing his password on website B. Website B doesn't have an API which is what's making this more complicated than should be.
My first option is to render my own form on website A, user enters his website B password into my form, and I somehow securely pass his website B password to website B to log him in. This means I have to first pass the password securely from the client to my server, then pass it again securely from my server to the end website. That's what I'm guessing I have to do, but I don't have a plan for how to implement these 2 hops securely, so I worry that I might expose the user's password somehow.
So I thought of the second option which is to render the same website B from website B onto my website. But the form on website B is part of a larger page, so can this be done?
- How would I isolate the code for the form itself from the code for the full page
- how to present the form on my website. I want the user to see it. iframe comes to mind but never worked with it
- will the form still be valid when the user clicks the submit button or does rendering the form on my website somehow invalidates it
These are the 2 different solutions I thought of. I welcome answers for each of them, and also welcome answers that suggest an alternate third approach that may be easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只要您在网站上使用 SSL,就不会存在泄露用户密码的重大风险(除非您正在进行某种金融交易,那么请澄清)。
我的建议是,不要复制他们的表格。相反,复制该表单生成的 HTTP POST。您可以完全以编程方式完成此操作,并且用户永远不会离开您的网站,但(在大多数情况下)结果将是用户也登录到他们的网站。
如果需要处理某种散列字段,请请求其表单页面(以编程方式)并使用您收到的任何值将发回到第二个站点,以便请求得到验证。他们的服务器不知道请求不是来自浏览器(事实上,如果您愿意,您可以将用户代理添加到 HTTP 标头)。
我已经对 Verizon 网站和 LinkedIn(均出于合法目的)使用了这种方法,并且它有效。
回顾一下:
了解其 HTTP POST 的结构。
向您的网站添加登录表单。
在代码中处理请求,使其看起来像 POST 他们的请求
网站期望。
从您的代码发布到他们的网站。
在您的网站上显示对用户的响应(如果需要),
重定向,无论如何。
As long as you are using SSL on your site, there's not a significant risk in terms of compromising the user's password (unless you are doing financial transactions of some sort, then please clarify).
My suggestion would be, don't copy their form. Instead, replicate the HTTP POST generated by that form. You can do this completely programmatically and the user will never leave YOUR site, but (in most cases) the result will be that the user is logged in to THEIR site as well.
If there is some sort of hashed fields to deal with, request their form page (programatically) and use whatever values you receive to send back to the second site so that the request will validate. Their server doesn't know that the request isn't coming from a browser (indeed, you can add a user agent to the HTTP headers if you wish).
I have used this methodology against Verizon's site and LinkedIn (both for legitimate purposes) and it works.
To recap:
Learn the structure of their HTTP POST.
Add a login form to YOUR site.
Manipulate the request in your code to look like the POST their
site expects.
POST to their site from your code.
Display the response to the user on your site (if needed),
redirect, whatever.
这完全取决于您要发布的服务器如何处理 CSRF [跨站点请求伪造],因为这基本上就是您正在做的事情。例如,如果他们使用相对较新的 Django,那么来自外部服务器的 POST 请求默认会失败,除非它们包含 csrf cookie 值。
如果您也可以控制要发布到的服务器,则可以解决此问题。
It all depends on how the server you are POSTing to handles CSRF [Cross-Site Request Forgeries], as that is basically what you are doing. If they are using a Django that is relatively recent, for instance, then POST requests from an outside server will by default fail, unless they contain the csrf cookie value.
It is possible to get around this, if you have control of the server you are POSTing to as well.
我将尝试概述一个解决方案,但它可能不正确,甚至不是您想要的,而且绝对不切实际。
根据我对 XSRF 的了解,如果目标站点做好了功课,您将无法模仿从您的域提交的表单。
除了目标站点之外,唯一有权访问用户凭据的就是浏览器。所以你真正需要做的是成为一个浏览器。这意味着您必须让用户相信他可以将他的登录信息交给您到其他站点。我不知道你怎么能做到这一点,我个人不会相信这样的事情。 (另一种选择是欺骗他(网络钓鱼),但这是非法的,我认为您甚至不会考虑它。)
在您设法说服用户向您提供他的数据后,您仍然必须表现得像一个浏览器。您必须实现一个为每个用户存储 cookie 的系统,以便在您登录时创建会话。
这样做后,您将使用用户的数据登录,但用户不会< /强>。因此,您还需要通过您的网站实现用户需要的任何其他操作。
我的建议:
联系目标站点并设置可用于对站点中的用户进行身份验证的协议。有一些非常酷的 iframe 技术可以做到这一点。阅读本文,了解精彩的演示 http://softwareas.com/cross-domain-communication- with-iframe 。
或者让用户打开一个弹出窗口,其中包含该站点的登录页面。确保弹出窗口的地址栏可见。
I'll try to outline a solution, but it may not be correct, or even what you wanted and it definitely is impractical.
From what I know about XSRF if the destination site did its homework you will not be able to mimic a form submit from your domain.
The only one that has access to the user credentials, apart from the target site, is the browser. So what you actually need to do is become a browser. This means that you must convince the user that he can hand you his login information to the other site. I have no idea how you can do that, I for one wouldn't trust something like that. (Another option would be to trick him (phishing), but that is illegal and I don't think you would even consider it.)
After you manage to convince the user to give you his data you'll still have to act like a browser. You'll have to implement a system for storing cookies for each user so that a session can be created when you log in.
After doing so you will be logged in with the user's data, but the user won't be. So you will need to implement any other operation the user would need through your site as well.
What I recommend:
Either contact the destination site and set up a protocol that you can use to authenticate the user from your site. There are some really cool iframe techniques to do that. Read this article for a competent presentation http://softwareas.com/cross-domain-communication-with-iframes .
Or make the user open a popup with the login page to that site. Make sure the popup has the address bar visible.
我并没有真正得到你想要做的事情,如果只是登录网站,你也许可以做这样的事情:
Mainsite:
/logonto-other.html:
这样用户将只登录到其他站点,您与其交互的能力仍然会受到限制(或者应该,我我想,我从来没有学过同源策略,因为我从来没有觉得我所做的事情有任何限制,也许今天的浏览器更严格)。
关于“2 hop”的事情,您的服务器 A 上有证书和 SSL/HTTPS 支持吗? B服务器有这个吗?您认为您能让用户信任您拥有他们的密码吗?一个提示是您添加一段解释情况的段落和一个链接,用户可以在其中联系站点 B 以向他们施加压力以实施 OAuth 和/或 OpenID?
即使您的服务器上没有 HTTPS,您也可以使用 JavaScript 或 Java 小程序来加密密码(网络上的某个地方必须有 JavaScript 中的 PGP),也许可以很好地找到某些内容。尽管如此,如果浏览器地址栏变绿,用户可以更轻松地决定是否信任您。
想知道我的问题的答案吗?我们可以知道站点 B 是什么吗?也许我们可以合作。 。
祝你好运
I'm not really getting what you want to do, if it is just logging into the site you can maybe do something like this:
Mainsite:
/logonto-other.html:
This way the user will just be logged into the other site, your interaction abilities with it would still be limited (or should, I think, I never learned the same-origin policies as I have never felt that there is any restriction to what I do. Maybe todays browsers are stricter).
About the "2 hop" thing, do you have a certificate and SSL/HTTPS support on your server A? Does server B have this? Do you think you can make the users trust you on having their password? One tip would be that you add a paragraph explaining the situation and a link there the user can contact site B to add pressure on them to implement OAuth and / or OpenID?
It might be possible to path something up nicely, even if you don't have HTTPS on your server you can use JavaScript or an Java applet to encrypt the password (there has to be PGP in JavaScript somewhere on the net. Although, if the browsers addressbar turns green, the users have an easier decision whether or not to trust you.
Would Like some answers to my questions, and can we be allowed to know what Site B is? And what exactly you try to do, maybe we can work this out together.
Wish you good luck.
要将表单与较大的页面隔离,请找到
标记,并仅复制它们之间的任何 和 元素。现在将该表单放在您的页面上,其操作仍然指向它在原始网站上指向的 URL。密码永远不会通过您的服务器。
To isolate the form from the larger page, find the
<form>
and</form>
tags, and only copy any and elements between them. Now put that form on your page, with its action still pointing to the URL it was pointing to on the original website. The password never goes through your server.参见:Sockets
请记住,您可能希望就您在站点 B 上所做的事情保持开放和诚实的对话,否则您可能承担民事诉讼。
See: Sockets
Keep in mind you may want to keep an open and honest dialog about what you're doing with Site B, or you could be liable for civil action.
您所描述的场景可能非常棘手甚至不可能。您面临的主要问题是如何将登录凭据发布到站点 B,以便成功登录本身返回的 cookie 最终出现在用户的浏览器中并属于站点 B,以便站点 B 在登录时获取它们。用户实际上导航到那里。
有几个人建议将表单发布到服务器上的 PHP 文件中,并让它在服务器之间进行登录。这个解决方案几乎永远不会起作用。即使站点 B 接受您的服务器-服务器登录(它可能会这样做,因为您可以伪造任何您想要的浏览器),您的网络服务器将获得新的 cookie,并且会话将与之关联的网络服务器的 IP。即使您将 cookie 返回给浏览器,它也会将其保存到您的站点(站点 A),并且当浏览器到达站点 B 时不会将其提交到站点 B,因此用户将保持注销状态 。
因此,您在这里唯一能做的就是在您的服务器上托管其网站的登录表单 这意味着您必须向浏览器发送一个表单,其中包含指向站点 B 的登录表单的操作。这也意味着,每当用户单击登录按钮时,控制权就会转移到站点 B:用户将离开您的站点,您就会失去他。
第二种方法有两个主要的技术问题:一是站点 B 可能有 XSS 防护,实际上可能会阻止您将内容发布到他们的登录页面。另一个问题是,如果您的网站使用 SSL,浏览器会很烦您向其他网站提交表单。这些都不是你自己能够解决的。
唯一干净的解决方案是真正与站点 B 坐下来,制定一些有关通用身份验证或至少是身份验证 API 的计划。您可以尝试表单交叉发布解决方案,但很可能您会遇到您不喜欢的问题。
The scenario you are describing can be tricky to impossible. The main problem you are facing is about how you post the login credentials to site B in such a way that the cookies returned by the successful login itself end up in the user's browser and belong to site B, so that site B gets them when the user actually navigates there.
A couple of people have suggested to have the form posted to a PHP file on your server and let that do the login from server to server. This solution almost never works. Even if site B accepts your server-server login (which it probably will, as you can fake any browser you want), it will be your webserver which will be getting the new cookies, and your webserver's IP which the session will be associated with. Even if you return the cookie to the browser, it will save it for your site (site A) and won't submit it to site B when the browser gets there, so the user will stay logged out of site B.
So, the only thing you can do here is to host a login form for their site on your server. This means that you will have to send out a form to the browser which has an ACTION that points to site B's login form. This also means that whenever the user clicks the login button, the control is transferred to site B: the user will be navigating away from your site and you lose him.
There are 2 major technical problems with this second method: one is that site B might have XSS prevention and may actually block you from posting stuff to their login page. The other problem is that if your site is on SSL, browsers will be nagging big time for you submitting a form to a different website. None of these is something you can solve yourself.
The only clean solution would be to really sit down with site B and make some plan about a common authentication, or at least an authentication API. You can try the form crossposting solution but chances are good that you'll have problems with it you don't like.