"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/lib/selenium/webdriver/common/driver_extensions/has_authentication.rb" (17 Feb 2023, 3134 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "has_authentication.rb": 4.7.0_vs_4.8.0.

    1 # frozen_string_literal: true
    2 
    3 # Licensed to the Software Freedom Conservancy (SFC) under one
    4 # or more contributor license agreements.  See the NOTICE file
    5 # distributed with this work for additional information
    6 # regarding copyright ownership.  The SFC licenses this file
    7 # to you under the Apache License, Version 2.0 (the
    8 # "License"); you may not use this file except in compliance
    9 # with the License.  You may obtain a copy of the License at
   10 #
   11 #   http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 # Unless required by applicable law or agreed to in writing,
   14 # software distributed under the License is distributed on an
   15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   16 # KIND, either express or implied.  See the License for the
   17 # specific language governing permissions and limitations
   18 # under the License.
   19 
   20 module Selenium
   21   module WebDriver
   22     module DriverExtensions
   23       module HasAuthentication
   24         #
   25         # Registers basic authentication handler which is automatically
   26         # used whenever browser gets an authentication required response.
   27         # This currently relies on DevTools so is only supported in
   28         # Chromium browsers.
   29         #
   30         # @example Authenticate any request
   31         #   driver.register(username: 'admin', password: '123456')
   32         #
   33         # @example Authenticate based on URL
   34         #   driver.register(username: 'admin1', password: '123456', uri: /mysite1\.com/)
   35         #   driver.register(username: 'admin2', password: '123456', uri: /mysite2\.com/)
   36         #
   37         # @param [String] username
   38         # @param [String] password
   39         # @param [Regexp] uri to associate the credentials with
   40         #
   41 
   42         def register(username:, password:, uri: //)
   43           auth_handlers << {username: username, password: password, uri: uri}
   44 
   45           devtools.network.set_cache_disabled(cache_disabled: true)
   46           devtools.fetch.on(:auth_required) do |params|
   47             authenticate(params['requestId'], params.dig('request', 'url'))
   48           end
   49           devtools.fetch.on(:request_paused) do |params|
   50             devtools.fetch.continue_request(request_id: params['requestId'])
   51           end
   52           devtools.fetch.enable(handle_auth_requests: true)
   53         end
   54 
   55         private
   56 
   57         def auth_handlers
   58           @auth_handlers ||= []
   59         end
   60 
   61         def authenticate(request_id, url)
   62           credentials = auth_handlers.find do |handler|
   63             url.match?(handler[:uri])
   64           end
   65 
   66           if credentials
   67             devtools.fetch.continue_with_auth(
   68               request_id: request_id,
   69               auth_challenge_response: {
   70                 response: 'ProvideCredentials',
   71                 username: credentials[:username],
   72                 password: credentials[:password]
   73               }
   74             )
   75           else
   76             devtools.fetch.continue_with_auth(
   77               request_id: request_id,
   78               auth_challenge_response: {
   79                 response: 'CancelAuth'
   80               }
   81             )
   82           end
   83         end
   84       end # HasAuthentication
   85     end # DriverExtensions
   86   end # WebDriver
   87 end # Selenium