Include accountname in totp url
[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 chomp $hex_seed;
30 my $accountname = <$fh>;
31 chomp $accountname;
32 my $seed = encode_base32(pack('H*', $hex_seed));
33 close $fh;
34 #unlink $filename;
35
36 my $totpurl = "otpauth://totp/Debian:$accountname?secret=$seed&issuer=Debian";
37 my $totppng = "data:image/png;base64, " .
38         encode_base64(GD::Barcode::QRcode->new($totpurl,
39                                                { ModuleSize => 10 })->plot->png);
40
41 &Util::HTMLSendHeader;
42 open (F, "<", "fetch-totp-seed.html") || &Util::HTMLError($!);
43 while (<F>) {
44     s/~totppng~/$totppng/g;
45     s/~totpseed~/$seed/g;
46     print;
47   }
48   close F;
49
50 # fill out HTML template with QR code (inline svg/png?)
51 # self-link with png link to avoid changing content-security-policy (change it back)
52
53 exit 0;