在 AWS Cognito 预身份验证 Lambda 触发器中获取 IP
我正在使用 SAML 作为联合提供商通过 Azure AD 记录我的用户,我想知道此时是否可以接收 IP。如果是这样怎么办。
I'm logging my users via Azure AD using SAML as a federation provider, I wanted to know if it's possible to receive the ip at this point. If so how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,我还没有找到直接的方法来将 IP 地址发送到 lambda。
我们知道,预身份验证 lambda 将从 Cognito 接收此类数据:
正如我们所见,IP 地址不包含在其中。
但是,我们可以利用“validationData”属性,它是可以从客户端设置的其他属性的集合。
IP 地址就是这样的属性之一。
现在我们需要以某种方式获取此 IP 地址并将其发送给 lambda。
我发现在客户端应用程序中获取 IP 地址的一种方法是向 https:// 发出 GET 请求geolocation-db.com/json/ (或对任何其他 IP 提供商的请求)
响应将如下所示
只需按照您认为合适的方式清理响应,然后将其添加到您的认知登录请求中即可。
更改客户端代码而不发送或发送修改后的 IP 地址存在风险,您可以在 lambda 本身上进行验证,具体取决于其重要性。
实际上,你必须看看有人会有多大的动力去做这样的修改。
I have so far not found a direct way to enable the IP address to be sent to the lambda.
What we know is that the pre-authentication lambda will receive such data, from Cognito:
As we can see, the IP address is not contained within.
However, we could leverage the "validationData" property, which is a collection of other properties, that can be set from the client.
One such property could be the IP address.
Now we need to somehow obtain this IP address and send it to the lambda.
One way I found to obtain the IP address in a client application, is by making a GET request to https://geolocation-db.com/json/ (or a request to any other IP providers)
The response will look like this
Simply clean up the response the way you see fit, then add it to your cognito log in request.
There is a risk for altering the client's code, and not sending, or sending a modified IP address, which, depending on how important this is, you could validate on the lambda itself.
You have to see, really, how motivated would somebody be, to do such a modification.
另一种方法是使用 AWS WAF(Web 应用程序防火墙),请参阅此 文档
在 WAF 中,您可以按照以下步骤创建 Web ACL:
这样,向 Cognito 发出的每个 HTTP(S) 请求都会首先执行被WAF过滤,如果符合您创建的IP规则,流量将被允许,否则将被禁止(403状态码)
Another approach is using AWS WAF (Web Application Firewall), see this docs
In WAF you can create a Web ACL by following these steps:
This way, every HTTP(S) request made to Cognito will first be filtered by WAF, if it matches the IP Rule you created traffic will be allowed, otherwise it will be Forbidden (403 status code)