-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.lua
32 lines (26 loc) · 1.2 KB
/
token.lua
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
-- token.lua - generate google authenticator token value keystrokes
-- written by Teun Vink <github@teun.tv>
-- https://github.com/teunvink/hammerspoon
--
-- Retrieve a google authenticator token seed from keychain and use this to calculate the current value
-- Simulate keystrokes for this token value
local gauth = require "gauth"
-- code is based on:
-- https://github.com/imzyxwvu/lua-gauth/blob/master/gauth.lua (with small modifications)
-- https://github.com/kikito/sha.lua
-- read a password from a keychain
function password_from_keychain(name)
-- 'name' should be saved in the login keychain
local cmd="/usr/bin/security 2>&1 >/dev/null find-generic-password -ga " .. name .. " | sed -En '/^password: / s,^password: \"(.*)\"$,\\1,p'"
local handle = io.popen(cmd)
local result = handle:read("*a")
handle:close()
return (result:gsub("^%s*(.-)%s*$", "%1"))
end
-- read a token seed from keychain, generate a code and make keystrokes for it
function token_keystroke(token_name)
local token = password_from_keychain(token_name)
local hash = gauth.GenCode(token, math.floor(os.time() / 30))
-- generate keystrokes for the result
hs.eventtap.keyStrokes(("%06d"):format(hash))
end