Class: SolidAgent::AgentManifest::Registry::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_agent/agent_manifest/registry/auth.rb

Overview

Auth handles token management for the ActiveAgents registry.

Tokens are stored in ~/.activeagent/token by default. Can also be configured via environment variable.

Examples:

Check if logged in

Registry::Auth.logged_in?

Get current token

token = Registry::Auth.token

Save a token

Registry::Auth.save_token("aa_live_xxx")

Constant Summary collapse

TOKEN_PATH =
File.expand_path("~/.activeagent/token")
TOKEN_ENV_VAR =
"ACTIVEAGENT_TOKEN"

Class Method Summary collapse

Class Method Details

.auth_headerHash

Get authorization header for API requests

Returns:

  • (Hash)

    Authorization header



93
94
95
96
97
98
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 93

def auth_header
  t = token
  return {} unless t

  { "Authorization" => "Bearer #{t}" }
end

.clear_tokenBoolean

Clear the saved token

Returns:

  • (Boolean)

    true if cleared successfully



63
64
65
66
67
68
69
70
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 63

def clear_token
  return true unless File.exist?(TOKEN_PATH)

  File.delete(TOKEN_PATH)
  true
rescue StandardError => e
  raise RegistryError, "Failed to clear token: #{e.message}"
end

.logged_in?Boolean

Check if user is logged in

Returns:

  • (Boolean)


75
76
77
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 75

def logged_in?
  token.present?
end

.require_token!String

Require authentication, raising if not logged in

Returns:

  • (String)

    The token

Raises:



83
84
85
86
87
88
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 83

def require_token!
  t = token
  raise RegistryError, "Not authenticated. Run 'activeagent login' or set #{TOKEN_ENV_VAR}" unless t

  t
end

.save_token(new_token) ⇒ Boolean

Save a token to the token file

Parameters:

  • new_token (String)

    The token to save

Returns:

  • (Boolean)

    true if saved successfully



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 47

def save_token(new_token)
  # Ensure directory exists
  FileUtils.mkdir_p(File.dirname(TOKEN_PATH))

  # Write token with secure permissions
  File.write(TOKEN_PATH, new_token.strip)
  File.chmod(0o600, TOKEN_PATH)

  true
rescue StandardError => e
  raise RegistryError, "Failed to save token: #{e.message}"
end

.tokenString?

Get the current authentication token

Checks in order:

  1. Environment variable (ACTIVEAGENT_TOKEN)
  2. Token file (~/.activeagent/token)

Returns:

  • (String, nil)

    The token or nil if not authenticated



32
33
34
35
36
37
38
39
40
41
# File 'lib/solid_agent/agent_manifest/registry/auth.rb', line 32

def token
  # Check environment variable first
  env_token = ENV[TOKEN_ENV_VAR]
  return env_token if env_token.present?

  # Check token file
  return nil unless File.exist?(TOKEN_PATH)

  File.read(TOKEN_PATH).strip.presence
end