Sha256: 0f6acd42107790bb42082be22df5145be7b7258770426afca2feaefed19d34eb

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module SimpleAuth
  module MagicLink
    class Verifier
      def self.call(id:, url:, purpose:, time:)
        new.call(id:, url:, purpose:, time:)
      end

      def call(id:, url:, purpose:, time:)
        return unless SignedURL.verified?(url, key: MagicLink.encryption_key)

        uri = URI(url)
        params = Rack::Utils.parse_query(uri.query)
        actual_id = params.fetch("id")

        verify_model(wanted_id: id, actual_id:, purpose:, time:)
      end

      def secure_compare(a, b)
        ActiveSupport::SecurityUtils.secure_compare(a, b)
      end

      def verify_model(wanted_id:, actual_id:, purpose:, time:)
        magic_link = MagicLink.model.find_by(id: wanted_id)

        return unless magic_link
        return unless secure_compare(magic_link.id.to_s, actual_id.to_s)
        return unless magic_link.expires_at >= time
        return unless secure_compare(magic_link.purpose, purpose.to_s)

        return unless magic_link

        magic_link.destroy!

        magic_link.email
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_auth-magic_link-0.0.2 lib/simple_auth/magic_link/verifier.rb