Class: OmfCommon::Message::Json::Message
- Inherits:
-
OmfCommon::Message
show all
- Defined in:
- omf_common/lib/omf_common/message/json/json_message.rb
Constant Summary
- @@key2json_key =
This maps properties in the internal representation of a message to names
used for the JSON message
{
operation: :op,
res_id: :rid
}
OMF_CORE_READ, OMF_CORE_WRITE, OMF_NAMESPACE
Instance Attribute Summary
#issuer
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
#[], #[]=, authenticate?, #create_inform_reply_message, #default_props_ns, #error?, #guard?, init, #itype, #props_ns, #resource, #resource_address, #success?
Constructor Details
- (Message) initialize(content, issuer = nil)
Returns a new instance of Message
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 226
def initialize(content, issuer = nil)
debug "Create message: #{content.inspect}"
unless op = content[:op]
raise "Missing message type (:operation)"
end
@content = {}
@issuer = issuer
content[:op] = op.to_sym if src = content[:src]
content[:src] = OmfCommon.comm.create_topic(src)
end
content.each {|k,v| _set_core(k, v)}
@properties = content[:props] || []
@authenticate = self.class.authenticate?
@certOnTopic = {}
end
|
Class Method Details
+ (Object) create(type, properties, body = {})
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 24
def self.create(type, properties, body = {})
if type == :request
unless (req_props = properties).kind_of?(Array)
raise "Expected array, but got #{properties.class} for request message"
end
properties = {}
req_props.each {|n| properties[n] = nil }
elsif not properties.kind_of?(Hash)
raise "Expected hash, but got #{properties.class}"
end
content = body.merge({
op: type,
mid: SecureRandom.uuid,
ts: Time.now.to_i,
props: properties
})
issuer = self.authenticate? ? (body[:issuer] || body[:src]) : nil
self.new(content, issuer)
end
|
46
47
48
49
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 46
def self.create_inform_message(itype = nil, properties = {}, body = {})
body[:itype] = itype if itype
create(:inform, properties, body)
end
|
+ (Object) parse(str, content_type, &block)
Create and authenticate, if necessary a message and pass it on to
'block' if parsing (and authentication) is successful.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 54
def self.parse(str, content_type, &block)
issuer = nil
case content_type.to_s
when 'jwt'
content, issuer = parse_jwt(str, &block)
when 'text/json'
content = JSON.parse(str, :symbolize_names => true)
else
warn "Received message with unknown content type '#{content_type}'"
end
if (content)
msg = new(content, issuer)
block.call(msg)
end
end
|
+ (Object) parse_jwt(jwt_string)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 72
def self.parse_jwt(jwt_string)
key_or_secret = :skip_verification
case jwt_string.count('.')
when 2 , claims, signature = jwt_string.split('.', 3).collect do |segment|
UrlSafeBase64.decode64 segment.to_s
end
, claims = [, claims].collect do |json|
JSON.parse(json, :symbolize_names => true)
end
signature_base_string = jwt_string.split('.')[0, 2].join('.')
jwt = JSON::JWT.new claims
jwt. =
jwt.signature = signature
unless issuer = claims[:iss]
warn "JWT: Message is missing :iss element"
return nil
end
if cert_pem = claims[:crt]
pem = "#{OmfCommon::Auth::Certificate::BEGIN_CERT}#{cert_pem}#{OmfCommon::Auth::Certificate::END_CERT}"
cert = OmfCommon::Auth::Certificate.create_from_pem(pem)
cert.resource_id = issuer
OmfCommon::Auth::CertificateStore.instance.register(cert)
end
unless cert = OmfCommon::Auth::CertificateStore.instance.cert_for(issuer)
warn "JWT: Can't find cert for issuer '#{issuer}'"
return nil
end
unless OmfCommon::Auth::CertificateStore.instance.verify(cert)
warn "JWT: Invalid certificate '#{cert.to_s}', NOT signed by CA certs, or its CA cert NOT loaded into cert store."
return nil
end
jwt.verify signature_base_string, cert.to_x509.public_key [JSON.parse(claims[:cnt], :symbolize_names => true), cert]
else
warn('JWT: Invalid Format. JWT should include 2 or 3 dots.')
return nil
end
end
|
Instance Method Details
- (Object) _get_core(key)
249
250
251
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 249
def _get_core(key)
@content[@@key2json_key[key] || key]
end
|
- (Object) _get_property(key, ns = nil)
258
259
260
261
262
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 258
def _get_property(key, ns = nil)
warn "Can't handle namespaces yet" if ns
@properties[key]
end
|
- (Object) _set_core(key, value)
245
246
247
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 245
def _set_core(key, value)
@content[(@@key2json_key[key] || key).to_sym] = value
end
|
- (Object) _set_property(key, value, ns = nil)
253
254
255
256
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 253
def _set_property(key, value, ns = nil)
warn "Can't handle namespaces yet" if ns
@properties[key] = value
end
|
- (Object) each_bound_request_property(&block)
Loop over all the bound (sent with a value) properties of a request
message.
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 162
def each_bound_request_property(&block)
unless type == :request
raise "Can only be used for request messages"
end
self[:select].each do |el|
if el.is_a? Hash
el.each do |key, value|
block.call(key, value)
end
end
end
end
|
- (Object) each_property(&block)
123
124
125
126
127
128
129
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 123
def each_property(&block)
@properties.each do |k, v|
block.call(k, v)
end
end
|
- (Object) each_unbound_request_property(&block)
Loop over all the unbound (sent without a value) properties of a request
message.
147
148
149
150
151
152
153
154
155
156
157
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 147
def each_unbound_request_property(&block)
unless type == :request
raise "Can only be used for request messages"
end
self[:select].each do |el|
if el.is_a? Symbol
block.call(el)
end
end
end
|
- (Boolean) has_properties?
136
137
138
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 136
def has_properties?
not @properties.empty?
end
|
- (Object) marshall(topic)
Marshall message into a string to be shipped across the network. Depending
on authentication setting, the message will be signed as well, or maybe
even dropped.
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 187
def marshall(topic)
raise "Missing SRC declaration in #{@content}" unless @content[:src]
if @content[:src].is_a? OmfCommon::Comm::Topic
@content[:src] = @content[:src].address
end
@content[:itype] = self.itype(:frcp) unless self.itype(:frcp).nil?
payload = @content.to_json
if self.class.authenticate?
unless issuer = self.issuer
raise "Missing ISSUER for '#{self}'"
end
if issuer.is_a? OmfCommon::Auth::CertificateStore
cert = issuer
issuer = cert.subject
else
cert = OmfCommon::Auth::CertificateStore.instance.cert_for(issuer)
end
if cert && cert.can_sign?
debug "Found cert for '#{issuer} - #{cert}"
msg = {cnt: payload, iss: issuer}
unless @certOnTopic[k = [topic, issuer]]
msg[:crt] = cert.to_pem_compact
end
p = JSON::JWT.new(msg).sign(cert.key , :RS256).to_s
return ['jwt', p]
end
end
['text/json', payload]
end
|
- (Object) properties
131
132
133
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 131
def properties
@properties
end
|
- (Object) to_s
177
178
179
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 177
def to_s
"JsonMessage: #{@content.inspect}"
end
|
- (Boolean) valid?
140
141
142
|
# File 'omf_common/lib/omf_common/message/json/json_message.rb', line 140
def valid?
true end
|