Sha256: c80a38e043e54733bf938b9a2c3fa29cdc4c98251447fe76d44aee194858e6e1

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require 'lockup/engine'

module Lockup
  extend ActiveSupport::Concern

  included do
    if respond_to?(:before_action)
      before_action :check_for_lockup, except: ['unlock']
    else
      before_filter :check_for_lockup, except: ['unlock']
    end
  end

  def self.from_config(setting, secrets_or_credentials = :credentials)
    return unless Rails.application.respond_to?(secrets_or_credentials)

    store = Rails.application.public_send(secrets_or_credentials)

    store.lockup.respond_to?(:fetch) &&
      store.lockup.fetch(setting, store.public_send("lockup_#{setting}")) ||
      store.public_send("lockup_#{setting}") || store.public_send(setting)
  end

  private

  def check_for_lockup
    return unless respond_to?(:lockup) && lockup_codeword
    return if cookies[:lockup].present? && cookies[:lockup] == lockup_codeword.to_s.downcase

    redirect_to lockup.unlock_path(
      return_to: request.fullpath.split('?lockup_codeword')[0],
      lockup_codeword: params[:lockup_codeword]
    )
  end

  def cookie_lifetime
    @cookie_lifetime ||=
      ENV['COOKIE_LIFETIME_IN_WEEKS'] ||
      ENV['cookie_lifetime_in_weeks'] ||
      Lockup.from_config(:cookie_lifetime_in_weeks, :secrets) ||
      Lockup.from_config(:cookie_lifetime_in_weeks)
  end

  def lockup_codeword
    @lockup_codeword ||=
      ENV['LOCKUP_CODEWORD'] ||
      ENV['lockup_codeword'] ||
      Lockup.from_config(:codeword, :secrets) ||
      Lockup.from_config(:codeword)
  end

  def lockup_cookie_lifetime
    seconds = (cookie_lifetime.to_f * 1.week).to_i
    if seconds > 0
      seconds
    else
      5.years
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lockup-1.6 lib/lockup.rb
lockup-1.5.1 lib/lockup.rb
lockup-1.5 lib/lockup.rb