Skip to content

Commit

Permalink
[bidi][js] Allow passing in uri for authentication handlers (#14386)
Browse files Browse the repository at this point in the history
Co-authored-by: David Burns <[email protected]>
  • Loading branch information
pujagani and AutomatedTester authored Nov 13, 2024
1 parent 0338677 commit 25551ad
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 21 deletions.
53 changes: 33 additions & 20 deletions javascript/node/selenium-webdriver/lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ const { InterceptPhase } = require('../bidi/interceptPhase')
const { AddInterceptParameters } = require('../bidi/addInterceptParameters')

class Network {
#callbackId = 0
#driver
#network
#callBackInterceptIdMap = new Map()
#authHandlers = new Map()

constructor(driver) {
this.#driver = driver
Expand All @@ -39,39 +40,51 @@ class Network {
return
}
this.#network = await network(this.#driver)
}

async addAuthenticationHandler(username, password) {
await this.#init()
await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))

const interceptId = await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await this.#network.authRequired(async (event) => {
const requestId = event.request.request
const uri = event.request.url
const credentials = this.getAuthCredentials(uri)
if (credentials !== null) {
await this.#network.continueWithAuth(requestId, credentials.username, credentials.password)
return
}

const id = await this.#network.authRequired(async (event) => {
await this.#network.continueWithAuth(event.request.request, username, password)
await this.#network.continueWithAuthNoCredentials(requestId)
})
}

getAuthCredentials(uri) {
for (let [, value] of this.#authHandlers) {
if (uri.match(value.uri)) {
return value
}
}
return null
}
async addAuthenticationHandler(username, password, uri = '//') {
await this.#init()

this.#callBackInterceptIdMap.set(id, interceptId)
const id = this.#callbackId++

this.#authHandlers.set(id, { username, password, uri })
return id
}

async removeAuthenticationHandler(id) {
await this.#init()

const interceptId = this.#callBackInterceptIdMap.get(id)

await this.#network.removeIntercept(interceptId)
await this.#network.removeCallback(id)

this.#callBackInterceptIdMap.delete(id)
if (this.#authHandlers.has(id)) {
this.#authHandlers.delete(id)
} else {
throw Error(`Callback with id ${id} not found`)
}
}

async clearAuthenticationHandlers() {
for (const [key, value] of this.#callBackInterceptIdMap.entries()) {
await this.#network.removeIntercept(value)
await this.#network.removeCallback(key)
}

this.#callBackInterceptIdMap.clear()
this.#authHandlers.clear()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ suite(
assert.equal(source.includes('Access granted'), true)
})

it('can add authentication handler with filter', async function () {
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
await driver.get(Pages.basicAuth)

await driver.wait(until.elementLocated(By.css('pre')))
let source = await driver.getPageSource()
assert.equal(source.includes('Access granted'), true)
})

it('can add multiple authentication handlers with filter', async function () {
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
await driver.network().addAuthenticationHandler('test', 'test', 'test')
await driver.get(Pages.basicAuth)

await driver.wait(until.elementLocated(By.css('pre')))
let source = await driver.getPageSource()
assert.equal(source.includes('Access granted'), true)
})

it('can add multiple authentication handlers with the same filter', async function () {
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
await driver.get(Pages.basicAuth)

await driver.wait(until.elementLocated(By.css('pre')))
let source = await driver.getPageSource()
assert.equal(source.includes('Access granted'), true)
})

it('can remove authentication handler', async function () {
const id = await driver.network().addAuthenticationHandler('genie', 'bottle')

Expand All @@ -59,8 +88,17 @@ suite(
}
})

it('throws an error when remove authentication handler that does not exist', async function () {
try {
await driver.network().removeAuthenticationHandler(10)
assert.fail('Expected error not thrown. Non-existent handler cannot be removed')
} catch (e) {
assert.strictEqual(e.message, 'Callback with id 10 not found')
}
})

it('can clear authentication handlers', async function () {
await driver.network().addAuthenticationHandler('genie', 'bottle')
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')

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

Expand Down

0 comments on commit 25551ad

Please sign in to comment.