Class: Dependabot::Python::AuthedUrlBuilder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/python/authed_url_builder.rb

Class Method Summary collapse

Class Method Details

.authed_url(credential:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dependabot/python/authed_url_builder.rb', line 12

def self.authed_url(credential:)
  token = T.let(credential.fetch("token", nil), T.nilable(String))
  url = T.let(credential.fetch("index-url", nil), T.nilable(String))
  return "" unless url
  return url unless token

  basic_auth_details =
    if token.ascii_only? && token.include?(":") then token
    elsif Base64.decode64(token).ascii_only? &&
          Base64.decode64(token).include?(":")
      Base64.decode64(token)
    else
      token
    end

  if basic_auth_details.include?(":")
    username, _, password = basic_auth_details.partition(":")
    basic_auth_details = "#{CGI.escape(username)}:#{CGI.escape(password)}"
  end

  url.sub("://", "://#{basic_auth_details}@")
end