Class: SelfSDK::Messages::Attestation

Inherits:
Object
  • Object
show all
Defined in:
lib/messages/attestation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messaging) ⇒ Attestation

Returns a new instance of Attestation.



8
9
10
# File 'lib/messages/attestation.rb', line 8

def initialize(messaging)
  @messaging = messaging
end

Instance Attribute Details

#audienceObject

Returns the value of attribute audience.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def audience
  @audience
end

#expected_valueObject

Returns the value of attribute expected_value.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def expected_value
  @expected_value
end

#fact_nameObject

Returns the value of attribute fact_name.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def fact_name
  @fact_name
end

#operatorObject

Returns the value of attribute operator.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def operator
  @operator
end

#originObject

Returns the value of attribute origin.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def origin
  @origin
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def source
  @source
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def to
  @to
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def value
  @value
end

#verifiedObject

Returns the value of attribute verified.



6
7
8
# File 'lib/messages/attestation.rb', line 6

def verified
  @verified
end

Instance Method Details

#parse(name, attestation) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/messages/attestation.rb', line 12

def parse(name, attestation)
  payload = JSON.parse(@messaging.jwt.decode(attestation[:payload]), symbolize_names: true)
  @origin = payload[:iss]
  @to = payload[:sub]
  @audience = payload[:aud]
  @source = payload[:source]
  header = JSON.parse(@messaging.jwt.decode(attestation[:protected]), symbolize_names: true)
  @verified = valid_signature?(attestation, header[:kid])
  @expected_value = payload[:expected_value]
  @operator = payload[:operator]
  @fact_name = name.to_s
  unless payload[name].nil?
    @value = payload[name]
  end
end

#signedObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/messages/attestation.rb', line 39

def signed
  o = {
      sub: @to,
      iss: @origin,
      source: @source,
      fact: @fact_name,
      expected_value: @expected_value,
      operator: @operator,
  }
  o[:aud] = @audience unless @audience.nil?
  o[@fact_name.to_sym] = @value
  @messaging.jwt.signed(o)
end

#valid_signature?(body, kid) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (::StandardError)


28
29
30
31
32
33
# File 'lib/messages/attestation.rb', line 28

def valid_signature?(body, kid)
  k = @messaging.client.public_key(@origin, kid).raw_public_key
  raise ::StandardError.new("invalid signature") unless @messaging.jwt.verify(body, k)

  true
end

#validate!(original) ⇒ Object

Raises:

  • (::StandardError)


35
36
37
# File 'lib/messages/attestation.rb', line 35

def validate!(original)
  raise ::StandardError.new("invalid origin") if @to != original.to
end