Class: A2A::Plugins::ExampleAuth

Inherits:
A2A::Plugin::AuthPlugin show all
Defined in:
lib/a2a/plugins/example_auth.rb

Instance Attribute Summary

Attributes inherited from A2A::Plugin::Base

#config, #logger

Instance Method Summary collapse

Methods inherited from A2A::Plugin::Base

dependencies, depends_on, inherited, #initialize, plugin_type

Constructor Details

This class inherits a constructor from A2A::Plugin::Base

Instance Method Details

#authenticate_request(request, **options) ⇒ Hash

Authenticate request

Parameters:

  • Request data

  • Authentication options

Returns:

  • Authenticated request



21
22
23
24
25
26
27
28
29
# File 'lib/a2a/plugins/example_auth.rb', line 21

def authenticate_request(request, **options)
  logger&.info("Authenticating request with Example Auth")

  # Add custom authentication header
  request[:headers] ||= {}
  request[:headers]["X-Example-Auth"] = generate_token(options)

  request
end

#cleanupObject (private)



65
66
67
68
# File 'lib/a2a/plugins/example_auth.rb', line 65

def cleanup
  @secret_key = nil
  logger&.info("Example Auth plugin cleaned up")
end

#generate_token(options) ⇒ Object (private)



70
71
72
73
74
75
76
77
78
# File 'lib/a2a/plugins/example_auth.rb', line 70

def generate_token(options)
  # Simple token generation - in real implementation, use proper JWT or similar
  payload = {
    timestamp: Time.now.to_i,
    client_id: options[:client_id] || "unknown"
  }

  Base64.encode64("#{payload.to_json}:#{@secret_key}").strip
end

#register_hooks(plugin_manager) ⇒ Object

Register hooks for this plugin



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/a2a/plugins/example_auth.rb', line 43

def register_hooks(plugin_manager)
  plugin_manager.add_hook(A2A::Plugin::Events::BEFORE_REQUEST) do |request|
    logger&.debug("Example Auth: Validating request authentication")

    # Add request timestamp for security
    request[:auth_timestamp] = Time.now.to_i
  end

  plugin_manager.add_hook(A2A::Plugin::Events::REQUEST_ERROR) do |error, request|
    if error.is_a?(A2A::Errors::AuthenticationError)
      logger&.warn("Example Auth: Authentication failed for request #{request[:id]}")
    end
  end
end

#setupObject (private)



60
61
62
63
# File 'lib/a2a/plugins/example_auth.rb', line 60

def setup
  @secret_key = config[:secret_key] || "default_secret"
  logger&.info("Example Auth plugin initialized")
end

#strategy_nameObject

Authentication strategy name



13
14
15
# File 'lib/a2a/plugins/example_auth.rb', line 13

def strategy_name
  "example"
end

#validate_credentials(credentials) ⇒ Boolean

Validate credentials

Parameters:

  • Credentials to validate

Returns:

  • Whether credentials are valid



34
35
36
37
38
39
40
# File 'lib/a2a/plugins/example_auth.rb', line 34

def validate_credentials(credentials)
  return false unless credentials.is_a?(Hash)
  return false unless credentials[:api_key]

  # Simple validation - in real implementation, validate against backend
  credentials[:api_key].start_with?("example_")
end