1pass.lua
changeset 56 a573346e6f7b
parent 49 2c57a0ad7c8f
equal deleted inserted replaced
55:0aaf56a96d21 56:a573346e6f7b
   138 local function setMenuItemChecked(menuitem, ischecked)
   138 local function setMenuItemChecked(menuitem, ischecked)
   139     menuitem["checked"] = ischecked
   139     menuitem["checked"] = ischecked
   140 end
   140 end
   141 
   141 
   142 
   142 
   143 local function build_secret_menuitem(menu, type, str, hidden)
   143 local function build_secret_menuitem(menu, type, str, hidden, transform)
   144     if str == nil then
   144     if str == nil then
   145         return nil
   145         return nil
   146     end
   146     end
   147 
   147 
   148     local valuestr = str
   148     local valuestr = str
   150         valuestr = "*****"
   150         valuestr = "*****"
   151     end
   151     end
   152     local text = type .. " " .. valuestr
   152     local text = type .. " " .. valuestr
   153 
   153 
   154     local callback = function()
   154     local callback = function()
       
   155         if transform ~= nil then
       
   156             str = transform(str)
       
   157         end
   155         copyToClipboard(str)
   158         copyToClipboard(str)
   156         --print("Copied data [" .. str .. "] to clipboard.")
   159         --print("Copied data [" .. str .. "] to clipboard.")
   157         guiDestroyMenu(keyhookGuiMenus[1])
   160         guiDestroyMenu(keyhookGuiMenus[1])
   158     end
   161     end
   159     return appendMenuItem(menu, text, callback)
   162     return appendMenuItem(menu, text, callback)
       
   163 end
       
   164 
       
   165 local function transform_otp(str)
       
   166     local algorithm, name, argstr = string.match(str, "^otpauth://(.-)/(.-)?(.+)$")
       
   167     if algorithm == nil then algorithm = "" end
       
   168     if algorithm ~= "totp" then
       
   169         print("FIXME: don't know how to handle One Time Passwords using the '" .. algorithm .. "' algorithm!")
       
   170         return "000000"
       
   171     end
       
   172 
       
   173     local args = {}
       
   174     while argstr ~= nil and argstr ~= "" do
       
   175         local arg
       
   176         local idx = string.find(argstr, "&")
       
   177         if idx == nil then
       
   178             arg = argstr
       
   179             argstr = nil
       
   180         else
       
   181             arg = string.sub(argstr, 0, idx);
       
   182             argstr = string.sub(argstr, idx + 1);
       
   183         end
       
   184 
       
   185         local key, val = string.match(arg, "^(.-)=(.*)$")
       
   186         if (key ~= nil) and (val ~= nil) then
       
   187             args[key] = val
       
   188         end
       
   189     end
       
   190 
       
   191     --dumptable("otpauth://" .. algorithm .. "/" .. name, args);
       
   192 
       
   193     if args["secret"] == nil then
       
   194         print("FIXME: this One Time Password doesn't seem to have a secret key!")
       
   195         return "000000"
       
   196     end
       
   197 
       
   198     local retval = decryptTopt(args["secret"]);
       
   199     if retval == nil then
       
   200         print("FIXME: failed to generate One Time Password; is the secret key bogus?")
       
   201         retval = "000000"
       
   202     end
       
   203     return retval
   160 end
   204 end
   161 
   205 
   162 
   206 
   163 local secret_menuitem_builders = {}
   207 local secret_menuitem_builders = {}
   164 
   208 
   165 local function build_secret_menuitem_webform(menu, info, secure)
   209 local function build_secret_menuitem_webform(menu, info, secure)
   166     local addthis = false
   210     local addthis = false
   167     local username = nil
   211     local username = nil
   168     local password = nil
   212     local password = nil
       
   213     local otp = nil
   169     local designated_password = nil
   214     local designated_password = nil
   170     local designated_username = nil
   215     local designated_username = nil
   171     local email = nil
   216     local email = nil
   172 
   217 
   173     if secure.fields == nil then
   218     if secure.fields == nil then
   197         if not ignored then
   242         if not ignored then
   198             addthis = true
   243             addthis = true
   199         end
   244         end
   200     end
   245     end
   201 
   246 
       
   247     -- this is probably all wrong.
       
   248     if secure.sections ~= nil then
       
   249         for i,v in ipairs(secure.sections) do
       
   250             if v.fields ~= nil then
       
   251                 for i2,v2 in ipairs(v.fields) do
       
   252                     if (type(v2.v) == "string") and (string.sub(v2.v, 0, 10) == "otpauth://") then
       
   253                         otp = v2.v
       
   254                         addthis = true
       
   255                     end
       
   256                 end
       
   257             end
       
   258         end
       
   259     end
       
   260 
   202     if addthis then
   261     if addthis then
   203         -- designated fields always win out.
   262         -- designated fields always win out.
   204         if (designated_username ~= nil) then
   263         if (designated_username ~= nil) then
   205             username = designated_username
   264             username = designated_username
   206         end
   265         end
   214         end
   273         end
   215 
   274 
   216         build_secret_menuitem(menu, "username", username)
   275         build_secret_menuitem(menu, "username", username)
   217         build_secret_menuitem(menu, "email", email)
   276         build_secret_menuitem(menu, "email", email)
   218         build_secret_menuitem(menu, "password", password, true)
   277         build_secret_menuitem(menu, "password", password, true)
       
   278         build_secret_menuitem(menu, "otp", otp, true, transform_otp)
   219     end
   279     end
   220 end
   280 end
   221 secret_menuitem_builders["webforms.WebForm"] = build_secret_menuitem_webform
   281 secret_menuitem_builders["webforms.WebForm"] = build_secret_menuitem_webform
   222 
   282 
   223 
   283