Class: WSDL::Security::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl/security/timestamp.rb

Overview

Represents a WS-Security Timestamp element.

The Timestamp element provides freshness guarantees by including creation and expiration times. It's commonly signed to prevent replay attacks.

Examples:

Basic usage

timestamp = Timestamp.new
timestamp.to_xml(builder)

With custom expiration

timestamp = Timestamp.new(expires_in: 600) # 10 minutes

See Also:

Constant Summary collapse

DEFAULT_TTL =

Default time-to-live in seconds (5 minutes)

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(created_at: nil, expires_in: DEFAULT_TTL, expires_at: nil, id: nil) ⇒ Timestamp

Creates a new Timestamp instance.

Parameters:

  • created_at (Time, nil) (defaults to: nil)

    the creation time (defaults to current UTC time)

  • expires_in (Integer) (defaults to: DEFAULT_TTL)

    seconds until expiration (default: 300)

  • expires_at (Time, nil) (defaults to: nil)

    explicit expiration time (overrides expires_in)

  • id (String, nil) (defaults to: nil)

    the wsu:Id attribute (auto-generated if nil)



45
46
47
48
49
# File 'lib/wsdl/security/timestamp.rb', line 45

def initialize(created_at: nil, expires_in: DEFAULT_TTL, expires_at: nil, id: nil)
  @created_at = (created_at || Time.now).utc
  @expires_at = expires_at&.utc || (@created_at + expires_in)
  @id = id || IdGenerator.for('Timestamp')
end

Instance Attribute Details

#created_atTime (readonly)

Returns the creation time.

Returns:

  • (Time)

    the UTC creation time



28
29
30
# File 'lib/wsdl/security/timestamp.rb', line 28

def created_at
  @created_at
end

#expires_atTime (readonly)

Returns the expiration time.

Returns:

  • (Time)

    the UTC expiration time



32
33
34
# File 'lib/wsdl/security/timestamp.rb', line 32

def expires_at
  @expires_at
end

#idString (readonly)

Returns the unique ID for this timestamp element.

Returns:

  • (String)

    the wsu:Id attribute value



36
37
38
# File 'lib/wsdl/security/timestamp.rb', line 36

def id
  @id
end

Instance Method Details

#created_at_xmlString

Returns the created timestamp as an XML Schema dateTime string.

Returns:

  • (String)

    ISO 8601 formatted timestamp



55
56
57
# File 'lib/wsdl/security/timestamp.rb', line 55

def created_at_xml
  @created_at.xmlschema
end

#expired?Boolean

Checks if the timestamp has expired.

Returns:

  • (Boolean)

    true if the current time is past the expiration time



71
72
73
# File 'lib/wsdl/security/timestamp.rb', line 71

def expired?
  Time.now.utc > @expires_at
end

#expires_at_xmlString

Returns the expiration timestamp as an XML Schema dateTime string.

Returns:

  • (String)

    ISO 8601 formatted timestamp



63
64
65
# File 'lib/wsdl/security/timestamp.rb', line 63

def expires_at_xml
  @expires_at.xmlschema
end

#to_hashHash

Returns a Hash representation suitable for Gyoku XML generation.

Returns:

  • (Hash)

    the timestamp structure as a hash



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wsdl/security/timestamp.rb', line 97

def to_hash
  {
    'wsu:Timestamp' => {
      'wsu:Created' => created_at_xml,
      'wsu:Expires' => expires_at_xml,
      :attributes! => {
        'wsu:Timestamp' => { 'wsu:Id' => @id }
      },
      :order! => ['wsu:Created', 'wsu:Expires']
    }
  }
end

#to_xml(xml) ⇒ void

This method returns an undefined value.

Builds the XML representation of the Timestamp element.

Examples:

Output XML structure

<wsu:Timestamp wsu:Id="Timestamp-abc123">
  <wsu:Created>2026-02-01T12:00:00Z</wsu:Created>
  <wsu:Expires>2026-02-01T12:05:00Z</wsu:Expires>
</wsu:Timestamp>

Parameters:

  • xml (Nokogiri::XML::Builder)

    the XML builder



86
87
88
89
90
91
# File 'lib/wsdl/security/timestamp.rb', line 86

def to_xml(xml)
  xml['wsu'].Timestamp('wsu:Id' => @id) do
    xml['wsu'].Created(created_at_xml)
    xml['wsu'].Expires(expires_at_xml)
  end
end