-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfa
executable file
·78 lines (63 loc) · 1.88 KB
/
mfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/local/opt/[email protected]/bin/php
<?php
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require(__DIR__ . '/vendor/autoload.php');
}
if (file_exists(__DIR__ . '/../../autoload.php')) {
require(__DIR__ . '/../../autoload.php');
}
$ga = new PHPGangsta_GoogleAuthenticator();
$config = getenv('MFA_CONFIG_FILE');
if (!$config) {
$homedir = trim(`cd ~ && pwd`);
$config = $homedir . DIRECTORY_SEPARATOR . '.mfa';
}
$secrets = [];
if (file_exists($config)) {
$mfaSecrets = file_get_contents($config);
} else {
$mfaSecrets = 'demo = SECRET';
}
foreach (array_filter(explode(PHP_EOL, $mfaSecrets)) as $secret) {
$parts = explode('=', $secret);
$name = trim($parts[0]);
$secrets[$name] = trim($parts[1]);
}
while (1) {
fwrite(STDOUT, "Choose MFA\n");
$i = 0;
foreach ($secrets as $key => $secret) {
echo "[$i]: $key " . PHP_EOL;
$i++;
}
$index = intval(fgets(STDIN));
if (array_key_exists($index, array_keys($secrets))) {
$secret = $secrets[array_keys($secrets)[$index]];
break;
}
}
while (1) {
$oneCode = $ga->getCode($secret);
echo "\r" . array_keys($secrets)[$index] . " checking code: $oneCode";
//$shell="echo -n $oneCode | /usr/bin/pbcoby";
//echo $shell;
//`$shell`;
copy2clipboard($oneCode);
usleep(100);
}
function copy2clipboard($string)
{
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "a.txt", "a") // stderr is a file to write to
);
$process = proc_open('pbcopy', $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $string);
fclose($pipes[0]);
fclose($pipes[1]);
$return_value = proc_close($process);
return $return_value;
}
}