ENH store bitwarden name in conf directory

This commit is contained in:
Nathan Dwarshuis 2021-06-26 17:14:23 -04:00
parent a606056b26
commit 3a3c5eb004
1 changed files with 39 additions and 16 deletions

View File

@ -1,3 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- | rofi-pinentry - a simply pinentry proxy for bitwarden -- | rofi-pinentry - a simply pinentry proxy for bitwarden
-- --
@ -6,31 +8,52 @@
module Main where module Main where
import Data.List
import Bitwarden.Internal import Bitwarden.Internal
import Data.List
import Data.Yaml
import System.Directory
import System.Environment
import System.Exit import System.Exit
import System.FilePath.Posix
import System.IO import System.IO
import System.Posix.Process import System.Posix.Process
main :: IO () main :: IO ()
main = do main = do
hSetBuffering stdout LineBuffering hSetBuffering stdout LineBuffering
-- TODO don't hardcode this
let n = "gnupg"
putStrLn "OK Pleased to meet you" putStrLn "OK Pleased to meet you"
pinentryLoop n pinentryLoop =<< readPinConf
pinentryLoop :: String -> IO () newtype PinConf = PinConf { pcBwName :: String } deriving (Eq, Show)
pinentryLoop n = do
c <- getLine
processLine n $ words c
pinentryLoop n
processLine :: String -> [String] -> IO () instance FromJSON PinConf where
parseJSON (Object o) = PinConf <$> o .:? "bitwarden-name" .!= "gnupg"
parseJSON _ = fail "pinentry yaml parse error"
readPinConf :: IO PinConf
readPinConf = do
c <- decodeFileEither =<< pinConfDir
case c of
Left e -> print e >> exitWith (ExitFailure 1)
Right r -> return r
pinConfDir :: IO FilePath
pinConfDir = maybe defHome (return . (</> confname)) =<< lookupEnv "GNUPGHOME"
where
defHome = (</> ".gnupg" </> confname) <$> getHomeDirectory
confname = "pinentry-rofi.yml"
pinentryLoop :: PinConf -> IO ()
pinentryLoop p = do
processLine p . words =<< getLine
pinentryLoop p
processLine :: PinConf -> [String] -> IO ()
processLine _ [] = noop processLine _ [] = noop
processLine _ ["BYE"] = exitSuccess processLine _ ["BYE"] = exitSuccess
processLine n ["GETPIN"] = getPin n processLine p ["GETPIN"] = getPin p
processLine _ ["GETINFO", o] = processGetInfo o processLine _ ["GETINFO", o] = processGetInfo o
@ -54,11 +77,11 @@ processLine _ ss = unknownCommand $ unwords ss
unknownCommand :: String -> IO () unknownCommand :: String -> IO ()
unknownCommand c = putStrLn $ "ERR 275 Unknown command " ++ c unknownCommand c = putStrLn $ "ERR 275 Unknown command " ++ c
getPin :: String -> IO () getPin :: PinConf -> IO ()
getPin n = do getPin p = do
its <- getItems its <- getItems
let p = (password . login) =<< find (\i -> n == name i) its let w = (password . login) =<< find (\i -> pcBwName p == name i) its
maybe err send p maybe err send w
where where
err = putStrLn "ERR 83886179 Operation canceled <rofi>" err = putStrLn "ERR 83886179 Operation canceled <rofi>"