且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python & 的解决方法Selenium:针对 Active Directory 进行身份验证

更新时间:2023-11-30 23:20:22

找到的解决方案:

我需要使用浏览器扩展程序.我的解决方案是为 Chrome 构建的,但它应该几乎不变地移植到 Firefox 和 也许 edge.

首先,您需要为浏览器提供 2 个 API:

First up, you need 2 APIs to be available for your browser:

  • webRequest.onAuthRequired - Chrome & Firefox
  • runtime.nativeMessaging - Chrome & Firefox

虽然这两个浏览器 API 非常相似,但它们确实有一些显着差异 - 例如 Chrome 的实现缺少 Promises.

While both browser APIs are very similar, they do have some significant differences - such as Chrome's implementation lacking Promises.

如果您将本地消息传递主机设置为发送格式正确的 JSON 字符串,则只需轮询一次.这意味着您可以使用一次对 runtime.sendNativeMessage() 的调用,并确保您的凭据是可解析的.双关语.

If you setup your Native Messaging Host to send a properly-formed JSON string, you need only poll it once. This means you can use a single call to runtime.sendNativeMessage() and be assured that your credentials are paresable. Pun intended.

接下来,我们需要看看我们应该如何处理 webRequest.onAuthRequired 事件.

Next, we need to look at how we're supposed to handle the webRequest.onAuthRequired event.

因为我在 Chromium 工作,所以我需要使用无承诺的 Chrome API.

Since I'm working in Chromium, I need to use the promise-less Chrome API.

chrome.webRequest.onAuthRequired.addListener(
  callbackFunctionHere,
  {urls:[targetUrls]},
  ['asyncBlocking'] // --> this line is important, too. Very.

变化:

我将调用我的函数 provideCredentials 因为我是一个大偷窃者并且使用了一个来自 这个 来源.寻找异步版本.

The Change:

I'll be calling my function provideCredentials because I'm a big stealy-stealer and used an example from this source. Look for the asynchronous version.

示例代码从 storage.local ...

The example code fetches the credentials from storage.local ...

chrome.storage.local.get(null, gotCredentials);

我们不想那样.没有.

我们希望从对 sendNativeMessage 的单个调用中获取凭据,因此我们将更改这一行.

We want to get the credentials from a single call to sendNativeMessage so we'll change that one line.

chrome.runtime.sendNativeMessage(hostName, { text: "Ready" }, gotCredentials);

仅此而已.严重地.只要您的主持人表现出色,这就是最大的秘密.我什至不会告诉你我花了多长时间才找到它!

That's all it takes. Seriously. As long as your Host plays nice, this is the big secret. I won't even tell you how long it took me to find it!

我的问题以及有用的链接:

My questions with helpful links:

  • 此处 - 针对 Active Directory 进行身份验证的解决方法
  • 这里 - 还有一些工作代码用于功能NM主机
  • 此处 - 一些关于 Promise 的启发性材料
  • Here - Workaround for Authenticating against Active Directory
  • Here - Also has some working code for a functional NM Host
  • Here - Some enlightening material on promises

结果证明这是一个不平凡的问题.

So this turns out to be a non-trivial problem.

我还没有实施解决方案,但我知道如何实现...

I haven't implemented the solution, yet, but I know how to get there...

将值传递给扩展程序是第一步 - 这可以在 Chrome 和 Firefox 中完成.观察版本以确保所需的 API(nativeMessaging)确实存在于您的版本中.由于这个原因,我不得不改用铬.

Passing values to an extension is the first step - this can be done in both Chrome and Firefox. Watch the version to make sure the API required, nativeMessaging, actually exists in your version. I have had to switch to chromium for this reason.

或者,您可以使用存储 API 首先将值放入浏览器存储中.

Alternatively, one can use the storage API to put values in browser storage first. [edit: I did not go this way for security concerns]

接下来是使用来自 webRequest API 的 onAuthRequired 事件.在事件上设置监听器并传入您需要的值.

Next is to use the onAuthRequired event from the webRequest API . Setup a listener on the event and pass in the values you need.

注意事项:我已经为 nativeMessaging API 解决方案构建了扩展本身的所有内容,但在让脚本识别数据方面仍然存在问题.这几乎可以肯定是我的 JavaScript 技能与使这些 API 变得有意义所需的神秘知识发生冲突......我还没有尝试过这种存储方法,因为它不太安全(在我看来),但它似乎更简单.

Caveats: I have built everything right up to the extension itself for the nativeMessaging API solution and there's still a problem with getting the script to recognise the data. This is almost certainly my JavaScript skills ***ing with the arcane knowledge required to make these APIs make much sense ... I have yet to attempt the storage method as it's less secure (in my mind) but it does seem to be simpler.