Sha256: 3ba1457d1d00b7ea257b064861d93e530342f2997f033e9e0f82e112b7e6c2ea

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Gatleon
  module Rails
    module Authform
      class Concern < Module
        def initialize(public_key:,
                       secret_key:,
                       current_user_method_name: "current_user",
                       _authform_base_url: "https://authform.gatleon.com")
          super() do
            extend ActiveSupport::Concern

            included do
              helper_method "#{current_user_method_name}".to_sym
              before_action :_exchange_user_voucher_for_user
            end

            private

            # defaults to current_user
            define_method current_user_method_name do
              begin
                JSON.parse(cookies[_authform_user_cookie_key])["data"]
              rescue
                nil
              end
            end

            define_method :_exchange_user_voucher_for_user do
              if params[:_authformForm] == public_key && params[:_authformUserVoucher]
                # TODO: headers for api verification
                
                uri = URI("#{_authform_base_url}/v1/exchangeUserVoucherForUser/#{params[:_authformUserVoucher]}")
                response = Net::HTTP.get_response(uri)

                if response.code.to_i == 200
                  cookies[_authform_user_cookie_key] = {
                    value: response.body,
                    domain: :all
                  }
                end

                q = Rack::Utils.parse_query(URI.parse(request.url).query)
                q.delete("_authformUserVoucher")
                q.delete("_authformForm")
                url = q.empty? ? request.path : "#{request.path}?#{q.to_query}"

                redirect_to url, status: 302 # redirect to finish removal of query param
              end
            end

            define_method :_authform_user_cookie_key do
              public_key # allows for multiple forms per site
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gatleon-rails-0.2.0 lib/gatleon/rails/authform/concern.rb