Sha256: 6356dbb064a603ac596107e0e1c465830be7aac84d73684a5e0d6598055c6b03

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

require 'openssl'

module ChartCandy
  class Authentication
    def self.compact_params(original_params)
      compacted_params = ''

      original_params.each { |k,v| compacted_params << (k.to_s + v.to_s) if not self.reserved_params.include?(k.to_s) }

      return compacted_params
    end

    def self.reserved_params
      ['action', 'class', 'controller', 'format', 'from', 'nature', 'step', 'to', 'token', 'tools', 'update_every', 'version']
    end

    def self.tokenize(str)
      OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, Rails.configuration.secret_token, str.chars.sort.join.gsub('/', ''))
    end

    def initialize(request_url, params={})
      @request_url = request_url
      @params = params
    end

    def expired?
      @params[:timestamp] and Time.parse(@params[:timestamp]) + 12.hours < Time.now
    end

    def valid_token?
      @params[:token] == tokenize(filter_url)
    end

    private

    def filter_url
      filtered_url = @request_url.split('?').first.rpartition('/').first

      return filtered_url + ChartCandy::Authentication.compact_params(@params)
    end

    def tokenize(str)
      ChartCandy::Authentication.tokenize(str)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
chart-candy-0.1.7 lib/chart-candy/authentication.rb
chart-candy-0.1.6 lib/chart-candy/authentication.rb
chart-candy-0.1.5 lib/chart-candy/authentication.rb
chart-candy-0.1.4 lib/chart-candy/authentication.rb
chart-candy-0.1.3 lib/chart-candy/authentication.rb
chart-candy-0.1.2 lib/chart-candy/authentication.rb