totp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use FindBin::libs;
  6. use Trog::Auth;
  7. use Trog::Config;
  8. =head1 SYNOPSIS
  9. Print a user's TOTP enrollment details, so that they can set up 2FA without
  10. going through the web UI -- or get back in when they've lost their
  11. authenticator and you need to read them the current code.
  12. =head2 USAGE
  13. bin/totp username
  14. Prints the otpauth:// URI (what a QR code would encode), the base32 secret to
  15. type into an authenticator by hand, and the code which is valid right now.
  16. Note that this hands out everything needed to generate that user's codes
  17. forever, so treat the output like a password.
  18. =head2 CAVEATS
  19. Requires general.hostname to be set in the tCMS configuration, as the domain is
  20. part of what an authenticator shows the user. Set it with bin/tcms-hostname.
  21. =cut
  22. my $user = shift @ARGV;
  23. my $domain = Trog::Config->get()->param('general.hostname');
  24. die "Must provide a user" unless $user;
  25. die "Must Set domain in configuration file" unless $domain;
  26. my ( $uri, undef, undef, undef, $totp ) = Trog::Auth::totp( $user, $domain );
  27. print "TOTP URI: $uri\n" . "Secret: " . $totp->base32secret() . "\n" . "Current Auth code: " . $totp->expected_totp_code(time) . "\n";