322908a514dd5a95d4d7c9128bf07229d59754de
[mirror/userdir-ldap-cgi.git] / fetch-totp-seed.cgi
1 #! /usr/bin/perl -T
2
3 use lib '.';
4 use strict;
5 use warnings;
6 use CGI;
7 use Util;
8 use Convert::Base32;
9 use MIME::Base64;
10 use GD::Barcode::QRcode;
11
12 # Global settings...
13 my %config = &Util::ReadConfigFile;
14
15 my $query = new CGI;
16
17 my $random_id = $query->param('id');
18 $random_id =~ /^((\d+)-([a-f0-9]+))$/;
19 $random_id = $1;
20 my $timestamp = $2;
21
22 if ($timestamp + 1800 < time()) {
23         &Util::HTMLError("Timestamp too old, please request a new seed");
24 }
25
26 my $filename = $config{totpticketdirectory} . "/" . $random_id;
27 open(my $fh, "<", $filename) or &Util::HTMLError("TOTP seed file not found or permission denied: $! ; $filename");
28 my $hex_seed = <$fh>;
29 my $accountname = <$fh>;
30 my $seed = encode_base32(pack('H*', hex_seed));
31 close $fh;
32 #unlink $filename;
33
34 my $totpurl = "otpauth://totp/Debian?secret=$seed&issuer=Debian";
35 my $totppng = "data:image/png;base64, " .
36         encode_base64(GD::Barcode::QRcode->new($totpurl,
37                                                { ModuleSize => 10 })->plot->png);
38
39 &Util::HTMLSendHeader;
40 open (F, "<", "fetch-totp-seed.html") || &Util::HTMLError($!);
41 while (<F>) {
42     s/~totppng~/$totppng/g;
43     s/~totpseed~/$seed/g;
44     print;
45   }
46   close F;
47
48 # fill out HTML template with QR code (inline svg/png?)
49 # self-link with png link to avoid changing content-security-policy (change it back)
50
51 exit 0;