From ccb82fe054a73a6e662b1998aaee5609c62a3b32 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 22 Apr 2015 11:56:27 +0200 Subject: Separate configure and build steps. --- lib/Net/MulkyID/Builder.pm | 13 +++-- lib/Net/MulkyID/Setup.pm | 140 ++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 63 deletions(-) (limited to 'lib') diff --git a/lib/Net/MulkyID/Builder.pm b/lib/Net/MulkyID/Builder.pm index bd5058f..2100cf3 100644 --- a/lib/Net/MulkyID/Builder.pm +++ b/lib/Net/MulkyID/Builder.pm @@ -10,14 +10,19 @@ sub new { my ($class, @args) = @_; my $self = $class->SUPER::new(@args); return bless $self, $class; - #my $self = $self->SUPER::new(); - #return $self; - #return bless {}, shift; +} + +sub ACTION_configure { + my ($self, @args) = @_; + eval "use Net::MulkyID::Setup; configure();"; + if ($@) { + die $@; + } } sub ACTION_build { my ($self, @args) = @_; - eval "use Net::MulkyID::Setup; setup();"; + eval "use Net::MulkyID::Setup; build();"; if ($@) { die $@; } diff --git a/lib/Net/MulkyID/Setup.pm b/lib/Net/MulkyID/Setup.pm index 59ad0d6..2709c87 100755 --- a/lib/Net/MulkyID/Setup.pm +++ b/lib/Net/MulkyID/Setup.pm @@ -15,13 +15,15 @@ use Data::Dumper; #use autodie; use base 'Exporter'; -our @EXPORT = qw(setup); +our @EXPORT = qw(configure build); + +our $conffile = "www/config.pl"; sub prompt($$) { my ($question, $default) = @_; $|++; print "${question} \[${default}\] "; - $_ = <>; + $_ = ; chomp; if ($_) { return $_; @@ -39,15 +41,85 @@ sub makespec($$) { "provisioning" => "$basepath/provision.html"}); }; -sub setup() { - my $conffile = "www/config.pl"; - - # Generate configuration file. +sub load_configuration() { $::MULKONF = { }; if (stat($conffile)) { say "Found existing configuration ($conffile)."; do $conffile; } +} + +sub write_configuration() { + write_file($conffile, <Dump([$::MULKONF], ["::MULKONF"])]} +1; +EOF + ) or die "Could not write configuration to $conffile: $!"; + say "Configuration saved to: $conffile"; + + say ""; + say "******************************************************************"; + say "* FINISHED. *"; + say "* *"; + say "* Please put browserid.json where it will be served as *"; + say "* https:///.well-known/browserid *"; + say "* with a content type of: *"; + say "* application/json *"; + say "* *"; + say "* In addition, please ensure that the private key file can be *"; + say "* read by the web server by assigning the file to the *"; + say "* appropriate owner. *"; + say "******************************************************************"; +} + +sub build_deps() { + # Download jQuery. + make_path("www/jquery"); + if (stat("www/jquery/jquery.js")) { + say "Using existing copy of jQuery (www/jquery/jquery.js)."; + } else { + say "Fetching jQuery..."; + getstore("http://code.jquery.com/jquery-1.7.2.min.js", "www/jquery/jquery.js") + or die "Could not fetch jQuery"; + say "jQuery saved to: www/jquery/jquery.js"; + } +} + +sub build_spec() { + # Generate the private key and generate the BrowserID spec file. + my $pemfile = $::MULKONF->{pemfile} // die "pemfile not defined"; + my $basepath = $::MULKONF->{basepath} // die "basepath not defined"; + my $configpath = $::MULKONF->{configpath} // die "configpath not defined"; + + my $key; + if (stat($pemfile)) { + say "Using existing private key ($pemfile)."; + $key = Crypt::OpenSSL::RSA->new_private_key(scalar read_file($pemfile)); + } else { + say "Generating private key..."; + $key = Crypt::OpenSSL::RSA->generate_key(2048); + make_path($configpath); + write_file($pemfile, $key->get_private_key_string()) + or die "Could not write private key to $pemfile: $!"; + say "Private key saved to: $pemfile"; + chmod 0440, $pemfile; + } + + write_file("browserid.json", makespec($key, $basepath)) + or die "Could not write spec to browserid.json: $!"; + say "Persona spec file saved to: browserid.json"; +} + +sub build() { + load_configuration; + build_deps; + build_spec; +} + +sub configure() { + load_configuration; my $configpath = $::MULKONF->{configpath} // "/etc/mulkyid"; my $pemfile = $::MULKONF->{pemfile} // "$configpath/rsa2048.pem"; @@ -88,38 +160,6 @@ sub setup() { say "OK."; - # Download jQuery. - make_path("www/jquery"); - if (stat("www/jquery/jquery.js")) { - say "Using existing copy of jQuery (www/jquery/jquery.js)."; - } else { - say "Fetching jQuery..."; - getstore("http://code.jquery.com/jquery-1.7.2.min.js", "www/jquery/jquery.js") - or die "Could not fetch jQuery"; - say "jQuery saved to: www/jquery/jquery.js"; - } - - # Generate the private key. - my $key; - if (stat($pemfile)) { - say "Using existing private key ($pemfile)."; - $key = Crypt::OpenSSL::RSA->new_private_key(scalar read_file($pemfile)); - } else { - say "Generating private key..."; - $key = Crypt::OpenSSL::RSA->generate_key(2048); - make_path($configpath); - write_file($pemfile, $key->get_private_key_string()) - or die "Could not write private key to $pemfile: $!"; - say "Private key saved to: $pemfile"; - chmod 0440, $pemfile; - } - - # Generate spec file. - write_file("browserid.json", makespec($key, $basepath)) - or die "Could not write spec to browserid.json: $!"; - say "Persona spec file saved to: browserid.json"; - - # Generate configuration file. $::MULKONF = { configpath => $configpath, pemfile => $pemfile, @@ -133,28 +173,10 @@ sub setup() { google_oauth2_client_secret => $google_oauth2_client_secret, google_oauth2_client_id => $google_oauth2_client_id }; - write_file($conffile, <Dump([$::MULKONF], ["::MULKONF"])]} -1; -EOF - ) or die "Could not write configuration to $conffile: $!"; - say "Configuration saved to: $conffile"; - say ""; - say "******************************************************************"; - say "* FINISHED. *"; - say "* *"; - say "* Please put browserid.json where it will be served as *"; - say "* https:///.well-known/browserid *"; - say "* with a content type of: *"; - say "* application/json *"; - say "* *"; - say "* In addition, please ensure that the private key file can be *"; - say "* read by the web server by assigning the file to the *"; - say "* appropriate owner. *"; - say "******************************************************************"; + build_deps; + build_spec; + write_configuration; } 1; -- cgit v1.2.3