perlsky is a Perl 5 implementation of an AT Protocol Personal Data Server.
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

Extract account action token helper

alice 25608293 5773ea4b

+45 -44
+45 -44
lib/ATProto/PDS/API/Server.pm
··· 345 345 my $body = $c->req->json || {}; 346 346 my $account = $c->store->get_account_by_email($body->{email} // q()); 347 347 if ($account) { 348 - my $token = $c->store->create_action_token( 349 - did => $account->{did}, 350 - email => $account->{email}, 351 - purpose => 'password_reset', 352 - expires_at => time + 3600, 353 - ); 354 - $c->store->log_outbound_email( 355 - recipient_did => $account->{did}, 356 - recipient_email => $account->{email}, 357 - subject => 'perlsky password reset', 358 - content => "Use token $token->{token} to reset your password.", 348 + _issue_account_action_token( 349 + $c, 350 + $account, 351 + purpose => 'password_reset', 352 + subject => 'perlsky password reset', 353 + content => sub ($token) { "Use token $token->{token} to reset your password." }, 359 354 ); 360 355 } 361 356 return {}; ··· 388 383 $registry->register('com.atproto.server.requestEmailConfirmation', sub ($c, $endpoint) { 389 384 my (undef, $account) = require_auth($c, audience => 'access'); 390 385 return {} unless $account->{email}; 391 - my $token = $c->store->create_action_token( 392 - did => $account->{did}, 393 - email => $account->{email}, 394 - purpose => 'email_confirm', 395 - expires_at => time + 3600, 396 - ); 397 - $c->store->log_outbound_email( 398 - recipient_did => $account->{did}, 399 - recipient_email => $account->{email}, 400 - subject => 'perlsky email confirmation', 401 - content => "Use token $token->{token} to confirm your email address.", 386 + _issue_account_action_token( 387 + $c, 388 + $account, 389 + purpose => 'email_confirm', 390 + subject => 'perlsky email confirmation', 391 + content => sub ($token) { "Use token $token->{token} to confirm your email address." }, 402 392 ); 403 393 return {}; 404 394 }); ··· 427 417 my (undef, $account) = require_auth($c, audience => 'access'); 428 418 my $token_required = defined $account->{email_confirmed_at} ? 1 : 0; 429 419 if ($token_required) { 430 - my $token = $c->store->create_action_token( 431 - did => $account->{did}, 432 - email => $account->{email}, 433 - purpose => 'email_update', 434 - expires_at => time + 3600, 435 - ); 436 - $c->store->log_outbound_email( 437 - recipient_did => $account->{did}, 438 - recipient_email => $account->{email}, 439 - subject => 'perlsky email change authorization', 440 - content => "Use token $token->{token} to update your email address.", 420 + _issue_account_action_token( 421 + $c, 422 + $account, 423 + purpose => 'email_update', 424 + subject => 'perlsky email change authorization', 425 + content => sub ($token) { "Use token $token->{token} to update your email address." }, 441 426 ); 442 427 } 443 428 return { ··· 469 454 470 455 $registry->register('com.atproto.server.requestAccountDelete', sub ($c, $endpoint) { 471 456 my (undef, $account) = require_auth($c, audience => 'access', required_scope => 'full'); 472 - my $token = $c->store->create_action_token( 473 - did => $account->{did}, 474 - email => $account->{email}, 475 - purpose => 'account_delete', 476 - expires_at => time + 3600, 457 + _issue_account_action_token( 458 + $c, 459 + $account, 460 + purpose => 'account_delete', 461 + subject => 'perlsky account deletion', 462 + content => sub ($token) { "Use token $token->{token} to delete your account." }, 477 463 ); 478 - $c->store->log_outbound_email( 479 - recipient_did => $account->{did}, 480 - recipient_email => $account->{email}, 481 - subject => 'perlsky account deletion', 482 - content => "Use token $token->{token} to delete your account.", 483 - ) if $account->{email}; 484 464 return {}; 485 465 }); 486 466 ··· 789 769 return undef unless defined $email && length $email; 790 770 return undef unless $c->config_value('testing_auto_confirm_email', 1); 791 771 return time; 772 + } 773 + 774 + sub _issue_account_action_token ($c, $account, %args) { 775 + return undef unless $account; 776 + my $token = $c->store->create_action_token( 777 + did => $account->{did}, 778 + email => $account->{email}, 779 + purpose => $args{purpose}, 780 + expires_at => $args{expires_at} // (time + 3600), 781 + ); 782 + if (defined($account->{email}) && length($account->{email})) { 783 + $c->store->log_outbound_email( 784 + recipient_did => $account->{did}, 785 + recipient_email => $account->{email}, 786 + subject => $args{subject}, 787 + content => ref($args{content}) eq 'CODE' 788 + ? $args{content}->($token) 789 + : $args{content}, 790 + ); 791 + } 792 + return $token; 792 793 } 793 794 794 795 sub _require_action_token ($c, %args) {