Trog-Vault.t 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #!/usr/bin/env perl
  2. # The vault, the code that authorizes opening it, and the hashing underneath
  3. # both. Nothing here needs a webserver: what is being tested is that a database
  4. # on its own is not enough, and that a code is worth one thing.
  5. use 5.040;
  6. use re '/aasx';
  7. use warnings FATAL => 'all';
  8. use FindBin;
  9. use FindBin::libs;
  10. use Test::More;
  11. use Test::MockModule qw{strict};
  12. our ( $REPO, $ROOT, $OLDCWD );
  13. BEGIN {
  14. require Cwd;
  15. require File::Copy;
  16. require File::Path;
  17. require File::Temp;
  18. $REPO = Cwd::abs_path("$FindBin::Bin/..");
  19. $OLDCWD = Cwd::getcwd();
  20. $ROOT = File::Temp::tempdir( 'tcms-vault-XXXXXX', TMPDIR => 1, CLEANUP => 1 );
  21. $ENV{HOME} = $ROOT;
  22. File::Path::make_path( map { "$ROOT/$_" } qw{config schema logs totp} );
  23. File::Copy::copy( "$REPO/schema/auth.schema", "$ROOT/schema/auth.schema" ) or die $!;
  24. open( my $fh, '>', "$ROOT/config/default.cfg" ) or die $!;
  25. print {$fh} "[general]\n data_model=FlatFile\n hostname=vault.example.com\n";
  26. close $fh;
  27. # The key the way a deployment hands one over: base64, in the environment,
  28. # put there by tpsgi.sh out of a systemd credential. Trog::Vault takes it
  29. # out of %ENV as it loads, which is the behaviour tested below.
  30. require Crypt::PRNG;
  31. require Crypt::Misc;
  32. $ENV{TPSGI_VAULT_KEY} = Crypt::Misc::encode_b64( Crypt::PRNG::random_bytes(32) );
  33. chdir($ROOT) or die "could not chdir to the sandbox: $!";
  34. }
  35. END {
  36. chdir($OLDCWD) if $OLDCWD;
  37. }
  38. require_ok('Trog::Auth') or BAIL_OUT("Can't find Trog::Auth");
  39. require_ok('Trog::Vault') or BAIL_OUT("Can't find SUT");
  40. Trog::Auth::useradd( 'bob', 'Bob Bobson', 'hunter2', ['admin'], 'bob@example.com' )
  41. or BAIL_OUT('could not make a user to test with');
  42. Trog::Auth::useradd( 'eve', 'Eve Evilson', 'letmein', ['public'], 'eve@example.com' )
  43. or BAIL_OUT('could not make a second user');
  44. subtest 'the key is taken out of the environment' => sub {
  45. ok( Trog::Vault::has_key(), 'the key was read' );
  46. ok( !exists $ENV{TPSGI_VAULT_KEY}, 'and is gone from the environment, so nothing we fork inherits it' );
  47. like( Trog::Vault::key_id(), qr/^[0-9a-f]{16}$/, 'and fingerprints to something a row can name' );
  48. };
  49. subtest 'what a secret looks like on disk' => sub {
  50. my ( $ok, $why ) = Trog::Vault::set( 'bob', 'keepass', 'correct horse battery staple' );
  51. is( $ok, 1, 'a secret can be stored' ) or diag($why);
  52. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  53. my $rows = $dbh->selectall_arrayref( "SELECT * FROM user_secret WHERE username='bob'", { Slice => {} } );
  54. is( scalar(@$rows), 1, 'and lands in one row' );
  55. my $row = $rows->[0];
  56. unlike( $row->{ciphertext}, qr/correct[ ]horse/, 'which does not contain the secret' );
  57. is( length( $row->{salt} ), 32, 'carries a fresh 32 byte salt' );
  58. is( length( $row->{tag} ), 16, 'and a GCM tag' );
  59. is( $row->{key_id}, Trog::Vault::key_id(), 'naming the key it was sealed under' );
  60. is( Trog::Vault::get( 'bob', 'keepass' ), 'correct horse battery staple', 'and it comes back out' );
  61. ( $ok, $why ) = Trog::Vault::set(qw{bob pin 0});
  62. is( $ok, 1, 'a secret of "0" is a secret, not nothing' ) or diag($why);
  63. is( Trog::Vault::get( 'bob', 'pin' ), '0', 'and comes back out' );
  64. };
  65. subtest 'a fresh salt every write' => sub {
  66. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  67. Trog::Vault::set( 'bob', 'same', 'the very same secret' );
  68. my ($first) = $dbh->selectrow_array("SELECT hex(ciphertext) FROM user_secret WHERE username='bob' AND name='same'");
  69. Trog::Vault::set( 'bob', 'same', 'the very same secret' );
  70. my ($second) = $dbh->selectrow_array("SELECT hex(ciphertext) FROM user_secret WHERE username='bob' AND name='same'");
  71. # Same plaintext, same key, different ciphertext -- which is what a random
  72. # salt per write buys, and what GCM needs to stay safe.
  73. isnt( $first, $second, 'the same secret stored twice does not look the same twice' );
  74. is( Trog::Vault::get( 'bob', 'same' ), 'the very same secret', 'and still reads back' );
  75. };
  76. subtest 'a row cannot be moved or renamed' => sub {
  77. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  78. # Exactly what somebody with write access to the database would try: take
  79. # the admin's sealed passphrase and make it their own.
  80. $dbh->do("INSERT OR REPLACE INTO user_secret (username, name, key_id, salt, ciphertext, tag, created) SELECT 'eve', name, key_id, salt, ciphertext, tag, created FROM user_secret WHERE username='bob' AND name='keepass'");
  81. is( Trog::Vault::get( 'eve', 'keepass' ), undef, 'a row lifted into another account does not open' );
  82. $dbh->do("INSERT OR REPLACE INTO user_secret (username, name, key_id, salt, ciphertext, tag, created) SELECT 'bob', 'renamed', key_id, salt, ciphertext, tag, created FROM user_secret WHERE username='bob' AND name='keepass'");
  83. is( Trog::Vault::get( 'bob', 'renamed' ), undef, 'nor does one renamed into being the answer to a different question' );
  84. is( Trog::Vault::get( 'bob', 'keepass' ), 'correct horse battery staple', 'while the row it was copied from is untouched' );
  85. $dbh->do("DELETE FROM user_secret WHERE name IN ('renamed') OR username='eve'");
  86. };
  87. subtest 'and not without the key it was sealed under' => sub {
  88. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  89. # A different installation's key, which is what a stolen database meets.
  90. my $theirs = Trog::Vault::key();
  91. {
  92. no warnings qw{once};
  93. local $Trog::Vault::master = Crypt::PRNG::random_bytes(32);
  94. is( Trog::Vault::get( 'bob', 'keepass' ), undef, 'the wrong key opens nothing' );
  95. my $listed = Trog::Vault::list('bob');
  96. my ($keepass) = grep { $_->{name} eq 'keepass' } @$listed;
  97. is( $keepass->{readable}, 0, 'and the listing says so rather than pretending' );
  98. }
  99. is( Trog::Vault::get( 'bob', 'keepass' ), 'correct horse battery staple', 'the right one still does' );
  100. # Tampering, as opposed to the wrong key: the tag is what notices.
  101. $dbh->do("UPDATE user_secret SET ciphertext = ciphertext || X'00' WHERE username='bob' AND name='keepass'");
  102. is( Trog::Vault::get( 'bob', 'keepass' ), undef, 'a row somebody edited does not open either' );
  103. Trog::Vault::set( 'bob', 'keepass', 'correct horse battery staple' );
  104. };
  105. subtest 'the listing never shows a value' => sub {
  106. my $listed = Trog::Vault::list('bob');
  107. ok( scalar(@$listed) >= 2, 'lists what bob has' );
  108. foreach my $secret (@$listed) {
  109. ok( !exists $secret->{ciphertext}, "$secret->{name} is listed without its ciphertext" );
  110. ok( exists $secret->{created}, "$secret->{name} says when it was stored" );
  111. }
  112. is_deeply( Trog::Vault::list('eve'), [], 'and one user cannot see another\'s' );
  113. ok( Trog::Vault::forget( 'bob', 'same' ), 'a secret can be forgotten' );
  114. is( Trog::Vault::get( 'bob', 'same' ), undef, 'after which it is gone' );
  115. is( Trog::Vault::forget( 'bob', 'same' ), 0, 'and forgetting it twice is not a thing that happened' );
  116. };
  117. subtest 'a name is a name' => sub {
  118. foreach my $bad ( '../../etc/passwd', 'has spaces', '', 'a' x 100, '.hidden' ) {
  119. my ( $ok, $why ) = Trog::Vault::set( 'bob', $bad, 'nope' );
  120. is( $ok, 0, "'$bad' is refused as a name" );
  121. }
  122. my ( $ok, $why ) = Trog::Vault::set( 'bob', 'fine', '' );
  123. is( $ok, 0, 'and an empty secret is not a secret' );
  124. };
  125. subtest 'no key, no vault' => sub {
  126. no warnings qw{once};
  127. local $Trog::Vault::master = undef;
  128. local $Trog::Vault::looked_further = 1;
  129. ok( !Trog::Vault::has_key(), 'an installation with no key has no vault' );
  130. my ( $ok, $why ) = Trog::Vault::set( 'bob', 'nowhere', 'to put it' );
  131. is( $ok, 0, 'storing is refused' );
  132. like( $why, qr/no[ ]vault[ ]key/, 'saying why, since this is a thing to go and configure' );
  133. is( Trog::Vault::get( 'bob', 'keepass' ), undef, 'and nothing already stored is offered' );
  134. };
  135. subtest 'the other two places a key can be' => sub {
  136. no warnings qw{once};
  137. require Crypt::Misc;
  138. # A key file, for a machine not running systemd. bin/tcms-vault-key --file
  139. # writes exactly this: base64 and a newline.
  140. my $on_disk = Crypt::Misc::encode_b64( Crypt::PRNG::random_bytes(32) );
  141. open( my $fh, '>', 'config/secrets.key' ) or die $!;
  142. print {$fh} "$on_disk\n";
  143. close $fh;
  144. chmod( oct('600'), 'config/secrets.key' );
  145. {
  146. local $Trog::Vault::master = undef;
  147. local $Trog::Vault::looked_further = 0;
  148. is( Crypt::Misc::encode_b64( Trog::Vault::key() ), $on_disk, 'a key file is found, newline and all' );
  149. }
  150. # And systemd's credential store, which is where it should be: the directory
  151. # systemd hands over, for a service that is not chrooted away from it.
  152. my $in_store = Crypt::Misc::encode_b64( Crypt::PRNG::random_bytes(32) );
  153. File::Path::make_path('creds');
  154. open( $fh, '>', 'creds/tpsgi-vault' ) or die $!;
  155. print {$fh} $in_store;
  156. close $fh;
  157. {
  158. local $ENV{CREDENTIALS_DIRECTORY} = 'creds';
  159. local $Trog::Vault::master = undef;
  160. local $Trog::Vault::looked_further = 0;
  161. is( Crypt::Misc::encode_b64( Trog::Vault::key() ), $in_store, 'the credential store wins over the file beside the database' );
  162. }
  163. # Something that is not a key is not quietly used as one.
  164. open( $fh, '>', 'config/secrets.key' ) or die $!;
  165. print {$fh} "hunter2\n";
  166. close $fh;
  167. {
  168. local $Trog::Vault::master = undef;
  169. local $Trog::Vault::looked_further = 0;
  170. is( Trog::Vault::key(), undef, 'a key that is not 32 bytes of base64 is refused' );
  171. }
  172. unlink('config/secrets.key');
  173. };
  174. subtest 'a code is worth one thing' => sub {
  175. my ( $uri, $qr, $failure, $message, $totp ) = Trog::Auth::totp( 'bob', 'vault.example.com' );
  176. ok( $uri, 'bob is enrolled' ) or diag($message);
  177. my $now = time();
  178. my $code = $totp->expected_totp_code($now);
  179. my ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', $code );
  180. is( $ok, 1, 'the code from their authenticator is accepted' ) or diag($why);
  181. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', $code );
  182. is( $ok, 0, 'and is not accepted a second time' );
  183. like( $why, qr/already[ ]been[ ]used/, 'saying so, since the answer is to wait thirty seconds' );
  184. # A code from before the one just spent is also spent, whatever the
  185. # arithmetic says: that is what walking the counter forward is for.
  186. my $previous = $totp->expected_totp_code( $now - 30 );
  187. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', $previous );
  188. is( $ok, 0, 'nor is an older code from inside the tolerance window' );
  189. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', '000000' );
  190. is( $ok, 0, 'a wrong code is refused' );
  191. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', 'notacode' );
  192. is( $ok, 0, 'and so is something that is not a code at all' );
  193. ( $ok, $why ) = Trog::Auth::spend_totp( 'eve', $totp->expected_totp_code( $now + 30 ) );
  194. is( $ok, 0, 'somebody who has not enrolled cannot spend anything' );
  195. like( $why, qr/no[ ]second[ ]factor/, 'and is told that rather than that their code was wrong' );
  196. # The next window is a new code and works, which is the whole cost of this.
  197. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', $totp->expected_totp_code( $now + 30 ) );
  198. is( $ok, 1, 'the next code works' ) or diag($why);
  199. };
  200. subtest 'passwords are argon2id, and become it' => sub {
  201. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  202. my ($hash) = $dbh->selectrow_array("SELECT hash FROM user WHERE name='bob'");
  203. like( $hash, qr/^\$argon2id\$/, 'a user made today is stored as argon2id' );
  204. ok( Trog::Auth::check_password( 'bob', 'hunter2' ), 'and their password checks out' );
  205. ok( !Trog::Auth::check_password( 'bob', 'hunter3' ), 'while a wrong one does not' );
  206. ok( !Trog::Auth::check_password( 'bob', '' ), 'and neither does nothing at all' );
  207. # A row as it was before this change: one unstretched sha256 of the password
  208. # and the salt beside it.
  209. require Digest::SHA;
  210. $dbh->do( "UPDATE user SET hash=?, salt=? WHERE name=?", undef, Digest::SHA::sha256( 'letmein' . 'oldsalt' ), 'oldsalt', 'eve' );
  211. my ($legacy) = $dbh->selectrow_array("SELECT hash FROM user WHERE name='eve'");
  212. unlike( $legacy, qr/^\$argon2/, 'a row from before this reads as the old kind' );
  213. ok( !Trog::Auth::check_password( 'eve', 'wrong' ), 'which still refuses a wrong password' );
  214. ($legacy) = $dbh->selectrow_array("SELECT hash FROM user WHERE name='eve'");
  215. unlike( $legacy, qr/^\$argon2/, 'and is left alone when it does' );
  216. ok( Trog::Auth::check_password( 'eve', 'letmein' ), 'and accepts the right one' );
  217. my ($upgraded) = $dbh->selectrow_array("SELECT hash FROM user WHERE name='eve'");
  218. like( $upgraded, qr/^\$argon2id\$/, 'rewriting it as argon2id on the way past' );
  219. ok( Trog::Auth::check_password( 'eve', 'letmein' ), 'after which the same password still works' );
  220. # Raising the cost later is a one line change, not a migration, for the same
  221. # reason: the next correct password rewrites the row.
  222. {
  223. no warnings qw{once};
  224. local $Trog::Auth::argon2_time = $Trog::Auth::argon2_time + 1;
  225. ok( Trog::Auth::check_password( 'eve', 'letmein' ), 'a hash made under weaker parameters still verifies' );
  226. my ($rehashed) = $dbh->selectrow_array("SELECT hash FROM user WHERE name='eve'");
  227. isnt( $rehashed, $upgraded, 'and is rewritten under the stronger ones' );
  228. }
  229. };
  230. subtest 'logging in spends a code' => sub {
  231. my ( $uri, $qr, $failure, $message, $totp ) = Trog::Auth::totp( 'bob', 'vault.example.com' );
  232. # The subtest above spent the code for the window we are standing in, and
  233. # the counter only walks forwards. Standing here for thirty seconds would
  234. # say the same thing and cost thirty seconds.
  235. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  236. $dbh->do("DELETE FROM totp_spent WHERE username='bob'");
  237. my $now = time();
  238. my $code = $totp->expected_totp_code($now);
  239. is( Trog::Auth::mksession( 'bob', 'hunter2', '' ), '', 'an enrolled user cannot log in without a code' );
  240. is( Trog::Auth::mksession( 'bob', 'wrong', $code ), '', 'nor with a wrong password' );
  241. # And a login that failed on the password did not spend the code on the way
  242. # past, or a wrong guess would cost the person their next thirty seconds.
  243. ok( Trog::Auth::mksession( 'bob', 'hunter2', $code ), 'the right password and a live code get a session' );
  244. is( Trog::Auth::mksession( 'bob', 'hunter2', $code ), '', 'and that code cannot be replayed into a second one' );
  245. };
  246. subtest 'when the key is gone' => sub {
  247. no warnings qw{once};
  248. require Trog::Routes::HTML;
  249. my $tpsgi = bless {}, 'FakeTPSGI2';
  250. {
  251. no warnings qw{once};
  252. *FakeTPSGI2::see_also = sub { return [ 303, [ Location => $_[1] ], [''] ] };
  253. }
  254. my $rendered;
  255. my $index = Test::MockModule->new('Trog::Routes::HTML');
  256. $index->redefine( index => sub { $rendered = $_[0]; return [ 200, [], [''] ] } );
  257. Trog::Vault::set( 'bob', 'keepass', 'correct horse battery staple' );
  258. # The machine was rebuilt and the credential went with it. A key lives and
  259. # dies with its machine on purpose, so this is the ordinary case rather than
  260. # a disaster: everything sealed under the old one is gone, and the users
  261. # store theirs again.
  262. foreach my $gone (
  263. [ 'no key at all', sub { $Trog::Vault::master = undef; $Trog::Vault::looked_further = 1 } ],
  264. [ 'a different key', sub { $Trog::Vault::master = Crypt::PRNG::random_bytes(32) } ],
  265. ) {
  266. my ( $what, $break ) = @$gone;
  267. local $Trog::Vault::master = Trog::Vault::key();
  268. local $Trog::Vault::looked_further = 1;
  269. $break->();
  270. # Nothing explodes, and nothing claims a secret is usable.
  271. is( Trog::Vault::has( 'bob', 'keepass' ), 0, "$what: nothing reads as usable" );
  272. is( Trog::Vault::get( 'bob', 'keepass' ), undef, "$what: and nothing opens" );
  273. Trog::Routes::HTML::secrets( { user => 'bob', user_acls => ['admin'], tpsgi => $tpsgi } );
  274. my ($listed) = grep { $_->{name} eq 'keepass' } @{ $rendered->{secrets} };
  275. ok( $listed, "$what: the row is still listed rather than vanishing" );
  276. is( $listed->{readable}, 0, "$what: marked as one that will not open" );
  277. # And a page which knows storing cannot work does not draw the form for
  278. # it -- has_vault is what the template hangs that on.
  279. is( $rendered->{has_vault}, ( $what eq 'no key at all' ? 0 : 1 ), "$what: the page knows whether there is a vault to store into" );
  280. my ( $stored, $why ) = Trog::Vault::set( 'bob', 'replacement', 'a new one' );
  281. if ( $what eq 'no key at all' ) {
  282. is( $stored, 0, "$what: storing is refused rather than silently lost" );
  283. like( $why, qr/no[ ]vault[ ]key/, "$what: saying why" );
  284. }
  285. else {
  286. is( $stored, 1, "$what: a new key stores new secrets fine" );
  287. is( Trog::Vault::get( 'bob', 'replacement' ), 'a new one', "$what: and opens them" );
  288. Trog::Vault::forget( 'bob', 'replacement' );
  289. }
  290. }
  291. # The key came back: what was sealed under it was never damaged, only
  292. # unreadable while it was away.
  293. is( Trog::Vault::get( 'bob', 'keepass' ), 'correct horse battery staple', 'and the original key still opens the original row' );
  294. };
  295. subtest 'the page a user manages them from' => sub {
  296. require Trog::Routes::HTML;
  297. my $tpsgi = bless {}, 'FakeTPSGI';
  298. {
  299. no warnings qw{once};
  300. *FakeTPSGI::see_also = sub { return [ 303, [ Location => $_[1] ], [''] ] };
  301. *FakeTPSGI::forbidden = sub { return [ 403, [], ['no'] ] };
  302. }
  303. no warnings 'once';
  304. my $route = $Trog::Routes::HTML::routes{'/secrets'};
  305. ok( $route, 'the page is a route' );
  306. ok( $route->{auth}, 'behind a login' );
  307. ok( $route->{nocache}, 'and never cached, since it is about who is asking' );
  308. ok( !$route->{totp_exempt}, 'and not somewhere an unenrolled login can get to' );
  309. my $rendered;
  310. my $index = Test::MockModule->new('Trog::Routes::HTML');
  311. $index->redefine( index => sub { $rendered = $_[0]; return [ 200, [], [''] ] } );
  312. Trog::Routes::HTML::secrets( { user => 'bob', user_acls => ['admin'], tpsgi => $tpsgi } );
  313. ok( $rendered->{has_vault}, 'the page knows there is a vault' );
  314. my ($keepass) = grep { $_->{name} eq 'keepass' } @{ $rendered->{secrets} };
  315. ok( $keepass, "bob's own secret is listed" );
  316. ok( !exists $keepass->{ciphertext}, 'without its ciphertext' );
  317. ok( $keepass->{stored}, 'with when it was stored' );
  318. # The username comes off the session, so there is no field to put somebody
  319. # else's in.
  320. Trog::Routes::HTML::secrets( { user => 'eve', user_acls => ['public'], tpsgi => $tpsgi } );
  321. is_deeply( $rendered->{secrets}, [], "and eve sees none of bob's" );
  322. # Storing, and the thing stored not surviving the request that stored it.
  323. my $query = { user => 'eve', tpsgi => $tpsgi, name => 'ssh', secret => 'a passphrase' };
  324. my $res = Trog::Routes::HTML::secrets_save($query);
  325. is( $res->[0], 303, 'saving redirects back to the page' );
  326. ok( !exists $query->{secret}, 'and the secret is gone from the query afterwards' );
  327. is( Trog::Vault::get( 'eve', 'ssh' ), 'a passphrase', 'having been stored' );
  328. $res = Trog::Routes::HTML::secrets_forget( { user => 'eve', tpsgi => $tpsgi, name => 'ssh' } );
  329. is( $res->[0], 303, 'forgetting redirects too' );
  330. is( Trog::Vault::get( 'eve', 'ssh' ), undef, 'and it is gone' );
  331. # One user cannot reach into another's, whatever they post.
  332. Trog::Routes::HTML::secrets_forget( { user => 'eve', tpsgi => $tpsgi, name => 'keepass' } );
  333. is( Trog::Vault::get( 'bob', 'keepass' ), 'correct horse battery staple', "bob's is untouched by eve asking to forget it" );
  334. };
  335. # The handle raises on every error, so these two are the writes whose failure
  336. # must still come back as an answer rather than as a die.
  337. subtest 'a write the database refuses is an answer, not a crash' => sub {
  338. my ( $ok, $why ) = Trog::Vault::set( 'nobody', 'thing', 'a secret for a user who does not exist' );
  339. is( $ok, 0, 'a secret for a user the foreign key does not know is refused' );
  340. is( $why, 'could not store it', 'with the message the page shows' );
  341. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  342. $dbh->do("DELETE FROM totp_spent WHERE username='bob'");
  343. $dbh->do("CREATE TEMP TRIGGER refuse_spend BEFORE INSERT ON totp_spent BEGIN SELECT RAISE(ABORT, 'refused for the test'); END");
  344. my ( undef, undef, undef, undef, $totp ) = Trog::Auth::totp( 'bob', 'vault.example.com' );
  345. ( $ok, $why ) = Trog::Auth::spend_totp( 'bob', $totp->expected_totp_code( time() ) );
  346. $dbh->do("DROP TRIGGER temp.refuse_spend");
  347. is( $ok, 0, 'a right code that cannot be recorded as spent is refused' );
  348. like( $why, qr/could[ ]not[ ]record[ ]the[ ]code/, 'saying why' );
  349. };
  350. done_testing();