Sha256: 667782907e9de4db594005f32355f79a65f2979829710f493e2028ddf677bf57

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

require 'hull/config'
require 'hull/connection'
require 'hull/request'
require 'base64'
require 'openssl'

module Hull
  class Client

    attr_accessor *Config::VALID_OPTIONS_KEYS

    include Hull::Connection
    include Hull::Request

    # Initializes a new API object
    #
    # @param attrs [Hash]
    # @return [Hull::Client]
    def initialize(attrs={})
      attrs = Hull.options.merge(attrs)
      Config::VALID_OPTIONS_KEYS.each do |key|
        instance_variable_set("@#{key}".to_sym, attrs[key])
      end
    end

    def credentials
      {
        :app_id      => @app_id,
        :app_secret  => @app_secret,
        :user_id     => @user_id,
      }
    end
    
    def app
      return unless app_id
      @app ||= get("/app", :app_id => app_id)
    end


    def read_cookie str
      return if str.nil? || str.length == 0
      JSON.parse(Base64.decode64(str)) rescue nil
    end

    def current_user_id user_id, user_sig
      return unless user_id && user_sig
      time, signature = user_sig.split(".")
      data    = [time, user_id].join("-")
      digest  = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), self.app_secret, data)
      return user_id if digest == signature
    end

    def authenticate_user env
      require 'rack/request'  
      request = Rack::Request.new(env)
      cookie  = request.cookies["hull_#{self.app_id}"]
      user_auth = read_cookie(cookie)
      return unless user_auth
      current_user_id(user_auth['Hull-User-Id'], user_auth['Hull-User-Sig'])
    end

    def user_hash user_infos
      timestamp = Time.now.to_i.to_s
      message = Base64.encode64(user_infos.to_json).gsub("\n", "")
      sig = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), app_secret, [message, timestamp].join(" "))
      [message, sig, timestamp].join(" ")
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hullio-0.3.3 lib/hull/client.rb
hullio-0.3.2 lib/hull/client.rb
hullio-0.3.1 lib/hull/client.rb
hullio-0.3.0 lib/hull/client.rb