Sha256: 3050a817e66d90d479e803e2dabae40f5f0ef3c1e2095f74f85a3c25f60cf015

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Ahoy
  class MessagesController < ApplicationController
    filters = _process_action_callbacks.map(&:filter) - AhoyEmail.preserve_callbacks
    skip_before_action(*filters, raise: false)
    skip_after_action(*filters, raise: false)
    skip_around_action(*filters, raise: false)

    # legacy
    def open
      send_data Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="), type: "image/gif", disposition: "inline"
    end

    def click
      if params[:id]
        # legacy
        token = params[:id].to_s
        url = params[:url].to_s
        signature = params[:signature].to_s
        expected_signature = OpenSSL::HMAC.hexdigest("SHA1", AhoyEmail::Utils.secret_token, url)
      else
        token = params[:t].to_s
        campaign = params[:c].to_s
        url = params[:u].to_s
        signature = params[:s].to_s
        expected_signature = AhoyEmail::Utils.signature(token: token, campaign: campaign, url: url)
      end

      redirect_options = {}
      redirect_options[:allow_other_host] = true if ActionPack::VERSION::MAJOR >= 7

      if ActiveSupport::SecurityUtils.secure_compare(signature, expected_signature)
        data = {}
        data[:campaign] = campaign if campaign
        data[:token] = token
        data[:url] = url
        data[:controller] = self
        AhoyEmail::Utils.publish(:click, data)

        redirect_to url, **redirect_options
      else
        if AhoyEmail.invalid_redirect_url
          redirect_to AhoyEmail.invalid_redirect_url, **redirect_options
        else
          render plain: "Link expired", status: :not_found
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ahoy_email-2.2.0 app/controllers/ahoy/messages_controller.rb
ahoy_email-2.1.3 app/controllers/ahoy/messages_controller.rb
ahoy_email-2.1.2 app/controllers/ahoy/messages_controller.rb