Sha256: 501d1dba6d068429bc2e3d379e18830de7b793be725609f2e171d1ea079c6797

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "pakyow/support/extension"
require "pakyow/support/message_verifier"
require "pakyow/support/safe_string"

module Pakyow
  module Presenter
    class Renderer
      module Behavior
        # @api private
        module InstallAuthenticity
          extend Support::Extension

          apply_extension do
            build do |view, app:|
              if app.config.presenter.embed_authenticity_token && head = view.head
                head.append(Support::SafeStringHelpers.html_safe("<meta name=\"pw-authenticity-token\">"))
                head.append(Support::SafeStringHelpers.html_safe("<meta name=\"pw-authenticity-param\" content=\"#{app.config.security.csrf.param}\">"))
              end
            end

            attach do |presenter|
              presenter.render node: -> {
                node = object.each_significant_node(:meta).find { |meta_node|
                  meta_node.attributes[:name] == "pw-authenticity-token"
                }

                unless node.nil?
                  View.from_object(node)
                end
              } do
                attributes[:content] = @presentables[:__verifier].sign(Support::MessageVerifier.key)
              end
            end

            expose do |connection|
              connection.set(:__verifier, connection.verifier)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/renderer/behavior/install_authenticity.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/renderer/behavior/install_authenticity.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/renderer/behavior/install_authenticity.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/renderer/behavior/install_authenticity.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/renderer/behavior/install_authenticity.rb