Class: Kward::RPC::AuthManager
- Inherits:
-
Object
- Object
- Kward::RPC::AuthManager
- Defined in:
- lib/kward/rpc/auth_manager.rb
Overview
RPC authentication manager for provider status, login, and logout requests.
Defined Under Namespace
Classes: Login
Instance Method Summary collapse
-
#initialize(server:, oauth_factory: -> { OpenAIOAuth.new }, github_oauth_factory: -> { GithubOAuth.new }, anthropic_oauth_factory: -> { AnthropicOAuth.new }, config_manager: ConfigManager.new) ⇒ AuthManager
constructor
A new instance of AuthManager.
- #login_status(login_id:) ⇒ Object
- #login_with_api_key(provider_id:, api_key:, configuration: nil) ⇒ Object
- #login_with_oauth(provider_id:, timeout_seconds: 120) ⇒ Object
- #logout_provider(provider_id:, auth_method: nil) ⇒ Object
- #providers ⇒ Object
- #start_oauth_login(provider_id:, oauth:, timeout_seconds: 120) ⇒ Object
- #start_openai_login(timeout_seconds: 120) ⇒ Object
- #status ⇒ Object
- #submit_openai_code(login_id:, code:) ⇒ Object
Constructor Details
#initialize(server:, oauth_factory: -> { OpenAIOAuth.new }, github_oauth_factory: -> { GithubOAuth.new }, anthropic_oauth_factory: -> { AnthropicOAuth.new }, config_manager: ConfigManager.new) ⇒ AuthManager
Returns a new instance of AuthManager.
18 19 20 21 22 23 24 25 26 |
# File 'lib/kward/rpc/auth_manager.rb', line 18 def initialize(server:, oauth_factory: -> { OpenAIOAuth.new }, github_oauth_factory: -> { GithubOAuth.new }, anthropic_oauth_factory: -> { AnthropicOAuth.new }, config_manager: ConfigManager.new) @server = server @oauth_factory = oauth_factory @github_oauth_factory = github_oauth_factory @anthropic_oauth_factory = anthropic_oauth_factory @config_manager = config_manager @logins = {} @mutex = Mutex.new end |
Instance Method Details
#login_status(login_id:) ⇒ Object
122 123 124 |
# File 'lib/kward/rpc/auth_manager.rb', line 122 def login_status(login_id:) login_payload(fetch_login(login_id)) end |
#login_with_api_key(provider_id:, api_key:, configuration: nil) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/kward/rpc/auth_manager.rb', line 46 def login_with_api_key(provider_id:, api_key:, configuration: nil) raise ArgumentError, "API key must be a non-empty string" if api_key.to_s.strip.empty? provider = ProviderCatalog.fetch(provider_id) @config_manager.configure_azure_openai(configuration || {}) if provider.id == "azure_openai" @config_manager.set_api_key(provider.id, api_key) { providerId: provider.id, authMethod: "api_key", configured: true, message: "Saved API key for #{provider.name}." } end |
#login_with_oauth(provider_id:, timeout_seconds: 120) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kward/rpc/auth_manager.rb', line 69 def login_with_oauth(provider_id:, timeout_seconds: 120) provider_id = provider_id.to_s case provider_id when "openai" start_oauth_login(provider_id: "openai", oauth: @oauth_factory.call, timeout_seconds: timeout_seconds) when "anthropic" start_oauth_login(provider_id: "anthropic", oauth: @anthropic_oauth_factory.call, timeout_seconds: timeout_seconds) when "github", "copilot" raise "GitHub Copilot OAuth is unavailable over RPC; use the interactive CLI login." when "openrouter" raise "OpenRouter OAuth is unavailable because Kward has not implemented OpenRouter's official PKCE flow." when "xai" raise "xAI OAuth is unavailable because no official stable third-party flow is supported." else raise "Unsupported OAuth provider: #{provider_id}" end end |
#logout_provider(provider_id:, auth_method: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kward/rpc/auth_manager.rb', line 55 def logout_provider(provider_id:, auth_method: nil) provider = ProviderCatalog.fetch(provider_id) auth_method = auth_method.to_s unless auth_method.nil? removed = false if provider.api_key? && (auth_method.nil? || auth_method == "api_key") removed = @config_manager.delete_api_key(provider.id) || removed end if provider.oauth? && (auth_method.nil? || auth_method == "oauth") removed = logout_oauth_provider(provider.id) || removed end { providerId: provider.id, authMethod: auth_method, removed: removed, message: "Logged out of #{provider.name}." }.compact end |
#providers ⇒ Object
42 43 44 |
# File 'lib/kward/rpc/auth_manager.rb', line 42 def providers { providers: ProviderCatalog.all.map { |provider| provider_payload(provider) } } end |
#start_oauth_login(provider_id:, oauth:, timeout_seconds: 120) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/kward/rpc/auth_manager.rb', line 91 def start_oauth_login(provider_id:, oauth:, timeout_seconds: 120) flow = oauth.start_login_flow pkce = flow.fetch(:pkce) state = flow.fetch(:state) server = flow.fetch(:server) redirect_uri = flow.fetch(:redirect_uri) url = flow.fetch(:authorization_url) login = Login.new( id: SecureRandom.uuid, provider_id: provider_id, oauth: oauth, pkce: pkce, state: state, server: server, redirect_uri: redirect_uri, status: "pending" ) @mutex.synchronize { @logins[login.id] = login } login.thread = Thread.new { wait_for_callback(login, timeout_seconds: timeout_seconds.to_i <= 0 ? 120 : timeout_seconds.to_i) } { providerId: provider_id, loginId: login.id, authorizationUrl: url, redirectUri: redirect_uri, status: login.status } end |
#start_openai_login(timeout_seconds: 120) ⇒ Object
87 88 89 |
# File 'lib/kward/rpc/auth_manager.rb', line 87 def start_openai_login(timeout_seconds: 120) start_oauth_login(provider_id: "openai", oauth: @oauth_factory.call, timeout_seconds: timeout_seconds) end |
#status ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kward/rpc/auth_manager.rb', line 28 def status provider_status = providers.fetch(:providers) { providers: provider_status, openaiOAuth: oauth_status("openai")[:configured], openrouterApiKey: @config_manager.api_key_status("openrouter")[:configured], openaiAccessToken: !ENV["OPENAI_ACCESS_TOKEN"].to_s.empty?, anthropicOAuth: oauth_status("anthropic")[:configured], githubOAuth: oauth_status("copilot")[:configured] } rescue StandardError => e { providers: [], openaiOAuth: false, error: e. } end |
#submit_openai_code(login_id:, code:) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/kward/rpc/auth_manager.rb', line 113 def submit_openai_code(login_id:, code:) login = fetch_login(login_id) raise "Login is not pending" unless login.status == "pending" code = login.oauth.(code.to_s, expected_state: login.state) complete_login(login, code) login_payload(login) end |