Module: Anthropic::Config

Defined in:
lib/anthropic/credentials/config.rb

Class Method Summary collapse

Class Method Details

.active_profileObject



19
20
21
22
23
# File 'lib/anthropic/credentials/config.rb', line 19

def active_profile
  profile = Anthropic::Credentials.active_profile
  validate_profile_name!(profile)
  profile
end

.active_profile_config?Boolean

Returns:



25
26
27
28
29
# File 'lib/anthropic/credentials/config.rb', line 25

def active_profile_config?
  config_file_path(active_profile).file?
rescue Anthropic::Errors::ConfigurationError
  false
end

.config_file_path(profile) ⇒ Object



9
10
11
12
# File 'lib/anthropic/credentials/config.rb', line 9

def config_file_path(profile)
  validate_profile_name!(profile)
  Anthropic::Credentials.config_file_path(profile)
end

.credentials_file_path(profile) ⇒ Object



14
15
16
17
# File 'lib/anthropic/credentials/config.rb', line 14

def credentials_file_path(profile)
  validate_profile_name!(profile)
  Anthropic::Credentials.config_dir.join("credentials", "#{profile}.json")
end

.require_https!(url, field:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/anthropic/credentials/config.rb', line 57

def require_https!(url, field:)
  begin
    uri = URI.parse(url.to_s)
  rescue URI::InvalidURIError
    raise Anthropic::Errors::ConfigurationError,
          "#{field} is not a valid URL (got '#{url}')"
  end

  return if uri.scheme == "https"
  return if uri.scheme == "http" && %w[localhost 127.0.0.1 ::1].include?(uri.hostname)

  raise Anthropic::Errors::ConfigurationError,
        "#{field} must use https (got '#{url}'); the token-exchange endpoint " \
        "carries secret material and cannot be used over cleartext HTTP."
end

.validate_profile_name!(profile, source: "profile name") ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/anthropic/credentials/config.rb', line 31

def validate_profile_name!(profile, source: "profile name")
  if profile.nil? || profile.empty?
    raise Anthropic::Errors::ConfigurationError,
          "#{source} must not be empty"
  end
  if profile != profile.strip
    raise Anthropic::Errors::ConfigurationError,
          "#{source} '#{profile}' has leading/trailing whitespace"
  end
  if profile.start_with?(".")
    raise Anthropic::Errors::ConfigurationError,
          "#{source} '#{profile}' must not start with a dot"
  end

  ["/", "\\", File::SEPARATOR].each do |sep|
    next if sep.empty?
    if profile.include?(sep)
      raise Anthropic::Errors::ConfigurationError,
            "#{source} '#{profile}' must not contain path separators"
    end
  end

  return unless profile.include?("\x00")
  raise Anthropic::Errors::ConfigurationError, "#{source} '#{profile}' must not contain null bytes"
end