]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/AccessControl.pm
remove debugging code
[pve-access-control.git] / PVE / AccessControl.pm
index 9e764d264fa1d1eb0a41f5865b5b7d7b0d072fd1..db85d08c4a6a4e82e346fd9021d079f5c1366e4a 100644 (file)
@@ -376,11 +376,12 @@ sub verify_one_time_pw {
     if ($type eq 'yubico') {
        my $keys = $usercfg->{users}->{$username}->{keys};
        yubico_verify_otp($otp, $keys, $tfa_cfg->{url}, $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
+    } elsif ($type eq 'oath') {
+       my $keys = $usercfg->{users}->{$username}->{keys};
+       oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits});
     } else {
        die "unknown tfa type '$type'\n";
     }
-
-    die "implement me";
 }
 
 # password should be utf8 encoded
@@ -755,7 +756,8 @@ sub parse_user_config {
            $cfg->{users}->{$user}->{email} = $email;
            $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment;
            $cfg->{users}->{$user}->{expire} = $expire;
-           $cfg->{users}->{$user}->{keys} = $keys if $keys; # allowed yubico key ids
+           # keys: allowed yubico key ids or oath secrets (base32 encoded)
+           $cfg->{users}->{$user}->{keys} = $keys if $keys; 
 
            #$cfg->{users}->{$user}->{groups}->{$group} = 1;
            #$cfg->{groups}->{$group}->{$user} = 1;
@@ -1226,4 +1228,34 @@ sub yubico_verify_otp {
     return $result;
 }
 
+sub oath_verify_otp {
+    my ($otp, $keys, $step, $digits) = @_;
+
+    die "oath: missing password\n" if !defined($otp);
+    die "oath: no associated oath keys\n" if $keys =~ m/^\s+$/; 
+
+    $step = 30 if !$step;
+    $digits = 6 if !$digits;
+
+    my $found;
+
+    my $parser = sub {
+       my $line = shift;
+
+       if ($line =~ m/^\d{6}$/) {
+           $found = 1 if $otp eq $line;
+       }
+    };
+
+    foreach my $k (PVE::Tools::split_list($keys)) {
+       # Note: we generate 3 values to allow small time drift
+       my $now = localtime(time() - $step);
+       my $cmd = ['oathtool', '--totp', '--digits', $digits, '-N', $now, '-s', $step, '-w', '2', '-b', $k];
+       eval { run_command($cmd, outfunc => $parser, errfunc => sub {}); };
+       last if $found;
+    }
+
+    die "oath auth failed\n" if !$found;
+}
+
 1;