Sha256: a005f153e7389ce9fec9f6a71accc2c0b00568fd3e70e6e8f7ed0c39c65b4f02

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

module Slosilo
  # A mixin module which simplifies generating signed and encrypted requests.
  # It's designed to be mixed into a standard Net::HTTPRequest object
  # and ensures the request is signed and optionally encrypted before execution.
  # Requests prepared this way will be recognized by Slosilo::Rack::Middleware.
  #
  # As an example, you can use it with RestClient like so:
  #   RestClient.add_before_execution_proc do |req, params|
  #     require 'slosilo'
  #     req.extend Slosilo::HTTPRequest
  #     req.keyname = :somekey
  #   end
  #
  # The request won't be encrypted unless you set the destination keyname.
  
  module HTTPRequest
    # Encrypt the request with key named @keyname from Slosilo::Keystore. 
    # If calling this manually, make sure to encrypt before signing.
    def encrypt!
      return unless @keyname
      return unless body && !body.empty?
      self.body, key = Slosilo[@keyname].encrypt body
      self['X-Slosilo-Key'] = Base64::urlsafe_encode64 key
    end
    
    # Sign the request with :own key from Slosilo::Keystore. 
    # If calling this manually, make sure to encrypt before signing.
    def sign!
      token = Slosilo[:own].signed_token signed_data
      self['Timestamp'] = token["timestamp"]
      self['X-Slosilo-Signature'] = token["signature"]
    end
    
    # Build the data hash to sign.
    def signed_data
      data = { "path" => path, "body" => [body].pack('m0') }
      if key = self['X-Slosilo-Key']
        data["key"] = key
      end
      if authz = self['Authorization']
        data["authorization"] = authz
      end
      data
    end
    
    # Encrypt, sign and execute the request.
    def exec *a
      # we need to hook here because the body might be set
      # in several ways and here it's hopefully finalized
      encrypt!
      sign!
      super *a
    end
    
    # Name of the key used to encrypt the request. 
    # Use it to establish the identity of the receiver.
    attr_accessor :keyname
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slosilo-0.4.1 lib/slosilo/http_request.rb
slosilo-0.4.0 lib/slosilo/http_request.rb
slosilo-0.2.4 lib/slosilo/http_request.rb
slosilo-0.2.3 lib/slosilo/http_request.rb
slosilo-0.2.2 lib/slosilo/http_request.rb
slosilo-0.1.2 lib/slosilo/http_request.rb