Initial totp-fetch-seed implementation, not complete yet
[mirror/userdir-ldap-cgi.git] / fetch-totp-seed.cgi
diff --git a/fetch-totp-seed.cgi b/fetch-totp-seed.cgi
new file mode 100644 (file)
index 0000000..639b7eb
--- /dev/null
@@ -0,0 +1,49 @@
+#! /usr/bin/perl -T
+
+use lib '.';
+use strict;
+use warnings;
+use CGI;
+use Util;
+use Convert::Base32;
+use MIME::Base64;
+use GD::Barcode::QRcode;
+
+# Global settings...
+my %config = &Util::ReadConfigFile;
+
+my $query = new CGI;
+
+my $random_id = $query->param('id');
+$random_id =~ /^((\d+)-([a-f0-9]+))$/;
+$random_id = $1;
+my $timestamp = $2;
+
+if ($timestamp + 1800 < time()) {
+       &Util::HTMLError("Timestamp too old, please request a new seed");
+}
+
+my $filename = $config{totpticketdirectory} . "/" . $random_id;
+open(my $fh, "<", $filename) or &Util::HTMLError("TOTP seed file not found or permission denied: $! ; $filename");
+my $seed = encode_base32(pack('H*', <$fh>));
+close $fh;
+#unlink $filename;
+
+my $totpurl = "otpauth://totp/Debian?secret=$seed&issuer=Debian";
+my $totppng = "data:image/png;base64, " .
+       encode_base64(GD::Barcode::QRcode->new($totpurl,
+                                              { ModuleSize => 10 })->plot->png);
+
+&Util::HTMLSendHeader;
+open (F, "<", "fetch-totp-seed.html") || &Util::HTMLError($!);
+while (<F>) {
+    s/~totppng~/$totppng/g;
+    s/~totpseed~/$seed/g;
+    print;
+  }
+  close F;
+
+# fill out HTML template with QR code (inline svg/png?)
+# self-link with png link to avoid changing content-security-policy (change it back)
+
+exit 0;