Class: Kward::APIKeyStore
- Inherits:
-
Object
- Object
- Kward::APIKeyStore
- Defined in:
- lib/kward/auth/api_key_store.rb
Overview
Stores API keys outside the non-secret Kward configuration file.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #configured?(provider_id) ⇒ Boolean
- #delete(provider_id) ⇒ Object
- #fetch(provider_id) ⇒ Object
-
#initialize(path: APIKeyStore.default_path, config_path: ConfigFiles.config_path, env: ENV) ⇒ APIKeyStore
constructor
A new instance of APIKeyStore.
- #migrate_openrouter_config_key! ⇒ Object
- #store(provider_id, api_key) ⇒ Object
- #stored?(provider_id) ⇒ Boolean
- #stored_value(provider_id) ⇒ Object
Constructor Details
#initialize(path: APIKeyStore.default_path, config_path: ConfigFiles.config_path, env: ENV) ⇒ APIKeyStore
Returns a new instance of APIKeyStore.
10 11 12 13 14 |
# File 'lib/kward/auth/api_key_store.rb', line 10 def initialize(path: APIKeyStore.default_path, config_path: ConfigFiles.config_path, env: ENV) @path = File.(path) @config_path = File.(config_path) @env = env end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/kward/auth/api_key_store.rb', line 16 def path @path end |
Class Method Details
.default_path ⇒ Object
18 19 20 |
# File 'lib/kward/auth/api_key_store.rb', line 18 def self.default_path File.join(ConfigFiles.config_dir, "api_keys.json") end |
Instance Method Details
#configured?(provider_id) ⇒ Boolean
27 28 29 |
# File 'lib/kward/auth/api_key_store.rb', line 27 def configured?(provider_id) !fetch(provider_id).to_s.empty? end |
#delete(provider_id) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/kward/auth/api_key_store.rb', line 44 def delete(provider_id) provider = ProviderCatalog.fetch(provider_id) keys = stored_keys return false unless keys.delete(provider.id) write_keys(keys) true end |
#fetch(provider_id) ⇒ Object
22 23 24 25 |
# File 'lib/kward/auth/api_key_store.rb', line 22 def fetch(provider_id) provider = ProviderCatalog.fetch(provider_id) environment_key(provider) || stored_key(provider.id) end |
#migrate_openrouter_config_key! ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kward/auth/api_key_store.rb', line 62 def migrate_openrouter_config_key! return false if stored?("openrouter") config = ConfigFiles.read_config(@config_path) key = config["openrouter_api_key"].to_s.strip return false if key.empty? store("openrouter", key) ConfigFiles.delete_config_key("openrouter_api_key", @config_path) true end |
#store(provider_id, api_key) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kward/auth/api_key_store.rb', line 31 def store(provider_id, api_key) provider = ProviderCatalog.fetch(provider_id) raise ArgumentError, "#{provider.name} does not accept an API key" unless provider.api_key? api_key = api_key.to_s.strip raise ArgumentError, "API key must be a non-empty string" if api_key.empty? keys = stored_keys keys[provider.id] = api_key write_keys(keys) path end |
#stored?(provider_id) ⇒ Boolean
53 54 55 |
# File 'lib/kward/auth/api_key_store.rb', line 53 def stored?(provider_id) !stored_key(provider_id).to_s.empty? end |
#stored_value(provider_id) ⇒ Object
57 58 59 60 |
# File 'lib/kward/auth/api_key_store.rb', line 57 def stored_value(provider_id) ProviderCatalog.fetch(provider_id) stored_key(provider_id) end |