interapp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +30 -0
  3. data/app/controllers/interapp/application_controller.rb +11 -0
  4. data/app/controllers/interapp/messages_controller.rb +12 -0
  5. data/app/models/interapp/message.rb +22 -0
  6. data/app/models/interapp/peer.rb +23 -0
  7. data/app/services/interapp/receive_message_service.rb +27 -0
  8. data/app/services/interapp/send_message_service.rb +26 -0
  9. data/config/routes.rb +3 -0
  10. data/lib/interapp.rb +23 -0
  11. data/lib/interapp/configuration.rb +18 -0
  12. data/lib/interapp/cryptography.rb +30 -0
  13. data/lib/interapp/engine.rb +5 -0
  14. data/lib/interapp/errors.rb +4 -0
  15. data/lib/interapp/version.rb +3 -0
  16. data/lib/tasks/interapp_tasks.rake +12 -0
  17. data/spec/dummy/README.rdoc +28 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/bin/bundle +3 -0
  25. data/spec/dummy/bin/rails +4 -0
  26. data/spec/dummy/bin/rake +4 -0
  27. data/spec/dummy/config.ru +4 -0
  28. data/spec/dummy/config/application.rb +23 -0
  29. data/spec/dummy/config/boot.rb +5 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +37 -0
  33. data/spec/dummy/config/environments/production.rb +83 -0
  34. data/spec/dummy/config/environments/test.rb +39 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  37. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/spec/dummy/config/initializers/inflections.rb +16 -0
  39. data/spec/dummy/config/initializers/interapp.rb +21 -0
  40. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  41. data/spec/dummy/config/initializers/session_store.rb +3 -0
  42. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/spec/dummy/config/locales/en.yml +23 -0
  44. data/spec/dummy/config/routes.rb +3 -0
  45. data/spec/dummy/config/secrets.yml +22 -0
  46. data/spec/dummy/db/development.sqlite3 +0 -0
  47. data/spec/dummy/db/test.sqlite3 +0 -0
  48. data/spec/dummy/log/test.log +264 -0
  49. data/spec/dummy/public/404.html +67 -0
  50. data/spec/dummy/public/422.html +67 -0
  51. data/spec/dummy/public/500.html +66 -0
  52. data/spec/dummy/public/favicon.ico +0 -0
  53. data/spec/dummy/tmp/pids/server.pid +1 -0
  54. data/spec/models/message_spec.rb +53 -0
  55. data/spec/requests/interapp_spec.rb +59 -0
  56. data/spec/services/receive_message_service_spec.rb +49 -0
  57. data/spec/services/send_message_service_spec.rb +30 -0
  58. data/spec/spec_helper.rb +14 -0
  59. metadata +220 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 98508f5b24fd144e338df835ef0d7e1271908353
