diff --git a/DESCRIPTION b/DESCRIPTION index de3dc08..b4b893d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -13,6 +13,7 @@ RoxygenNote: 7.2.1 Depends: R (>= 3.6) Imports: + clipr, dplyr, httr, tibble diff --git a/NAMESPACE b/NAMESPACE index 15ec241..a20b871 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ S3method(print,rtoot_bearer) S3method(print,rtoot_client) export(auth_setup) +export(convert_token_to_envvar) export(get_account) export(get_account_blocks) export(get_account_bookmarks) @@ -34,3 +35,4 @@ export(post_toot) export(post_user) export(search_accounts) export(verify_credentials) +export(verify_envvar) diff --git a/R/auth.R b/R/auth.R index 797e277..bc9a194 100644 --- a/R/auth.R +++ b/R/auth.R @@ -7,15 +7,20 @@ #' @param name give the token a name, in case you want to store more than one. #' @param path path to store the token in. The default is to store tokens in the #' path returned by `tools::R_user_dir("rtoot", "config")`. -#' +#' @param clipboard logical, whether to export the token to the clipboard #' @details If either `name` or `path` are set to `FALSE`, the token is only -#' returned and not saved. -#' +#' returned and not saved. If you would like to save your token as an environment variable, +#' please set `clipboard` to `TRUE`. Your token will be copied to clipboard in the environment variable +#' format. Please paste it into your environment file, e.g. ".Renviron", and restart +#' your R session. #' @return A bearer token +#' @seealso [verify_credentials()], [convert_token_to_envvar()] #' @examples +#' \dontrun{ #' auth_setup("mastodon.social", "public") +#' } #' @export -auth_setup <- function(instance = NULL, type = NULL, name = NULL, path = NULL) { +auth_setup <- function(instance = NULL, type = NULL, name = NULL, path = NULL, clipboard = FALSE) { while (is.null(instance) || instance == "") { instance <- readline(prompt = "On which instance do you want to authenticate (e.g., \"mastodon.social\")? ") } @@ -24,10 +29,17 @@ auth_setup <- function(instance = NULL, type = NULL, name = NULL, path = NULL) { type <- c("public", "user")[utils::menu(c("public", "user"), title = "What type of token do you want?")] } token <- create_token(client, type = type) - if (!isFALSE(name) && !isFALSE(path)) token_path <- save_auth_rtoot(token, name, path) + if (!isFALSE(name) && !isFALSE(path)) { + token_path <- save_auth_rtoot(token, name, path) + } options("rtoot_token" = token_path) verify_credentials(token) # this should be further up before saving, but seems to often fail - check_token_rtoot(token) + if (clipboard) { + convert_token_to_envvar(token) + return(invisible(token)) + } else { + check_token_rtoot(token) + } } ## login described at https://docs.joinmastodon.org/client/authorized/ @@ -100,11 +112,11 @@ create_token <- function(client, type = "public"){ bearer } - -#' verify mastodon credentials +#' Verify mastodon credentials #' #' @param token bearer token, either public or user level #' @return success or failure message of the verification process +#' @details If you have created your token as an environment variable, use `verify_envvar` to verify your token. #' @examples #' \dontrun{ #' #read a token from a file @@ -139,6 +151,13 @@ verify_credentials <- function(token) { invisible(acc) } +#' @export +#' @rdname verify_credentials +verify_envvar <- function() { + token <- get_token_from_envvar() + verify_credentials(token) +} + #' save a bearer token to file #' #' @param token bearer token created with [create_token] @@ -171,15 +190,77 @@ get_auth_rtoot <- function(){ is_auth_rtoot <- function(token) inherits(token, "rtoot_bearer") +#' Convert token to environment variable +#' @inheritParams verify_credentials +#' @param message logical whether to display message +#' @inheritParams auth_setup +#' @return Token (in environment variable format), invisibily +#' @examples +#' \dontrun{ +#' x <- auth_setup("mastodon.social", "public") +#' envvar <- convert_token_to_envvar(x) +#' envvar +#' } +#' @export +convert_token_to_envvar <- function(token, message = TRUE, clipboard = TRUE) { + envvar_string <- paste0("RTOOT_DEFAULT_TOKEN=\"", token$bearer, ";", token$type, ";", token$instance, "\"") + if (isTRUE(clipboard)) { + if (clipr::clipr_available()) { + clipr::write_clip(envvar_string) + if (message) { + message("Token (in environment variable format) has been copied to clipboard.") + } + } else { + if (message) { + message("Clipboard is not available.") + } + } + } + return(invisible(envvar_string)) +} + +get_token_from_envvar <- function(envvar = "RTOOT_DEFAULT_TOKEN", check_stop = TRUE) { + dummy <- list(bearer = "") + dummy$type <- "" + dummy$instance <- "" + class(dummy) <- "rtoot_bearer" + if (Sys.getenv(envvar) == "") { + if (check_stop) { + stop("envvar not found.") + } else { + ## warn the testers + message("You should do software testing with the `RTOOT_DEFAULT_TOKEN` envvar!\nRead: https://github.com/schochastics/rtoot/wiki/vcr") + return(dummy) + } + } + res <- strsplit(x = Sys.getenv(envvar), split = ";")[[1]] + if (length(res) != 3) { + if (check_stop) { + stop("Your envvar is malformed") + } else { + return(NULL) + } + } + bearer <- list(bearer = res[1]) + bearer$type <- res[2] + bearer$instance <- res[3] + class(bearer) <- "rtoot_bearer" + bearer +} + # check if a token is available and return one if not +## it checks the envvar RTOOT_DEFAULT_TOKEN first; then RDS; check_token_rtoot <- function(token = NULL) { - selection <- NULL - if(is.null(token)){ - + if (Sys.getenv("RTOOT_DEFAULT_TOKEN") != "") { + token <- get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) + if (!is.null(token)) { + return(token) + } + ## the envvar is malformed, go to the legacy RDS method. + } token_path <- options("rtoot_token")$rtoot_token - if (length(token_path) == 0) { token_path <- utils::head(list.files(tools::R_user_dir("rtoot", "config"), full.names = TRUE, @@ -216,6 +297,5 @@ check_token_rtoot <- function(token = NULL) { selection <- 2L } } - invisible(token) } diff --git a/man/auth_setup.Rd b/man/auth_setup.Rd index bdae43d..d54b671 100644 --- a/man/auth_setup.Rd +++ b/man/auth_setup.Rd @@ -4,7 +4,13 @@ \alias{auth_setup} \title{Authenticate with a Mastodon instance} \usage{ -auth_setup(instance = NULL, type = NULL, name = NULL, path = NULL) +auth_setup( + instance = NULL, + type = NULL, + name = NULL, + path = NULL, + clipboard = FALSE +) } \arguments{ \item{instance}{a public instance of Mastodon (e.g., mastodon.social).} @@ -17,6 +23,8 @@ query your followers).} \item{path}{path to store the token in. The default is to store tokens in the path returned by \code{tools::R_user_dir("rtoot", "config")}.} + +\item{clipboard}{logical, whether to export the token to the clipboard} } \value{ A bearer token @@ -26,8 +34,16 @@ Authenticate with a Mastodon instance } \details{ If either \code{name} or \code{path} are set to \code{FALSE}, the token is only -returned and not saved. +returned and not saved. If you would like to save your token as an environment variable, +please set \code{clipboard} to \code{TRUE}. Your token will be copied to clipboard in the environment variable +format. Please paste it into your environment file, e.g. ".Renviron", and restart +your R session. } \examples{ +\dontrun{ auth_setup("mastodon.social", "public") } +} +\seealso{ +\code{\link[=verify_credentials]{verify_credentials()}}, \code{\link[=convert_token_to_envvar]{convert_token_to_envvar()}} +} diff --git a/man/convert_token_to_envvar.Rd b/man/convert_token_to_envvar.Rd new file mode 100644 index 0000000..52df06b --- /dev/null +++ b/man/convert_token_to_envvar.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/auth.R +\name{convert_token_to_envvar} +\alias{convert_token_to_envvar} +\title{Convert token to environment variable} +\usage{ +convert_token_to_envvar(token, message = TRUE, clipboard = TRUE) +} +\arguments{ +\item{token}{bearer token, either public or user level} + +\item{message}{logical whether to display message} + +\item{clipboard}{logical, whether to export the token to the clipboard} +} +\value{ +Token (in environment variable format), invisibily +} +\description{ +Convert token to environment variable +} +\examples{ +\dontrun{ +x <- auth_setup("mastodon.social", "public") +envvar <- convert_token_to_envvar(x) +envvar +} +} diff --git a/man/verify_credentials.Rd b/man/verify_credentials.Rd index 6547299..e00921c 100644 --- a/man/verify_credentials.Rd +++ b/man/verify_credentials.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/auth.R \name{verify_credentials} \alias{verify_credentials} -\title{verify mastodon credentials} +\alias{verify_envvar} +\title{Verify mastodon credentials} \usage{ verify_credentials(token) + +verify_envvar() } \arguments{ \item{token}{bearer token, either public or user level} @@ -13,7 +16,10 @@ verify_credentials(token) success or failure message of the verification process } \description{ -verify mastodon credentials +Verify mastodon credentials +} +\details{ +If you have created your token as an environment variable, use \code{verify_envvar} to verify your token. } \examples{ \dontrun{ diff --git a/tests/fixtures/envvar.yml b/tests/fixtures/envvar.yml new file mode 100644 index 0000000..0e7f47a --- /dev/null +++ b/tests/fixtures/envvar.yml @@ -0,0 +1,57 @@ +http_interactions: +- request: + method: get + uri: https://emacs.ch/api/v1/accounts/verify_credentials + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + Authorization: Bearer <<>> + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + cache-control: no-store + content-encoding: gzip + content-security-policy: 'base-uri ''none''; default-src ''none''; frame-ancestors + ''none''; font-src ''self'' https://emacs.ch; img-src ''self'' https: data: + blob: https://emacs.ch; style-src ''self'' https://emacs.ch ''nonce-7MquUyr6YSyn1MSrGbaIUw==''; + media-src ''self'' https: data: https://emacs.ch; frame-src ''self'' https:; + manifest-src ''self'' https://emacs.ch; connect-src ''self'' data: blob: https://emacs.ch + https://emacs.ch wss://emacs.ch; script-src ''self'' https://emacs.ch ''unsafe-eval''; + child-src ''self'' blob: https://emacs.ch; worker-src ''self'' blob: https://emacs.ch' + content-type: application/json; charset=utf-8 + date: Tue, 15 Nov 2022 09:37:12 GMT + etag: W/"7f9afc2bc6267d16af26b905bf2789e9" + permissions-policy: interest-cohort=() + server: Mastodon + strict-transport-security: max-age=63072000; includeSubDomains + vary: + - Accept-Encoding + - Origin + x-cached: MISS + x-content-type-options: nosniff + x-frame-options: DENY + x-ratelimit-limit: '300' + x-ratelimit-remaining: '299' + x-ratelimit-reset: '2022-11-15T09:40:00.497295Z' + x-request-id: ccba4244-aae0-4d53-b1f3-9d9279c40f55 + x-runtime: '0.018026' + x-xss-protection: '0' + body: + encoding: '' + file: no + string: '{"id":"109337011845249544","username":"chainsawriot","acct":"chainsawriot","display_name":"chainsawriot","locked":false,"bot":false,"discoverable":false,"group":false,"created_at":"2022-11-13T00:00:00.000Z","note":"","url":"https://emacs.ch/@chainsawriot","avatar":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","avatar_static":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","header":"https://emacs.ch/headers/original/missing.png","header_static":"https://emacs.ch/headers/original/missing.png","followers_count":125,"following_count":62,"statuses_count":21,"last_status_at":"2022-11-14","noindex":true,"source":{"privacy":"public","sensitive":false,"language":"","note":"","fields":[{"name":"Stack","value":":emacs:, + R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine + Ahnung","verified_at":null},{"name":"Web","value":"https://chainsawriot.com/about/","verified_at":"2022-11-14T12:22:07.327+00:00"}],"follow_requests_count":0},"emojis":[{"shortcode":"emacs","url":"https://emacs.ch/system/custom_emojis/images/000/000/358/original/9f320443168e793f.png","static_url":"https://emacs.ch/system/custom_emojis/images/000/000/358/static/9f320443168e793f.png","visible_in_picker":true}],"fields":[{"name":"Stack","value":":emacs:, + R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine + Ahnung","verified_at":null},{"name":"Web","value":"\u003ca href=\"https://chainsawriot.com/about/\" + target=\"_blank\" rel=\"nofollow noopener noreferrer me\"\u003e\u003cspan + class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003echainsawriot.com/about/\u003c/span\u003e\u003cspan + class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e","verified_at":"2022-11-14T12:22:07.327+00:00"}],"role":{"id":"-99","name":"","permissions":"65536","color":"","highlighted":false}}' + recorded_at: 2022-11-15 09:37:12 GMT + recorded_with: vcr/1.1.0, webmockr/0.8.2 diff --git a/tests/testthat/setup-rtoot.R b/tests/testthat/setup-rtoot.R index dcf79b7..dd87de5 100644 --- a/tests/testthat/setup-rtoot.R +++ b/tests/testthat/setup-rtoot.R @@ -1,7 +1,7 @@ library("vcr") # *Required* as vcr is set up on loading invisible( vcr::vcr_configure( - filter_sensitive_data = list("<<>>" = Sys.getenv('RTOOT_DEFAULT_TOKEN')), + filter_sensitive_data = list("<<>>" = get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE)$bearer), dir = vcr::vcr_test_path("fixtures") )) vcr::check_cassette_names() diff --git a/tests/testthat/test-auth_envvar.R b/tests/testthat/test-auth_envvar.R new file mode 100644 index 0000000..0683bb8 --- /dev/null +++ b/tests/testthat/test-auth_envvar.R @@ -0,0 +1,53 @@ +original_envvar <- Sys.getenv("RTOOT_DEFAULT_TOKEN") + +test_that("convert_token_to_envvar", { + fake_token <- list(bearer = paste0(rep("a", 43), collapse = "")) + fake_token$type <- "user" + fake_token$instance <- paste0(rep("b", 10), collapse = "") + class(fake_token) <- "rtoot_bearer" + x <- convert_token_to_envvar(fake_token, message = FALSE, clipboard = FALSE) + expected_output <- paste0("RTOOT_DEFAULT_TOKEN=\"", + paste0(rep("a", 43), collapse = ""), + ";user;", + paste0(rep("b", 10), collapse = ""), "\"") + expect_equal(x, expected_output) +}) + +test_that("convert_token_to_envvar (clipboard)", { + skip_if_not(clipr::clipr_available()) + fake_token <- list(bearer = paste0(rep("a", 43), collapse = "")) + fake_token$type <- "user" + fake_token$instance <- paste0(rep("b", 10), collapse = "") + class(fake_token) <- "rtoot_bearer" + expected_output <- paste0("RTOOT_DEFAULT_TOKEN=\"", + paste0(rep("a", 43), collapse = ""), + ";user;", + paste0(rep("b", 10), collapse = ""), "\"") + expect_message(convert_token_to_envvar(fake_token, message = TRUE, clipboard = TRUE)) + x <- convert_token_to_envvar(fake_token, message = FALSE, clipboard = TRUE) + clipboard_content <- clipr::read_clip() + expect_equal(clipr::read_clip(), expected_output) + expect_equal(x, expected_output) +}) + +test_that("get_token_from_envvar", { + ## NB: This is not an exported function + skip_if(Sys.getenv("RTOOT_DEFAULT_TOKEN") == "") + x <- get_token_from_envvar() + expect_true("rtoot_bearer" %in% class(x)) + ## temper the envvar + Sys.setenv(RTOOT_DEFAULT_TOKEN = "") + expect_error(x <- get_token_from_envvar()) + expect_message(x <- get_token_from_envvar(check_stop = FALSE)) + ## should still return a dummy token + expect_true("rtoot_bearer" %in% class(x)) + expect_equal(x$bearer, "") + ## malformed + Sys.setenv(RTOOT_DEFAULT_TOKEN = "elonmusk") + expect_error(x <- get_token_from_envvar(check_stop = TRUE)) + x <- get_token_from_envvar(check_stop = FALSE) + expect_null(x) + expect_false("rtoot_bearer" %in% class(x)) +}) + +Sys.setenv(RTOOT_DEFAULT_TOKEN = original_envvar) diff --git a/tests/testthat/test-auth_verify.R b/tests/testthat/test-auth_verify.R new file mode 100644 index 0000000..f0c14df --- /dev/null +++ b/tests/testthat/test-auth_verify.R @@ -0,0 +1,15 @@ +original_envvar <- Sys.getenv("RTOOT_DEFAULT_TOKEN") +Sys.setenv(RTOOT_DEFAULT_TOKEN = "abc;user;emacs.ch") + +test_that("verify_envvar (Good case)", { + vcr::use_cassette("envvar", { + expect_error(capture_message(verify_envvar()), NA) + }) +}) + +test_that("verify_envvar (Bad case)", { + Sys.setenv(RTOOT_DEFAULT_TOKEN = "") + expect_error(verify_envvar()) +}) + +Sys.setenv(RTOOT_DEFAULT_TOKEN = original_envvar) diff --git a/tests/testthat/test-get_account.R b/tests/testthat/test-get_account.R index 8fd6675..ab7fdbf 100644 --- a/tests/testthat/test-get_account.R +++ b/tests/testthat/test-get_account.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_account", { vcr::use_cassette("get_account_default", { diff --git a/tests/testthat/test-get_account_auth.R b/tests/testthat/test-get_account_auth.R index 4c4c464..75e9da2 100644 --- a/tests/testthat/test-get_account_auth.R +++ b/tests/testthat/test-get_account_auth.R @@ -4,10 +4,9 @@ ## get_account_favourites, get_account_blocks, ## get_account_mutes -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" id <- "109281650341067731" diff --git a/tests/testthat/test-get_account_statuses.R b/tests/testthat/test-get_account_statuses.R index c36babd..c7e2978 100644 --- a/tests/testthat/test-get_account_statuses.R +++ b/tests/testthat/test-get_account_statuses.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_account_statuses", { vcr::use_cassette("get_account_statuses_default", { diff --git a/tests/testthat/test-get_context.R b/tests/testthat/test-get_context.R index de180e2..0dde50d 100644 --- a/tests/testthat/test-get_context.R +++ b/tests/testthat/test-get_context.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_context", { vcr::use_cassette("get_context_default", { diff --git a/tests/testthat/test-get_favourited_by.R b/tests/testthat/test-get_favourited_by.R index da76682..ce40867 100644 --- a/tests/testthat/test-get_favourited_by.R +++ b/tests/testthat/test-get_favourited_by.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_favourited_by", { vcr::use_cassette("get_favourited_by_default", { diff --git a/tests/testthat/test-get_poll.R b/tests/testthat/test-get_poll.R index d11e07d..bbccdd0 100644 --- a/tests/testthat/test-get_poll.R +++ b/tests/testthat/test-get_poll.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_poll", { vcr::use_cassette("get_poll_default", { diff --git a/tests/testthat/test-get_reblogged_by.R b/tests/testthat/test-get_reblogged_by.R index f8e8d2d..35e04a2 100644 --- a/tests/testthat/test-get_reblogged_by.R +++ b/tests/testthat/test-get_reblogged_by.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_reblogged_by", { vcr::use_cassette("get_reblogged_by_default", { diff --git a/tests/testthat/test-get_status.R b/tests/testthat/test-get_status.R index a4faa65..69b5c90 100644 --- a/tests/testthat/test-get_status.R +++ b/tests/testthat/test-get_status.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_status", { vcr::use_cassette("get_status_default", { diff --git a/tests/testthat/test-get_timeline_hashtag.R b/tests/testthat/test-get_timeline_hashtag.R index bbffac5..98870bd 100644 --- a/tests/testthat/test-get_timeline_hashtag.R +++ b/tests/testthat/test-get_timeline_hashtag.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_timeline_hashtag", { vcr::use_cassette("get_timeline_hashtag_default", { diff --git a/tests/testthat/test-get_timeline_home.R b/tests/testthat/test-get_timeline_home.R index ab1d071..0287684 100644 --- a/tests/testthat/test-get_timeline_home.R +++ b/tests/testthat/test-get_timeline_home.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_timeline_home", { vcr::use_cassette("get_timeline_home_default", { diff --git a/tests/testthat/test-get_timeline_list.R b/tests/testthat/test-get_timeline_list.R index f4ebee0..7bb600b 100644 --- a/tests/testthat/test-get_timeline_list.R +++ b/tests/testthat/test-get_timeline_list.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_timeline_list", { vcr::use_cassette("get_timeline_list_default", { diff --git a/tests/testthat/test-get_timeline_public.R b/tests/testthat/test-get_timeline_public.R index 50575af..bc6196b 100644 --- a/tests/testthat/test-get_timeline_public.R +++ b/tests/testthat/test-get_timeline_public.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_timeline_public", { vcr::use_cassette("get_timeline_public_default", { diff --git a/tests/testthat/test-instances.R b/tests/testthat/test-instances.R index cfc66c8..b5a55ed 100644 --- a/tests/testthat/test-instances.R +++ b/tests/testthat/test-instances.R @@ -1,9 +1,7 @@ ## tests for all instance functions - -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("get_fedi_instances", { vcr::use_cassette("get_fedi_instances_default", { diff --git a/tests/testthat/test-post.R b/tests/testthat/test-post.R index 00f96e7..a9e398f 100644 --- a/tests/testthat/test-post.R +++ b/tests/testthat/test-post.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("post_toot, defensive", { expect_error(post_toot(status = NA, token = fake_token)) diff --git a/tests/testthat/test-search_accounts.R b/tests/testthat/test-search_accounts.R index 3ab2095..2a62e0f 100644 --- a/tests/testthat/test-search_accounts.R +++ b/tests/testthat/test-search_accounts.R @@ -1,7 +1,6 @@ -fake_token <- list(bearer = Sys.getenv("RTOOT_DEFAULT_TOKEN")) +fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE) fake_token$type <- "user" fake_token$instance <- "social.tchncs.de" -class(fake_token) <- "rtoot_bearer" test_that("search_accounts", { vcr::use_cassette("search_accounts_default", { diff --git a/vignettes/auth.Rmd b/vignettes/auth.Rmd index 07e20cb..926efb1 100644 --- a/vignettes/auth.Rmd +++ b/vignettes/auth.Rmd @@ -14,6 +14,8 @@ knitr::opts_chunk$set( ) ``` +# Obtain and use a token + When a function in `rtoot` can't find a valid token on your computer, it automatically starts authentication. If you want to start the process manually, you can do so by calling: @@ -46,3 +48,20 @@ Or you can set the default token in the options at the start of a session: ```{r options, eval=FALSE} options("rtoot_token" = file.path(tools::R_user_dir("rtoot", "config"), "account1.rds")) ``` + +# Environment variable + +For advanced users, you can also store your token as an environment variable (envvar). You can either obtain a token by calling + +```{r clipboard, eval = FALSE} +auth_setup(clipboard = TRUE) +``` + +Or, if you already have a token + +```{r convert, eval = FALSE} +token <- readRDS(file.path(tools::R_user_dir("rtoot", "config"), "account1.rds")) +content <- convert_token_to_envvar(token) +``` + +Paste the content from clipboard to your configuration file. If you don't have access to clipboard, inspect `content`.