R/add_account.R

Defines functions add_account

Documented in add_account

#' add_account
#'
#' Add an account to the "<schema name>.accounts" table.
#'
#' @param conn the database connection
#' @param email the account email
#' @param host_secret the secret key for the hosted API.  The \code{host_secret}
#' goes in the config.yml of your hosted API and is used to encrypt/decrypt the
#' account secrets.
#' @param account_secret the account secret key as generated by \code{\link{create_secret}()}.
#' @param schema the name of the database schema
#'
#' @importFrom uuid UUIDgenerate
#' @importFrom digest digest
#' @importFrom pool dbExecute
#'
#' @return a named list with 2 elements
#' - uid: the uid for the newly created account
#' - secret: the api secret key for the newly created account
#'
#' @export
#'
add_account <- function(
  conn,
  email,
  host_secret,
  account_secret = create_secret(),
  schema = "polished"
) {

  uid <- uuid::UUIDgenerate()
  account_secret_hashed <- digest::digest(account_secret)


  pool::dbExecute(
    conn,
    paste0("INSERT INTO ", schema, ".accounts (uid, email, polished_key, hashed_polished_key) VALUES
    ($1, $2, PGP_SYM_ENCRYPT($3, $4), $5)"),
    list(
      uid,
      email,
      account_secret,
      host_secret,
      account_secret_hashed
    )
  )

  list(
    uid = uid,
    secret = account_secret
  )
}
Tychobra/polishedapi documentation built on July 19, 2020, 11:41 p.m.