Sha256: f416f9ccbc1f2d0cc14233697aba9d7ec901062f444363086e72635a8b2b7804

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module Blacklight::TokenBasedUser
  extend ActiveSupport::Concern

  included do
    helper_method :encrypt_user_id

    rescue_from Blacklight::Exceptions::ExpiredSessionToken do
      head :unauthorized
    end

  end

  protected

  def token_or_current_or_guest_user
    token_user || current_or_guest_user
  end

  def token_user
    @token_user ||= if params[:encrypted_user_id]
      user_id = decrypt_user_id params[:encrypted_user_id]
      User.find(user_id)
    end
  end

  # Used for #export action, with encrypted user_id.
  def decrypt_user_id(encrypted_user_id)
    user_id, timestamp = message_encryptor.decrypt_and_verify(encrypted_user_id)

    if timestamp < 1.hour.ago
      raise Blacklight::Exceptions::ExpiredSessionToken
    end

    user_id
  end

  # Used for #export action with encrypted user_id, available
  # as a helper method for views.
  def encrypt_user_id(user_id, current_time = nil)
    current_time ||= Time.zone.now
    message_encryptor.encrypt_and_sign([user_id, current_time])
  end

  def export_secret_token
    ActiveSupport::KeyGenerator.new(Rails.application.secrets.secret_key_base).generate_key('encrypted user session key')
  end

  def message_encryptor
    ActiveSupport::MessageEncryptor.new(export_secret_token)
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
blacklight-6.5.0 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.4.2 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.4.1 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.4.0 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.3.3 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.3.1 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.3.0 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.2.0 app/controllers/concerns/blacklight/token_based_user.rb
blacklight-6.1.0 app/controllers/concerns/blacklight/token_based_user.rb