Sha256: a84160f3a135315a60a1b7629e643bf51e8b2268d993c531fc033b9eb11faeae

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Zetto::Services::Cookie

  class FindSession
    include Zetto::Modules::Crypto

    def initialize(cookies)
      unless cookies.class.to_s == "ActionDispatch::Cookies::CookieJar"
        raise ArgumentError.new(I18n.t('exseptions.need_cookie'))
      end
      @cookies = cookies
    end

    def execute
      token_data = get_token_from_cookies
      if token_data.present?
        get_session_from_db(token_data)
      end
    rescue Exception => e
      Zetto::Services::Info.error_message I18n.t('exseptions.unknown_error', argument: 'Zetto::Services::Cookie::FindSession', current_method: __method__), e
      nil
    end

    private

    def get_token_from_cookies
      @cookies[:rembo]
    end

    def get_session_from_db(token_data)
      data_session = Zetto::Storage::ImpuretyData::Restore.new.execute(token_data)
      return nil if data_session.nil?

      data_token = get_data_of_token(data_session['token'], data_session['hash_step'], Zetto::Config::Params.session_length)
      session = Zetto::Storage::Session::FindBySession.new(data_token[:session_id]).execute

      if secret_hash_correct?(session, data_session['impurity_hash'], data_token[:ciphered_hash])
        session
      end

      session
    end

    def secret_hash_correct?(session, impurity_hash, ciphered_hash)
      return false if session.nil?
      generate_hashing(session.algorithm, impurity_hash) == ciphered_hash
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zetto-2.2.0 lib/zetto/services/cookie/find_session.rb