Skip to content

Commit 25551ad

Browse files
[bidi][js] Allow passing in uri for authentication handlers (#14386)
Co-authored-by: David Burns <[email protected]>
1 parent 0338677 commit 25551ad

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

javascript/node/selenium-webdriver/lib/network.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const { InterceptPhase } = require('../bidi/interceptPhase')
2020
const { AddInterceptParameters } = require('../bidi/addInterceptParameters')
2121

2222
class Network {
23+
#callbackId = 0
2324
#driver
2425
#network
25-
#callBackInterceptIdMap = new Map()
26+
#authHandlers = new Map()
2627

2728
constructor(driver) {
2829
this.#driver = driver
@@ -39,39 +40,51 @@ class Network {
3940
return
4041
}
4142
this.#network = await network(this.#driver)
42-
}
4343

44-
async addAuthenticationHandler(username, password) {
45-
await this.#init()
44+
await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
4645

47-
const interceptId = await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
46+
await this.#network.authRequired(async (event) => {
47+
const requestId = event.request.request
48+
const uri = event.request.url
49+
const credentials = this.getAuthCredentials(uri)
50+
if (credentials !== null) {
51+
await this.#network.continueWithAuth(requestId, credentials.username, credentials.password)
52+
return
53+
}
4854

49-
const id = await this.#network.authRequired(async (event) => {
50-
await this.#network.continueWithAuth(event.request.request, username, password)
55+
await this.#network.continueWithAuthNoCredentials(requestId)
5156
})
57+
}
58+
59+
getAuthCredentials(uri) {
60+
for (let [, value] of this.#authHandlers) {
61+
if (uri.match(value.uri)) {
62+
return value
63+
}
64+
}
65+
return null
66+
}
67+
async addAuthenticationHandler(username, password, uri = '//') {
68+
await this.#init()
5269

53-
this.#callBackInterceptIdMap.set(id, interceptId)
70+
const id = this.#callbackId++
71+
72+
this.#authHandlers.set(id, { username, password, uri })
5473
return id
5574
}
5675

5776
async removeAuthenticationHandler(id) {
5877
await this.#init()
5978

60-
const interceptId = this.#callBackInterceptIdMap.get(id)
61-
62-
await this.#network.removeIntercept(interceptId)
63-
await this.#network.removeCallback(id)
64-
65-
this.#callBackInterceptIdMap.delete(id)
79+
if (this.#authHandlers.has(id)) {
80+
this.#authHandlers.delete(id)
81+
} else {
82+
throw Error(`Callback with id ${id} not found`)
83+
}
6684
}
6785

6886
async clearAuthenticationHandlers() {
69-
for (const [key, value] of this.#callBackInterceptIdMap.entries()) {
70-
await this.#network.removeIntercept(value)
71-
await this.#network.removeCallback(key)
72-
}
73-
74-
this.#callBackInterceptIdMap.clear()
87+
this.#authHandlers.clear()
7588
}
7689
}
7790

javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ suite(
4545
assert.equal(source.includes('Access granted'), true)
4646
})
4747

48+
it('can add authentication handler with filter', async function () {
49+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
50+
await driver.get(Pages.basicAuth)
51+
52+
await driver.wait(until.elementLocated(By.css('pre')))
53+
let source = await driver.getPageSource()
54+
assert.equal(source.includes('Access granted'), true)
55+
})
56+
57+
it('can add multiple authentication handlers with filter', async function () {
58+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
59+
await driver.network().addAuthenticationHandler('test', 'test', 'test')
60+
await driver.get(Pages.basicAuth)
61+
62+
await driver.wait(until.elementLocated(By.css('pre')))
63+
let source = await driver.getPageSource()
64+
assert.equal(source.includes('Access granted'), true)
65+
})
66+
67+
it('can add multiple authentication handlers with the same filter', async function () {
68+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
69+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
70+
await driver.get(Pages.basicAuth)
71+
72+
await driver.wait(until.elementLocated(By.css('pre')))
73+
let source = await driver.getPageSource()
74+
assert.equal(source.includes('Access granted'), true)
75+
})
76+
4877
it('can remove authentication handler', async function () {
4978
const id = await driver.network().addAuthenticationHandler('genie', 'bottle')
5079

@@ -59,8 +88,17 @@ suite(
5988
}
6089
})
6190

91+
it('throws an error when remove authentication handler that does not exist', async function () {
92+
try {
93+
await driver.network().removeAuthenticationHandler(10)
94+
assert.fail('Expected error not thrown. Non-existent handler cannot be removed')
95+
} catch (e) {
96+
assert.strictEqual(e.message, 'Callback with id 10 not found')
97+
}
98+
})
99+
62100
it('can clear authentication handlers', async function () {
63-
await driver.network().addAuthenticationHandler('genie', 'bottle')
101+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
64102

65103
await driver.network().addAuthenticationHandler('bottle', 'genie')
66104

0 commit comments

Comments
 (0)