+sub show_login_page($$) {
+ my ($self, $login_failed) = @_;
+
+ $self->show_common_header;
+
+ print "<div class='gal-login'>\n";
+ print $self->extras('WebLoginExtras');
+
+ my $wrong = $login_failed ? " class='gal-login-bad'" : "";
+ print "\t<form method=POST action='.'>\n";
+ print "\t\t<input type=password name=pw$wrong>\n";
+ print "\t\t<input type=submit value='Login'>\n";
+ print "\t</form>\n";
+
+ print "</div>\n";
+ $self->html_bot;
+}
+
+sub auth_check($) {
+ my ($self) = @_;
+
+ my $needed = $self->auth_get_needed;
+ @$needed or return 1;
+
+ if (length $auth_password) {
+ my $passwords = $self->get('AuthPasswords');
+ my $match = 0;
+ for my $zone (@$needed) {
+ if (defined $passwords->{$zone} && $passwords->{$zone} eq $auth_password) {
+ my @opts = ();
+ my $path = $self->try_get('AuthCookiePath');
+ push @opts, 'path', $path if defined $path;
+ push @opts, 'secure', undef if $self->get('AuthCookieSecure');
+ UCW::CGI::set_cookie($self->get('AuthCookiePrefix') . $zone, $self->auth_zone_hash($zone), @opts);
+ $match++;
+ }
+ }
+
+ if ($match) {
+ my $abs = $self->try_get('WebAbsURL');
+ if (defined $abs) {
+ print "Status: 303\n";
+ print "Location: $abs\n\n";
+ exit 0;
+ }
+ return 1;
+ }
+
+ $self->show_login_page($match == 0);
+ return;
+ }
+
+ my $known_tokens = $self->auth_parse_cookies;
+ for my $zone (@$needed) {
+ return 1 if $known_tokens->{$zone};
+ }
+
+ $self->show_login_page(0);
+ return;
+}
+
+sub auth_get_needed($) {
+ my ($self) = @_;
+
+ my $auth = $self->get('AuthNeeded');
+ defined $auth or return [];
+ if (ref $auth) {
+ return $auth;
+ } else {
+ $auth ne "" or return [];
+ return [$auth];
+ }
+}
+
+sub auth_parse_cookies($) {
+ my ($self) = @_;
+ my $known_tokens = {};
+
+ my %cookies = UCW::CGI::parse_cookies;
+ my $px = $self->get('AuthCookiePrefix');
+ for my $k (keys %cookies) {
+ if (substr($k, 0, length $px) eq $px) {
+ my $zone = substr($k, length $px);
+ my $v = $cookies{$k};
+ if ($v eq $self->auth_zone_hash($zone)) {
+ $known_tokens->{$zone} = 1;
+ } else {
+ print STDERR "Gallery: Invalid auth cookie for zone $zone\n";
+ }
+ }
+ }
+
+ return $known_tokens;
+}
+
+sub auth_zone_hash($) {
+ my ($self, $zone) = @_;
+ my $secret = $self->get('AuthSecret');
+ return substr(Digest::SHA::hmac_sha256_hex($zone, $secret), 0, 16);
+}
+