Class: A2A::Types::AgentCardSignature

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/a2a/types/agent_card.rb

Overview

Represents an agent card signature

Signatures provide cryptographic verification of agent cards using JSON Web Signature (JWS) format.

Examples:

Creating a signature

signature = A2A::Types::AgentCardSignature.new(
  signature: "eyJhbGciOiJSUzI1NiJ9...",
  protected_header: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9"
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#==, #camelize, from_h, from_json, #hash, #to_h, #to_json, underscore, #valid?, #validate_array_type, #validate_inclusion, #validate_required, #validate_type

Constructor Details

#initialize(signature:, protected_header:) ⇒ AgentCardSignature

Initialize a new agent card signature

Parameters:

  • signature (String)

    JWS signature value (base64url encoded)

  • protected_header (String)

    JWS protected header (base64url encoded JSON)



457
458
459
460
461
# File 'lib/a2a/types/agent_card.rb', line 457

def initialize(signature:, protected_header:)
  @signature = signature
  @protected_header = protected_header
  validate!
end

Instance Attribute Details

#protected_headerObject (readonly)

Returns the value of attribute protected_header.



450
451
452
# File 'lib/a2a/types/agent_card.rb', line 450

def protected_header
  @protected_header
end

#signatureObject (readonly)

Returns the value of attribute signature.



450
451
452
# File 'lib/a2a/types/agent_card.rb', line 450

def signature
  @signature
end

Instance Method Details

#algorithmString?

Get the algorithm from the protected header

Returns:

  • (String, nil)

    The algorithm or nil if not present



490
491
492
493
494
# File 'lib/a2a/types/agent_card.rb', line 490

def algorithm
  decoded_header["alg"]
rescue StandardError
  nil
end

#decoded_headerHash

Decode the protected header

Returns:

  • (Hash)

    The decoded header as a hash



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/a2a/types/agent_card.rb', line 467

def decoded_header
  require "base64"
  require "json"

  # Add padding if needed for base64url decoding
  padded = @protected_header
  case padded.length % 4
  when 2
    padded += "=="
  when 3
    padded += "="
  end

  decoded = Base64.urlsafe_decode64(padded)
  JSON.parse(decoded)
rescue StandardError => e
  raise ArgumentError, "Invalid protected header: #{e.message}"
end

#uses_algorithm?(alg) ⇒ Boolean

Check if the signature uses a specific algorithm

Parameters:

  • alg (String)

    The algorithm to check

Returns:

  • (Boolean)

    True if the signature uses the algorithm



501
502
503
# File 'lib/a2a/types/agent_card.rb', line 501

def uses_algorithm?(alg)
  algorithm == alg
end

#validate!Object (private)



507
508
509
510
511
512
513
514
# File 'lib/a2a/types/agent_card.rb', line 507

def validate!
  validate_required(:signature, :protected_header)
  validate_type(:signature, String)
  validate_type(:protected_header, String)
  validate_base64url_format(:signature)
  validate_base64url_format(:protected_header)
  validate_protected_header_content
end

#validate_base64url_format(field) ⇒ Object (private)

Validate base64url format

Parameters:

  • field (Symbol)

    The field name

Raises:

  • (ArgumentError)


520
521
522
523
524
525
# File 'lib/a2a/types/agent_card.rb', line 520

def validate_base64url_format(field)
  value = instance_variable_get("@#{field}")
  return if value.match?(/\A[A-Za-z0-9_-]+\z/)

  raise ArgumentError, "#{field} must be valid base64url encoded"
end

#validate_protected_header_contentObject (private)

Validate protected header content



529
530
531
532
533
534
535
536
537
# File 'lib/a2a/types/agent_card.rb', line 529

def validate_protected_header_content
  header = decoded_header
  unless header.is_a?(Hash) && header["alg"]
    raise ArgumentError,
          "protected_header must contain a valid algorithm"
  end
rescue StandardError => e
  raise ArgumentError, "protected_header must be valid base64url encoded JSON: #{e.message}"
end