-- | Open a connection, authenticate, execute some action and close the connection.
--
-- It is the safe way of using SimpleSSH. This function is to be used to authenticate with a pair username / password, otherwise see 'withSessionKey'.
withSessionPassword :: String                   -- ^ Hostname
                    -> Integer                  -- ^ Port
                    -> String                   -- ^ Path to known_hosts
                    -> String                   -- ^ Username
                    -> String                   -- ^ Password
                    -> (Session -> SimpleSSH a) -- ^ Monadic action on the session
                    -> SimpleSSH a
withSessionPassword hostname port knownhostsPath username password action = do
  session              <- openSession hostname port knownhostsPath
  authenticatedSession <- authenticateWithPassword session username password
  res                  <- action authenticatedSession
  closeSession authenticatedSession
  return res

-- | Open a connection, authenticate, execute some action and close the connection.
--
-- It is the safe way of using SimpleSSH. This function is to be used to authenticate with a key, otherwise see 'withSessionPassword'.
withSessionPassword :: String                   -- ^ Hostname
                    -> Integer                  -- ^ Port
                    -> String                   -- ^ Path to known_hosts
                    -> String                   -- ^ Username
                    -> String                   -- ^ Password
                    -> (Session -> SimpleSSH a) -- ^ Monadic action on the session
                    -> SimpleSSH a
withSessionPassword hostname port knownhostsPath username password action = do
  session              <- openSession hostname port knownhostsPath
  authenticatedSession <- authenticateWithPassword session username password
  res                  <- action authenticatedSession
  closeSession authenticatedSession
  return res

-- | Open a connection, authenticate, execute some action and close the connection.
--
-- It is the safe way of using SimpleSSH. This function is to be used to authenticate with a key, otherwise see 'withSessionPassword'.
withSessionPassword hostname port knownhostsPath username password action = do
  session              <- openSession hostname port knownhostsPath
  authenticatedSession <- authenticateWithPassword session username password
  res                  <- action authenticatedSession
  closeSession authenticatedSession
  return res

-- | Open a connection, authenticate, execute some action and close the connection.
--
-- It is the safe way of using SimpleSSH. This function is to be used to authenticate with a key, otherwise see 'withSessionPassword'.