2020-12-11 21:18:17 -05:00
|
|
|
module Bitwarden.Internal
|
2023-02-13 22:19:49 -05:00
|
|
|
( Item (..)
|
|
|
|
, Login (..)
|
2020-12-11 21:18:17 -05:00
|
|
|
, Session
|
|
|
|
, runDaemon
|
|
|
|
, runClient
|
|
|
|
, getItems
|
|
|
|
, callGetSession
|
2023-02-13 22:19:49 -05:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
import Data.Aeson
|
|
|
|
import qualified Data.Text.IO as TI
|
|
|
|
import Data.UnixTime
|
|
|
|
import GHC.Generics
|
|
|
|
import RIO hiding (timeout)
|
|
|
|
import qualified RIO.Text as T
|
|
|
|
import Rofi.Command
|
|
|
|
import System.Clipboard
|
|
|
|
import System.Process
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-02-13 22:19:49 -05:00
|
|
|
|
2020-12-11 21:18:17 -05:00
|
|
|
-- | Daemon
|
|
|
|
--
|
|
|
|
-- Daemon will export an interface on DBus with two methods:
|
|
|
|
-- * get current session id - if no session is active, launch Rofi to prompt
|
|
|
|
-- for a password; return session id or null if password is invalid
|
|
|
|
-- * lock session - destroy the current session id if active
|
|
|
|
--
|
|
|
|
-- The session ID will be valid only as long as TIMEOUT
|
|
|
|
newtype BWServerConf = BWServerConf
|
2023-02-13 22:19:49 -05:00
|
|
|
{ timeout :: UnixDiffTime
|
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
-- TODO add a cache so the browse list will load faster
|
|
|
|
data CurrentSession = CurrentSession
|
2023-02-13 22:19:49 -05:00
|
|
|
{ timestamp :: UnixTime
|
2023-02-13 23:31:50 -05:00
|
|
|
, hash :: T.Text
|
2023-02-13 22:19:49 -05:00
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
type Session = MVar (Maybe CurrentSession)
|
|
|
|
|
|
|
|
runDaemon :: Int -> IO ()
|
|
|
|
runDaemon t = do
|
|
|
|
ses <- newMVar Nothing
|
2023-02-13 22:19:49 -05:00
|
|
|
let c = BWServerConf {timeout = UnixDiffTime (fromIntegral t) 0}
|
2020-12-11 21:18:17 -05:00
|
|
|
startService c ses
|
|
|
|
forever $ threadDelay 1000000
|
|
|
|
|
|
|
|
lockSession :: Session -> IO ()
|
|
|
|
lockSession ses = void $ swapMVar ses Nothing
|
|
|
|
|
|
|
|
syncSession :: BWServerConf -> Session -> IO ()
|
|
|
|
syncSession conf ses = notify =<< fmap join . mapM cmd =<< getSession' conf ses
|
|
|
|
where
|
|
|
|
cmd h = readCmdSuccess "bw" ["sync", "--session", h] ""
|
2023-02-13 22:19:49 -05:00
|
|
|
notify res =
|
|
|
|
let j = isJust res
|
|
|
|
in notifyStatus j $ if j then "sync succeeded" else "sync failed"
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
getSession' :: BWServerConf -> Session -> IO (Maybe T.Text)
|
2023-02-13 22:19:49 -05:00
|
|
|
getSession' BWServerConf {timeout = t} ses = do
|
2020-12-11 21:18:17 -05:00
|
|
|
ut <- getUnixTime
|
|
|
|
modifyMVar ses $ \s -> case s of
|
2023-02-13 22:19:49 -05:00
|
|
|
Just CurrentSession {timestamp = ts, hash = h} ->
|
2020-12-11 21:18:17 -05:00
|
|
|
if diffUnixTime ut ts > t then getNewSession else return (s, Just h)
|
|
|
|
Nothing -> getNewSession
|
|
|
|
where
|
|
|
|
getNewSession = do
|
2020-12-11 22:18:20 -05:00
|
|
|
pwd <- readPassword' "Bitwarden Password"
|
2020-12-11 21:18:17 -05:00
|
|
|
newHash <- join <$> mapM readSession pwd
|
2023-02-13 22:19:49 -05:00
|
|
|
(,newHash) <$> mapM newSession newHash
|
2020-12-11 21:18:17 -05:00
|
|
|
newSession h = do
|
|
|
|
ut <- getUnixTime
|
2023-02-13 22:19:49 -05:00
|
|
|
return CurrentSession {timestamp = ut, hash = h}
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
getSession :: BWServerConf -> Session -> IO T.Text
|
2020-12-11 21:18:17 -05:00
|
|
|
getSession conf ses = fromMaybe "" <$> getSession' conf ses
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
readSession :: T.Text -> IO (Maybe T.Text)
|
2020-12-11 21:18:17 -05:00
|
|
|
readSession pwd = readCmdSuccess "bw" ["unlock", pwd, "--raw"] ""
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
notifyStatus :: Bool -> T.Text -> IO ()
|
2020-12-11 21:18:17 -05:00
|
|
|
notifyStatus succeeded msg =
|
2023-02-13 23:31:50 -05:00
|
|
|
void $ spawnProcess "notify-send" ["-i", i, T.unpack msg]
|
2020-12-11 21:18:17 -05:00
|
|
|
where
|
2023-02-13 22:19:49 -05:00
|
|
|
i =
|
|
|
|
if succeeded
|
|
|
|
then "dialog-information-symbolic"
|
|
|
|
else "dialog-error-symbolic"
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-02-13 22:19:49 -05:00
|
|
|
|
2020-12-11 21:18:17 -05:00
|
|
|
-- | Client
|
|
|
|
--
|
|
|
|
-- The client will get the current session from the daemon (if it can) and then
|
|
|
|
-- go through a decision-tree like selection process to retrieve information as
|
|
|
|
-- needed. This will be in the form of the following menus:
|
|
|
|
--
|
|
|
|
-- Main menus
|
|
|
|
-- - Lock Session -> lock the session
|
|
|
|
-- - Browse logins -> show new menu of all logins
|
|
|
|
-- - select an entry -> show new menu with entry contents
|
|
|
|
-- - All -> copy all to clipboard
|
|
|
|
-- - username (if applicable) -> copy to clipboard
|
|
|
|
-- - password (if applicable) -> copy to clipboard
|
|
|
|
-- - anything else (notes and such) -> copy to clipboard
|
2023-02-13 23:31:50 -05:00
|
|
|
newtype BWClientConf = BWClientConf [T.Text]
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-14 22:28:26 -05:00
|
|
|
instance HasRofiConf BWClientConf where
|
2020-12-11 21:18:17 -05:00
|
|
|
defArgs (BWClientConf a) = a
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
runClient :: [T.Text] -> IO ()
|
2020-12-11 21:18:17 -05:00
|
|
|
runClient a = do
|
|
|
|
let c = BWClientConf a
|
2023-02-14 22:28:26 -05:00
|
|
|
runRofi c $
|
|
|
|
emptyMenu
|
|
|
|
{ groups = [untitledGroup $ toRofiActions ras]
|
|
|
|
, prompt = Just "Action"
|
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
where
|
2023-02-13 22:19:49 -05:00
|
|
|
ras =
|
|
|
|
[ ("Browse Logins", browseLogins)
|
|
|
|
, ("Sync Session", io callSyncSession)
|
|
|
|
, ("Lock Session", io callLockSession)
|
|
|
|
]
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-14 22:28:26 -05:00
|
|
|
browseLogins :: HasRofiConf c => RIO c ()
|
2020-12-11 22:18:20 -05:00
|
|
|
browseLogins = io getItems >>= selectItem
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2020-12-11 22:18:20 -05:00
|
|
|
getItems :: IO [Item]
|
|
|
|
getItems = maybe (return []) getItems' =<< callGetSession
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
getItems' :: T.Text -> IO [Item]
|
2020-12-11 22:18:20 -05:00
|
|
|
getItems' session = do
|
2023-02-13 23:31:50 -05:00
|
|
|
items <- io $ readProcess "bw" ["list", "items", "--session", T.unpack session] ""
|
2020-12-11 21:18:17 -05:00
|
|
|
return $ filter notEmpty $ fromMaybe [] $ decode $ fromString items
|
|
|
|
where
|
2023-02-13 22:19:49 -05:00
|
|
|
notEmpty Item {login = Login {username = Nothing, password = Nothing}} =
|
|
|
|
False
|
2020-12-11 21:18:17 -05:00
|
|
|
notEmpty _ = True
|
|
|
|
|
|
|
|
data Item = Item
|
2023-02-13 23:31:50 -05:00
|
|
|
{ name :: T.Text
|
2023-02-13 22:19:49 -05:00
|
|
|
, login :: Login
|
|
|
|
}
|
|
|
|
deriving (Show)
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
instance FromJSON Item where
|
2023-02-13 22:19:49 -05:00
|
|
|
parseJSON (Object o) =
|
|
|
|
Item
|
|
|
|
<$> o .: "name"
|
|
|
|
<*> o .:? "login" .!= Login {username = Nothing, password = Nothing}
|
2020-12-11 21:18:17 -05:00
|
|
|
parseJSON _ = mzero
|
|
|
|
|
|
|
|
data Login = Login
|
2023-02-13 23:31:50 -05:00
|
|
|
{ username :: Maybe T.Text
|
|
|
|
, password :: Maybe T.Text
|
2023-02-13 22:19:49 -05:00
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
instance FromJSON Login
|
|
|
|
|
|
|
|
-- TODO make menu buttons here to go back and to copy without leaving
|
|
|
|
-- the current menu
|
2023-02-14 22:28:26 -05:00
|
|
|
selectItem :: HasRofiConf c => [Item] -> RIO c ()
|
2023-02-13 22:19:49 -05:00
|
|
|
selectItem items =
|
|
|
|
selectAction $
|
|
|
|
emptyMenu
|
|
|
|
{ groups = [untitledGroup $ itemsToRofiActions items]
|
|
|
|
, prompt = Just "Login"
|
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-14 22:28:26 -05:00
|
|
|
itemsToRofiActions :: HasRofiConf c => [Item] -> RofiActions c
|
2020-12-11 21:18:17 -05:00
|
|
|
itemsToRofiActions = toRofiActions . fmap (\i -> (name i, selectCopy $ login i))
|
|
|
|
|
2023-02-14 22:28:26 -05:00
|
|
|
selectCopy :: HasRofiConf c => Login -> RIO c ()
|
2023-02-13 22:19:49 -05:00
|
|
|
selectCopy l =
|
|
|
|
selectAction $
|
|
|
|
emptyMenu
|
|
|
|
{ groups = [untitledGroup $ loginToRofiActions l copy]
|
|
|
|
, prompt = Just "Copy"
|
|
|
|
, hotkeys = [copyHotkey, backHotkey]
|
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
where
|
2023-02-13 23:31:50 -05:00
|
|
|
copy = io . setClipboardString . T.unpack
|
2020-12-11 21:18:17 -05:00
|
|
|
copyRepeat s = copy s >> selectCopy l
|
2023-02-13 22:19:49 -05:00
|
|
|
copyHotkey =
|
|
|
|
Hotkey
|
|
|
|
{ keyCombo = "Alt+c"
|
|
|
|
, keyDescription = "Copy One"
|
|
|
|
, keyActions = loginToRofiActions l copyRepeat
|
|
|
|
}
|
|
|
|
backHotkey =
|
|
|
|
Hotkey
|
|
|
|
{ keyCombo = "Alt+q"
|
|
|
|
, keyDescription = "Back"
|
|
|
|
, -- TODO this is overly complicated, all entries do the same thing
|
|
|
|
-- TODO this is slow, we can cache the logins somehow...
|
|
|
|
keyActions = loginToRofiActions l (const browseLogins)
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:28:26 -05:00
|
|
|
loginToRofiActions :: Login -> (T.Text -> RIO c ()) -> RofiActions c
|
2023-02-13 22:19:49 -05:00
|
|
|
loginToRofiActions Login {username = u, password = p} a =
|
2020-12-11 21:18:17 -05:00
|
|
|
toRofiActions $ catMaybes [user, pwd]
|
|
|
|
where
|
|
|
|
copyIfJust f = fmap $ liftM2 (,) f a
|
2023-02-13 23:31:50 -05:00
|
|
|
fmtUsername s = T.concat ["Username (", s, ")"]
|
|
|
|
fmtPassword s = T.concat ["Password (", T.take 32 (T.replicate (T.length s) "*"), ")"]
|
2020-12-11 21:18:17 -05:00
|
|
|
user = copyIfJust fmtUsername u
|
|
|
|
pwd = copyIfJust fmtPassword p
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
getItemPassword' :: BWServerConf -> Session -> T.Text -> IO (Maybe T.Text)
|
2021-02-14 13:17:17 -05:00
|
|
|
getItemPassword' conf session item = mapM getPwd =<< getSession' conf session
|
|
|
|
where
|
2023-02-13 23:31:50 -05:00
|
|
|
getPwd s =
|
|
|
|
T.pack
|
|
|
|
<$> readProcess
|
|
|
|
"bw"
|
|
|
|
["get", "password", T.unpack item, "--session", T.unpack s]
|
|
|
|
""
|
|
|
|
|
|
|
|
getItemPassword :: BWServerConf -> Session -> T.Text -> IO T.Text
|
2023-02-13 22:19:49 -05:00
|
|
|
getItemPassword conf session item =
|
|
|
|
fromMaybe ""
|
|
|
|
<$> getItemPassword' conf session item
|
2021-02-14 13:17:17 -05:00
|
|
|
|
2020-12-11 21:18:17 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2023-02-13 22:19:49 -05:00
|
|
|
-- | DBus
|
2020-12-11 21:18:17 -05:00
|
|
|
startService :: BWServerConf -> Session -> IO ()
|
|
|
|
startService c ses = do
|
|
|
|
client <- connectSession
|
|
|
|
let flags = [nameAllowReplacement, nameReplaceExisting]
|
|
|
|
_ <- requestName client busname flags
|
2023-02-13 22:19:49 -05:00
|
|
|
TI.putStrLn "Started rofi bitwarden dbus client"
|
|
|
|
export
|
|
|
|
client
|
|
|
|
path
|
|
|
|
defaultInterface
|
|
|
|
{ interfaceName = interface
|
|
|
|
, interfaceMethods =
|
|
|
|
[ autoMethod memGetSession $ getSession c ses
|
|
|
|
, autoMethod memLockSession $ lockSession ses
|
|
|
|
, autoMethod memSyncSession $ syncSession c ses
|
|
|
|
, autoMethod memGetPassword $ getItemPassword c ses
|
|
|
|
]
|
|
|
|
}
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
busname :: BusName
|
|
|
|
busname = "org.rofi.bitwarden"
|
|
|
|
|
|
|
|
path :: ObjectPath
|
|
|
|
path = "/bitwarden"
|
|
|
|
|
|
|
|
interface :: InterfaceName
|
|
|
|
interface = "org.rofi.bitwarden.session"
|
|
|
|
|
|
|
|
memGetSession :: MemberName
|
|
|
|
memGetSession = "GetSession"
|
|
|
|
|
|
|
|
memLockSession :: MemberName
|
|
|
|
memLockSession = "LockSession"
|
|
|
|
|
|
|
|
memSyncSession :: MemberName
|
|
|
|
memSyncSession = "SyncSession"
|
|
|
|
|
2021-02-14 13:17:17 -05:00
|
|
|
memGetPassword :: MemberName
|
|
|
|
memGetPassword = "GetPassword"
|
|
|
|
|
2020-12-11 21:18:17 -05:00
|
|
|
callMember :: MemberName -> IO [Variant]
|
|
|
|
callMember m = do
|
|
|
|
reply <- callMethod $ methodCall path interface m
|
|
|
|
case reply of
|
2023-02-13 22:19:49 -05:00
|
|
|
Left err -> TI.putStrLn (T.pack (methodErrorMessage err)) >> return []
|
2020-12-11 21:18:17 -05:00
|
|
|
Right body -> return body
|
|
|
|
|
|
|
|
callLockSession :: IO ()
|
|
|
|
callLockSession = void $ callMember memLockSession
|
|
|
|
|
|
|
|
callSyncSession :: IO ()
|
|
|
|
callSyncSession = void $ callMember memSyncSession
|
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
callGetSession :: IO (Maybe T.Text)
|
2021-02-14 13:17:17 -05:00
|
|
|
callGetSession = getBodyString <$> callMember memGetSession
|
|
|
|
|
|
|
|
-- TODO maybe will need to add a caller for getItemPassword
|
2020-12-11 21:18:17 -05:00
|
|
|
|
2023-02-13 23:31:50 -05:00
|
|
|
getBodyString :: [Variant] -> Maybe T.Text
|
|
|
|
getBodyString [b] = case fromVariant b :: Maybe T.Text of
|
2020-12-11 21:18:17 -05:00
|
|
|
Just "" -> Nothing
|
2023-02-13 22:19:49 -05:00
|
|
|
s -> s
|
2021-02-14 13:17:17 -05:00
|
|
|
getBodyString _ = Nothing
|
2020-12-11 21:18:17 -05:00
|
|
|
|
|
|
|
callMethod :: MethodCall -> IO (Either MethodError [Variant])
|
|
|
|
callMethod mc = do
|
|
|
|
client <- connectSession
|
2023-02-13 22:19:49 -05:00
|
|
|
reply <- call client mc {methodCallDestination = Just busname}
|
2020-12-11 21:18:17 -05:00
|
|
|
disconnect client
|
|
|
|
return $ methodReturnBody <$> reply
|