R/keyring.R

#' Store password
#'
#' Store password in a keyring
#' @param label Label
#' @param key Key
#' @param value Value
#' @param password Password to store
#' @param invisible Should invisible() be called?
#' @export

pass_store = function(label, key, value, password, invisible = TRUE) {

  tux_only()

  command = paste(
    paste(
      "secret-tool store --label",
      paste0("'", label, "'"),
      sep = "="
    ),
    key, value,
    sep = " "
  )

  call_keyring(command, password = password, invisible = invisible)
}

#' Lookup password
#'
#' Lookup password
#' @param key Key
#' @param value Value
#' @param invisible Should invisible() be called?
#' @export

pass_lookup = function(key, value, invisible = TRUE) {

  tux_only()

  command = paste(
    "secret-tool lookup",
    key, value,
    sep = " "
  )

  call_keyring(command, invisible = invisible)
}

#' Clear password
#'
#' Clear password
#' @param key Key
#' @param value Value
#' @param invisible Should invisible() be called?
#' @export

pass_clear = function(key, value, invisible = TRUE) {

  tux_only()

  command = paste(
    "secret-tool clear",
    key, value,
    sep = " "
  )

  call_keyring(command, invisible = invisible)
}

#' Search password
#'
#' Search password
#' @param key Key
#' @param value Value
#' @param invisible Should invisible() be called?
#' @export

pass_search = function(key, value, invisible = TRUE) {

  tux_only()

  command = paste(
    "secret-tool search",
    key, value,
    sep = " "
  )

  call_keyring(command, invisible = invisible)
}

# Main call
call_keyring = function(command, password = NULL, invisible = TRUE) {
  if (invisible && !is.null(password))
    invisible(
      system(
        command = command,
        intern = TRUE,
        input = password
      )
    )
  else if (!is.null(password))
    system(
      command = command,
      intern = TRUE,
      input = password
    )
  else if (invisible)
    invisible(
      system(
        command = command,
        intern = TRUE
      )
    )
  else
    system(
      command = command,
      intern = TRUE
    )
}

# In case we are not on tux
tux_only = function() {
  if (!Sys.info()["sysname"] == "Linux")
    stop(
      "Only on linux with Gnome keyring (sudo apt install libsecret-tools)",
      call. = FALSE
      )
}

libsecret_tools = function() {

  tux_only()

  out = system("apt-cache policy libsecret-tools", intern = TRUE)

  if (length(out) > 0)
    "Package libsecret-tools present"

  else
    "Package libsecret-tools missing, run:\nsudo apt install libsecret-tools"
}

packageStartupMessage(
  libsecret_tools()
)
jchrom/jcmisc documentation built on May 18, 2019, 10:23 p.m.