Skip to content

Commit

Permalink
Support generic messages (#314)
Browse files Browse the repository at this point in the history
* Support generic messages

* Support generic messages
  • Loading branch information
adriacidre authored Mar 19, 2024
1 parent c569d92 commit 668161a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
31 changes: 31 additions & 0 deletions lib/messages/generic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2020 Self Group Ltd. All Rights Reserved.

# frozen_string_literal: true

require 'self_msgproto'
require_relative 'base'
require_relative '../ntptime'

module SelfSDK
module Messages
class Generic < Base
def parse(input, envelope=nil)
@input = input
@payload = get_payload input
@id = @payload[:jti]
@from = @payload[:iss]
@to = @payload[:sub]
@audience = payload[:aud]
@expires = @payload[:exp]
@typ = @payload[:typ]
@body = @payload[:msg]

if envelope
issuer = envelope.sender.split(":")
@from = issuer.first
@from_device = issuer.last
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/messages/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
require_relative "voice_summary"
require_relative "document_sign_resp"
require_relative "connection_response"
require_relative "generic"

module SelfSDK
module Messages
class UnmappedMessage < StandardError
end

def self.parse(input, messaging, original=nil)
envelope = nil
body = if input.is_a? String
Expand Down Expand Up @@ -87,7 +88,8 @@ def self.parse(input, messaging, original=nil)
m = VoiceSummary.new(messaging)
m.parse(body, envelope)
else
raise UnmappedMessage.new("Invalid message type #{payload[:typ]}.")
m = Generic.new(messaging)
m.parse(body, envelope)
end
return m
end
Expand Down
1 change: 1 addition & 0 deletions selfsdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Gem::Specification.new do |s|
"lib/messages/voice_busy.rb",
"lib/messages/voice_summary.rb",
"lib/messages/message.rb",
"lib/messages/generic.rb",
"lib/messages/document_sign_resp.rb",
"lib/messages/connection_request.rb",
"lib/messages/connection_response.rb",
Expand Down
17 changes: 13 additions & 4 deletions test/messages/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ class SelfSDKTest < Minitest::Test
end

describe "invalid message type" do
let(:input) { '{"payload":"test_invalid"}' }
let(:input) { '{"protected": "header", "payload":"invalid"}' }
let(:typ) { "invalid" }
def test_parse_invalid_message
expect(jwt).to receive(:decode).with("test_invalid").and_return('{"typ":"invalid"}').once
_{ SelfSDK::Messages.parse(input, messaging) }.must_raise StandardError
let(:client) { double("client") }
let(:body) { '{"typ":"invalid","exp":"'+exp+'","iat":"'+iat+'"}' }
def test_invalid
expect(jwt).to receive(:decode).with("header").and_return('{"kid":"kid"}').once
expect(jwt).to receive(:decode).with("invalid").and_return(body).twice
expect(jwt).to receive(:verify).and_return(true)
expect(messaging).to receive(:client).and_return(client)
expect(messaging).to receive(:jwt).and_return(jwt)
expect(client).to receive(:jwt).and_return(jwt)
expect(client).to receive(:public_key).and_return(double(raw_public_key: "pk1"))
res = SelfSDK::Messages.parse(input, messaging)
assert_equal res.class, SelfSDK::Messages::Generic
end
end

Expand Down

0 comments on commit 668161a

Please sign in to comment.