excid3 / ferrum_pdf

A PDF generator for Rails using Ferrum & headless Chrome
MIT License
198 stars 6 forks source link

Handle Authentication for Non-middleware Rendering #7

Closed albarrync closed 1 month ago

albarrync commented 1 month ago

When calling render_pdf() or render_screenshot() from a controller action, the user's auth allows for rendering the HTML and no additional authentication is required. However, when rendering an object directly, the headless Chrome instance doesn't have a way to authenticate.

Grover handles this by allowing users to pass in username: and password: kwargs:

# Grover
Grover.new('<some URI with basic authentication', username: 'the username', password: 'super secret').to_pdf

Ferrum has the ability to add basic auth credentials to a page:

# Ferrum
page.network.authorize(user: "login", password: "pass") { |req| req.continue }

I was able to patch in a less-than-robust solution by accepting credentials in each render_thing() method and then add the Authorization: Basic header to the Ferrum.page object in the ferrum_pdf gem here, but this may not be the best long-term solution.

albarrync commented 1 month ago

Awesome 🎉