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.



12
13
14
# File 'lib/messages/attestation.rb', line 12

def initialize(messaging)
  @messaging = messaging
end

Instance Attribute Details

#audienceObject

Returns the value of attribute audience.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def audience
  @audience
end

#expected_valueObject

Returns the value of attribute expected_value.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def expected_value
  @expected_value
end

#fact_nameObject

Returns the value of attribute fact_name.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def fact_name
  @fact_name
end

#operatorObject

Returns the value of attribute operator.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def operator
  @operator
end

#originObject

Returns the value of attribute origin.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def origin
  @origin
end

#payloadObject

Returns the value of attribute payload.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def payload
  @payload
end

#sourceObject

Returns the value of attribute source.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def source
  @source
end

#toObject

Returns the value of attribute to.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def to
  @to
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def value
  @value
end

#verifiedObject

Returns the value of attribute verified.



10
11
12
# File 'lib/messages/attestation.rb', line 10

def verified
  @verified
end

Instance Method Details

#parse(name, attestation) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/messages/attestation.rb', line 16

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

  if @payload[name].nil?
    return if @payload[:facts].nil?

    @payload[:facts].each do |f|
      if f[:key] == name.to_s
        @value = f[:value]
        break
      end
    end
  else
    @value = @payload[name]
  end
end

#signedObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/messages/attestation.rb', line 53

def signed
  o = {
      sub: @to,
      iss: @origin,
      iat: SelfSDK::Time.now.strftime('%FT%TZ'),
      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)


42
43
44
45
46
47
# File 'lib/messages/attestation.rb', line 42

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)


49
50
51
# File 'lib/messages/attestation.rb', line 49

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