| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::RegisteredEditorsOnly - only known users may edit Phoebe wiki pages | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This extension limits editing to registered editors only. In order to register | ||
| 26 | an editor, you need to know the client certificate's fingerprint, and add it to | ||
| 27 | the Phoebe wiki F<config> file. Do this by setting C<@known_fingerprints>. | ||
| 28 | Here’s an example: | ||
| 29 | |||
| 30 | package App::Phoebe; | ||
| 31 | our @known_fingerprints = qw( | ||
| 32 | sha256$fce75346ccbcf0da647e887271c3d3666ef8c7b181f2a3b22e976ddc8fa38401 | ||
| 33 | sha256$54c0b95dd56aebac1432a3665107d3aec0d4e28fef905020ed6762db49e84ee1); | ||
| 34 | use App::Phoebe::RegisteredEditorsOnly; | ||
| 35 | our $server->{wiki_token} = []; # no tokens | ||
| 36 | 1; | ||
| 37 | |||
| 38 | If you have your editor’s client certificate (not their key!), run the | ||
| 39 | following to get the fingerprint: | ||
| 40 | |||
| 41 | openssl x509 -in client-cert.pem -noout -sha256 -fingerprint \ | ||
| 42 | | sed -e 's/://g' -e 's/SHA256 Fingerprint=/sha256$/' \ | ||
| 43 | | tr [:upper:] [:lower:] | ||
| 44 | |||
| 45 | This should give you the fingerprint in the correct format to add to the list | ||
| 46 | above. Add it, and restart Phoebe. | ||
| 47 | |||
| 48 | If a visitor uses a fingerprint that Phoebe doesn’t know, the fingerprint is | ||
| 49 | printed in the log (if your log level is set to “info” or more), so you can get | ||
| 50 | it from there in case the user can’t send you their client certificate, or tell | ||
| 51 | you what the fingerprint is. | ||
| 52 | |||
| 53 | You should also have a login link somewhere such that people can login | ||
| 54 | immediately. If they don’t, and they try to save, their client is going to ask | ||
| 55 | them for a certificate and their edits may or may not be lost. It depends. 😅 | ||
| 56 | |||
| 57 | => /login Login | ||
| 58 | |||
| 59 | This code works by intercepting all C<titan:> links, and all web edit requests. | ||
| 60 | If you allow editing via the web using L<App::Phoebe::WebEdit>, then those also | ||
| 61 | require a valid client certificate – and setting these up in a web browser are | ||
| 62 | not easy. Be prepared to explain how to do this to your users! | ||
| 63 | |||
| 64 | This code does I<not> prevent simple comments using L<App::Phoebe::Comments> or | ||
| 65 | L<App::Phoebe::WebComments>. People can still leave comments, if you use these | ||
| 66 | modules. This can be a problem: if only registered users can edit the site, you | ||
| 67 | probably don’t want a token; if anonymous users can comment, you probably want a | ||
| 68 | token. There is currently no solution for this. Choose one or the other. If you | ||
| 69 | choose both, registered users might have to provide a token, which might annoy | ||
| 70 | them. | ||
| 71 | |||
| 72 | Here’s an example config that allows reading and editing via the web, but only | ||
| 73 | for users with known fingerprints, with no comments and no tokens: | ||
| 74 | |||
| 75 | # tested by t/example-registered-editors-only.t | ||
| 76 | package App::Phoebe; | ||
| 77 | use App::Phoebe::Web; | ||
| 78 | use App::Phoebe::WebEdit; | ||
| 79 | use App::Phoebe::RegisteredEditorsOnly; | ||
| 80 | our @known_fingerprints = qw( | ||
| 81 | sha256$0ba6ba61da1385890f611439590f2f0758760708d1375859b2184dcd8f855a00); | ||
| 82 | our $server->{wiki_token} = []; # no tokens | ||
| 83 | 1; | ||
| 84 | |||
| 85 | At the time of this writing, here’s a way to do provide a client certificate for | ||
| 86 | Firefox users. First, we need a file in the C<PKCS12> format. On the command | ||
| 87 | line, create this file from the F<cert.pem> and F<key.pem> files you have. | ||
| 88 | Provide no password when you run the command. | ||
| 89 | |||
| 90 | openssl pkcs12 -export -inkey key.pem -in cert.pem -out cert.p12 | ||
| 91 | |||
| 92 | In Firefox, go to “Preferences” → “Privacy & Security” → “Certificates”; under | ||
| 93 | “When a server requests your personal certificate” check the option “Select one | ||
| 94 | automatically”; click on the “View Certificates” button, switch to the “Your | ||
| 95 | Certificates” tab, click on “Import…” and pick the F<cert.p12> file you just | ||
| 96 | created. | ||
| 97 | |||
| 98 | Once you have done this and you visit the Phoebe site, it’ll use the client | ||
| 99 | certificate you provided or it’ll ask you what client certificate to use. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | package App::Phoebe::RegisteredEditorsOnly; | ||
| 104 | use App::Phoebe qw(@request_handlers @extensions @known_fingerprints $log | ||
| 105 | port host_regex space_regex handle_titan result); | ||
| 106 | use Modern::Perl; | ||
| 107 | |||
| 108 | unshift(@request_handlers, '^titan://' => \&protected_titan); | ||
| 109 | |||
| 110 | sub protected_titan { | ||
| 111 | my $stream = shift; | ||
| 112 | my $data = shift; | ||
| 113 | my $hosts = host_regex(); | ||
| 114 | my $spaces = space_regex(); | ||
| 115 | my $port = port($stream); | ||
| 116 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 117 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 118 | $log->info("Successfully identified client certificate"); | ||
| 119 | return handle_titan($stream, $data); | ||
| 120 | } elsif ($fingerprint) { | ||
| 121 | $log->info("Unknown client certificate $fingerprint"); | ||
| 122 | result($stream, "61", "Your client certificate is not authorized for editing"); | ||
| 123 | } else { | ||
| 124 | $log->info("Requested client certificate"); | ||
| 125 | result($stream, "60", "You need a client certificate to edit this wiki"); | ||
| 126 | } | ||
| 127 | $stream->close_gracefully(); | ||
| 128 | } | ||
| 129 | |||
| 130 | # for App::Phoebe::WebEdit | ||
| 131 | unshift(@extensions, \&protect_edit_requests); | ||
| 132 | |||
| 133 | sub protect_edit_requests { | ||
| 134 | my ($stream, $request, $headers, $buffer) = @_; | ||
| 135 | my $host_regex = host_regex(); | ||
| 136 | my $spaces = space_regex(); | ||
| 137 | my $port = port($stream); | ||
| 138 | if ($request =~ m!^GET (?:/($spaces))?/do/edit/([^/#?]+) HTTP/1\.[01]$! | ||
| 139 | or $request =~ m!^POST (?:/($spaces))?/do/edit/([^/#?]+) HTTP/1\.[01]$!) { | ||
| 140 | # we don’t check $space and $host like we do in App::Phoebe::WebEdit! | ||
| 141 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 142 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 143 | $log->info("Successfully identified client certificate via the web"); | ||
| 144 | return 0; # let it be handled by process_edit_request in App::Phoebe::WebEdit! | ||
| 145 | } elsif ($fingerprint) { | ||
| 146 | $log->info("Unknown client certificate $fingerprint via the web"); | ||
| 147 | $stream->write("HTTP/1.1 403 Not authorized\r\n"); | ||
| 148 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 149 | $stream->write("\r\n"); | ||
| 150 | $stream->write("Your client certificate is not authorized for editing\n"); | ||
| 151 | return 1; # we handled it, no further action required | ||
| 152 | } else { | ||
| 153 | $log->info("Requested client certificate via the web"); | ||
| 154 | $stream->write("HTTP/1.1 403 Not authorized\r\n"); | ||
| 155 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 156 | $stream->write("\r\n"); | ||
| 157 | $stream->write("You need a client certificate to edit this wiki"); | ||
| 158 | return 1; # we handled it, no further action required | ||
| 159 | } | ||
| 160 | } | ||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | |||
| 164 | push(@extensions, \®istered_editor_login); | ||
| 165 | |||
| 166 | sub registered_editor_login { | ||
| 167 | my $stream = shift; | ||
| 168 | my $url = shift; | ||
| 169 | my $hosts = host_regex(); | ||
| 170 | my $spaces = space_regex(); | ||
| 171 | my $port = port($stream); | ||
| 172 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 173 | my $host; | ||
| 174 | if (($host) = $url =~ m!^gemini://($hosts)(?::$port)?/login!) { | ||
| 175 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 176 | $log->info("Successfully identified client certificate"); | ||
| 177 | result($stream, "30", "gemini://$host:$port/"); | ||
| 178 | } elsif ($fingerprint) { | ||
| 179 | $log->info("Unknown client certificate $fingerprint"); | ||
| 180 | result($stream, "61", "Your client certificate is not known"); | ||
| 181 | } else { | ||
| 182 | $log->info("Requested client certificate"); | ||
| 183 | result($stream, "60", "You need a client certificate to edit this wiki"); | ||
| 184 | } | ||
| 185 | return 1; | ||
| 186 | } | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; |