Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the creds_envvar() function #159

Merged
merged 5 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Suggests:
SystemRequirements: pandoc (>= 1.12.3) - http://pandoc.org
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Language: en-US
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export(create_smtp_creds_file)
export(create_smtp_creds_key)
export(creds)
export(creds_anonymous)
export(creds_envvar)
export(creds_file)
export(creds_key)
export(get_html_str)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# blastula (development version)

* Added the `creds_envvar()` credential helper function, which allows for SMTP password input via an environment variable. [#159](https://github.com/rich-iannone/blastula/pull/159)

* Fixed incorrect rendering in Outlook, due to wrong line endings used for quoted-printable encoding. Thanks @jdbarillas for identifying the source of the issue! [#153](https://github.com/rich-iannone/blastula/pull/153)

* Fixed Unicode issues on Windows. [#154](https://github.com/rich-iannone/blastula/pull/154)
Expand Down
42 changes: 42 additions & 0 deletions R/creds_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#' file stored on disk. We can create that file using the
#' [create_smtp_creds_file()] function.
#'
#' The [creds_envvar()] credential helper reads the password from the
#' `SMTP_PASSWORD` environment variable (or an environment variable name that
#' you specify). If using environment variables for other parameters, call
#' [Sys.getenv()] manually (e.g. `user = Sys.getenv("SMTP_USER")`).
#'
#' @param user The username for the email account. Typically, this is the email
#' address associated with the account.
#' @param provider An optional email provider shortname for autocompleting STMP
Expand All @@ -31,6 +36,9 @@
#' and `port` parameters are the address and port for the SMTP server;
#' `use_ssl` is an option as to whether to use SSL: supply a `TRUE` or `FALSE`
#' value.
#' @param pass_envvar The name of the environment variable that holds the value
#' for an email account password. This is only used in the [creds_envvar()]
#' credential helper function.
#' @param id When using the [creds_key()] credential helper, the ID value of the
#' key (in the system key-value store) needs to be given here. This was
#' explicitly provided when using the [create_smtp_creds_key()] function (with
Expand Down Expand Up @@ -88,6 +96,40 @@ creds_anonymous <- function(provider = NULL,
creds_list
}

#' @rdname credential_helpers
#' @export
creds_envvar <- function(user = NULL,
pass_envvar = "SMTP_PASSWORD",
provider = NULL,
host = NULL,
port = NULL,
use_ssl = TRUE) {

# Obtain the password from an environment variable
# using the `pass_envar` value
password <- Sys.getenv(pass_envvar, unset = NA)

# If `Sys.getenv()` returns NA, then stop
if (is.na(password)) {
stop("The environment variable defined by `pass_envvar` doesn't exist.",
call. = FALSE)
}

# Create a credentials list from the function inputs
creds_list <-
create_credentials_list(
provider = provider,
user = user,
password = password,
host = host,
port = port,
use_ssl = use_ssl
)

class(creds_list) <- c("creds", "blastula_creds")
creds_list
}

#' @rdname credential_helpers
#' @export
creds_key <- function(id) {
Expand Down
19 changes: 19 additions & 0 deletions man/credential_helpers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.