diff --git a/lib/messages/generic.rb b/lib/messages/generic.rb new file mode 100644 index 0000000..11f9236 --- /dev/null +++ b/lib/messages/generic.rb @@ -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 diff --git a/lib/messages/message.rb b/lib/messages/message.rb index 0111220..8046ba7 100644 --- a/lib/messages/message.rb +++ b/lib/messages/message.rb @@ -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 @@ -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 diff --git a/selfsdk.gemspec b/selfsdk.gemspec index 3f51022..08bceee 100644 --- a/selfsdk.gemspec +++ b/selfsdk.gemspec @@ -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", diff --git a/test/messages/message_test.rb b/test/messages/message_test.rb index ce4bf4f..ef4cc34 100644 --- a/test/messages/message_test.rb +++ b/test/messages/message_test.rb @@ -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