Sha256: aba717462dc4b059b29317d26a8cbfbad9aa6dc67c897bba52ae6706ac665f87

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module Logical
  module Naf
    class UserSession

      attr_reader :message

      def initialize(signed_message)
        @message = self.class.unsign_message(signed_message)
      end

      def valid?
        message.present? && message[:value].present? &&
          (Time.zone.now - message[:value]) < ::Naf.configuration.
            simple_cluster_authenticator_cookie_expiration_time
      end

      def token_cookie
        self.class.sign_message(self.class.build_token_cookie)
      end

      def self.build_token_cookie
        {
          value: Time.zone.now
        }
      end

      # Sign the provided string using a MessageVerifier.
      def self.sign_message(message)
        self.message_verifier.generate(message) if !message.nil?
      end

      # Unsign the provided string using a MessageVerifier.
      def self.unsign_message(message)
        if message.nil?
          return nil
        end

        begin
          self.message_verifier.verify(message)
        rescue ActiveSupport::MessageVerifier::InvalidSignature
          nil
        end
      end

      # Returns an ActiveSuport MessageVerifier for signing/unsigning strings seeded with the
      # applications secret token.
      def self.message_verifier
        @@message_verifier ||= ActiveSupport::MessageVerifier.new(Rails.application.class.config.secret_token)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
naf-2.1.13 app/models/logical/naf/user_session.rb
naf-2.1.12 app/models/logical/naf/user_session.rb
naf-2.1.11 app/models/logical/naf/user_session.rb
naf-2.1.10 app/models/logical/naf/user_session.rb
naf-2.1.9 app/models/logical/naf/user_session.rb
naf-2.1.8 app/models/logical/naf/user_session.rb