Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Support basic auth #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/shrimp/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Configuration
attr_accessor :default_options
attr_writer :phantomjs

[:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file, :viewport_width, :viewport_height, :max_redirect_count].each do |m|
[:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout,
:rendering_time, :command_config_file, :viewport_width, :viewport_height,
:max_redirect_count, :basic_auth_username, :basic_auth_password].each do |m|
define_method("#{m}=") do |val|
@default_options[m]=val
end
Expand All @@ -23,7 +25,9 @@ def initialize
:command_config_file => File.expand_path('../config.json', __FILE__),
:viewport_width => 600,
:viewport_height => 600,
:max_redirect_count => 0
:max_redirect_count => 0,
:basic_auth_username => nil,
:basic_auth_password => nil,
}
end

Expand Down
6 changes: 4 additions & 2 deletions lib/shrimp/phantom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def cmd
timeout,
viewport_width,
viewport_height,
max_redirect_count
].join(" ")
max_redirect_count,
options[:basic_auth_username],
options[:basic_auth_password]
].join(" ").rstrip
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 added rstrip to not break existing specs, in the case that basic auth fields are nil.

end

# Public: initializes a new Phantom Object
Expand Down
10 changes: 8 additions & 2 deletions lib/shrimp/rasterize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var
viewport_width = system.args[10] || 600,
viewport_height = system.args[11] || 600,
redirects_num = system.args[12] || 0,
basic_auth_username = system.args[13],
basic_auth_password = system.args[14],
cookies = {},
address, output, size;

Expand All @@ -21,7 +23,7 @@ function error(msg) {
}

function print_usage() {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height] [max_redirects_count]');
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height] [max_redirects_count] [basic_auth_username] [basic_auth_password]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
}

Expand Down Expand Up @@ -110,7 +112,7 @@ if (cookie_file) {
phantom.cookies = cookies;
}

if (system.args.length < 3 || system.args.length > 13) {
if (system.args.length < 3 || system.args.length > 15) {
print_usage() && phantom.exit(2);
} else {
address = system.args[1];
Expand All @@ -120,6 +122,10 @@ if (system.args.length < 3 || system.args.length > 13) {
viewportSize: {
width: viewport_width,
height: viewport_height
},
settings: {
userName: basic_auth_username,
password: basic_auth_password
}
};

Expand Down
18 changes: 18 additions & 0 deletions spec/shrimp/phantom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def valid_pdf(io)
phantom.cmd.should end_with " 10"
end

it "should pass basic auth options into cmd line" do
basic_auth_username = 'frodo9finger'
basic_auth_password = 'foobarbaz'
phantom = Shrimp::Phantom.new("file://#{testfile}", {
:margin => "2cm",
:max_redirect_count => 10,
:basic_auth_username => basic_auth_username,
:basic_auth_password => basic_auth_password }, { },
"#{Dir.tmpdir}/test.pdf"
)

phantom.cmd.should include "test.pdf A4 1 2cm portrait"
phantom.cmd.should include "file://#{testfile}"
phantom.cmd.should include "lib/shrimp/rasterize.js"
phantom.cmd.should include basic_auth_username
phantom.cmd.should end_with basic_auth_password
end

it "should properly escape arguments" do
malicious_uri = "file:///hello';shutdown"
bogus_phantom = Shrimp::Phantom.new(malicious_uri)
Expand Down