4
+ data.tar.gz: 0e479439cb499db15074f9d9de3af0d9544ebced
5
+ SHA512:
6
+ metadata.gz: 0a9d4cb13ffe1d0fb130e414279359791bea1c5ad66caae9e126e05b8f84e9cf2fbada99314cc54ab7d2a46a5d88a7217bc00001fbbd5710a95bf5530df8060b
7
+ data.tar.gz: 71acba52a9518c671478de8a2ba1a79f908ed1424132373e41baf80b2c135da6e940a0b1a2ef0f324f928d80eed3d78ef5c901c55b8dc6e39e2211dc964d3884
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Interapp'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+ load 'lib/tasks/interapp_tasks.rake'
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rspec/core'
25
+ require 'rspec/core/rake_task'
26
+
27
+ desc "Run all specs in spec directory (excluding plugin specs)"
28
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
29
+
30
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ module Interapp
2
+ class ApplicationController < ActionController::Base
3
+ rescue_from Interapp::SignatureInvalidError do
4
+ render json: { error: "SIGNATURE_INVALID" }, status: 403
5
+ end
6
+
7
+ rescue_from Interapp::UnknownPeerError do
8
+ render json: { error: "UNKNOWN_PEER" }, status: 403
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Interapp
2
+ class MessagesController < ApplicationController
3
+ def create
4
+ Interapp::ReceiveMessageService.new(
5
+ payload: request.body.read,
6
+ peer_identifier: request.headers["X-Interapp-Identifier"],
7
+ signature: request.headers["X-Interapp-Signature"]
8
+ ).perform
9
+ render json: { received_at: Time.now.to_i }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module Interapp
2
+ class Message
3
+ attr_accessor :payload, :peer, :signature
4
+
5
+ def initialize(attributes = {})
6
+ attributes.each { |name, value| send("#{name}=", value) }
7
+ end
8
+
9
+ def verify
10
+ Interapp::Cryptography.verify(payload, signature_decoded, peer.public_key_decoded)
11
+ end
12
+
13
+ def sign
14
+ encoded_signature = Interapp::Cryptography.sign(payload, Interapp.configuration.private_key.to_i(16))
15
+ self.signature = encoded_signature.unpack("H*").first
16
+ end
17
+
18
+ def signature_decoded
19
+ ECDSA::Format::SignatureDerString.decode([signature].pack("H*"))
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module Interapp
2
+ class Peer
3
+ attr_accessor :identifier, :public_key, :endpoint
4
+
5
+ def initialize(attributes = {})
6
+ attributes.each { |name, value| send("#{name}=", value) }
7
+ end
8
+
9
+ def public_key_decoded
10
+ @public_key_decoded ||= ECDSA::Format::PointOctetString.decode(
11
+ [public_key].pack("H*"), Interapp::EC_GROUP
12
+ )
13
+ end
14
+
15
+ def self.all
16
+ Interapp.configuration.peers
17
+ end
18
+
19
+ def self.find(identifier)
20
+ self.all.find{ |peer| peer.identifier == identifier }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ module Interapp
2
+ class ReceiveMessageService
3
+ attr :message, :peer, :data
4
+
5
+ def initialize(payload:, peer_identifier:, signature:)
6
+ find_peer(peer_identifier)
7
+ @message = Message.new(payload: payload, peer: @peer, signature: signature)
8
+ @data = JSON.load(@message.payload)
9
+ end
10
+
11
+ def perform
12
+ if @message.verify
13
+ Interapp.configuration.handler.call(data, message.peer.identifier)
14
+ else
15
+ raise Interapp::SignatureInvalidError
16
+ end
17
+ rescue OpenSSL::ASN1::ASN1Error
18
+ raise Interapp::SignatureInvalidError
19
+ end
20
+
21
+ private
22
+ def find_peer(peer_identifier)
23
+ @peer = Interapp::Peer.find(peer_identifier)
24
+ raise Interapp::UnknownPeerError if @peer.nil?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ module Interapp
2
+ class SendMessageService
3
+ attr :message, :peer, :payload
4
+
5
+ def initialize(data:, peer_identifier:)
6
+ find_peer(peer_identifier)
7
+ @payload = JSON.dump(data)
8
+ @message = Message.new(payload: @payload, peer: peer)
9
+ @message.sign
10
+ end
11
+
12
+ def perform
13
+ RestClient.post(peer.endpoint, message.payload, {
14
+ content_type: 'application/json',
15
+ "X-Interapp-Identifier" => Interapp.configuration.identifier,
16
+ "X-Interapp-Signature" => message.signature
17
+ })
18
+ end
19
+
20
+ private
21
+ def find_peer(peer_identifier)
22
+ @peer = Interapp::Peer.find(peer_identifier)
23
+ raise Interapp::UnknownPeerError if @peer.nil?
24
+ end
25
+ end
26
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Interapp::Engine.routes.draw do
2
+ post "/" => "messages#create", as: :root
3
+ end
data/lib/interapp.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "ecdsa"
2
+ require "rest-client"
3
+ require "interapp/engine"
4
+ require "interapp/configuration"
5
+ require "interapp/cryptography"
6
+ require "interapp/errors"
7
+
8
+ module Interapp
9
+ EC_GROUP = ECDSA::Group::Secp256k1
10
+
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.configure
16
+ self.configuration ||= Interapp::Configuration.new
17
+ yield(configuration)
18
+ end
19
+
20
+ def self.send_to(peer_identifier, data)
21
+ Interapp::SendMessageService.new(data: data, peer_identifier: peer_identifier)
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module Interapp
2
+ class Configuration
3
+ VALID_CONFIG_KEYS = [:identifier, :private_key, :handler, :peers]
4
+
5
+ attr_accessor *VALID_CONFIG_KEYS
6
+
7
+ def on_receive(&block)
8
+ @handler = block
9
+ end
10
+
11
+ def add_peer
12
+ peer = Interapp::Peer.new
13
+ yield(peer)
14
+ @peers ||= []
15
+ @peers << peer
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ module Interapp
2
+ module Cryptography
3
+ def self.generate_keypair
4
+ private_key = 1 + SecureRandom.random_number(group.order - 1)
5
+ public_key = group.generator.multiply_by_scalar(private_key)
6
+ public_key_binary = ECDSA::Format::PointOctetString.encode(public_key, compression: true)
7
+ return private_key.to_s(16), public_key_binary.unpack('H*').first
8
+ end
9
+
10
+ def self.sign(payload, private_key_binary)
11
+ digest = Digest::SHA2.digest(payload)
12
+ signature = nil
13
+ while signature.nil?
14
+ temp_key = 1 + SecureRandom.random_number(group.order - 1)
15
+ signature = ECDSA.sign(group, private_key_binary, digest, temp_key)
16
+ end
17
+ ECDSA::Format::SignatureDerString.encode(signature)
18
+ end
19
+
20
+ def self.verify(payload, signature_decoded, public_key_decoded)
21
+ digest = Digest::SHA2.digest(payload)
22
+ ECDSA.valid_signature?(public_key_decoded, digest, signature_decoded)
23
+ end
24
+
25
+ private
26
+ def self.group
27
+ Interapp::EC_GROUP
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Interapp
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Interapp
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Interapp
2
+ class UnknownPeerError < StandardError; end
3
+ class SignatureInvalidError < StandardError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Interapp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ # desc "Explaining what the task does"
2
+ # task :interapp do
3
+ # # Task goes here
4
+ # end
5
+
6
+ namespace :interapp do
7
+ desc "Generate a private key and its public key"
8
+ task :keypair do
9
+ keypair = Interapp::Cryptography.generate_keypair
10
+ puts "Private Key:\n#{keypair[0]}\nPublic Key:\n#{keypair[1]}"
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "interapp"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
23
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ #
7
+ default: &default
8
+ adapter: sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ development:
13
+ <<: *default
14
+ database: db/development.sqlite3
15
+
16
+ # Warning: The database defined as "test" will be erased and
17
+ # re-generated from your development database when you run "rake".
18
+ # Do not set this db to the same as development or production.
19
+ test:
20
+ <<: *default
21
+ database: db/test.sqlite3
22
+
23
+ production:
24
+ <<: *default
25
+ database: db/production.sqlite3