From 4bc51c71330b8e9ae35c3a2698ff2db7a67ac456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 14:07:42 +0100 Subject: [PATCH 01/12] draft test --- DESCRIPTION | 3 ++- R/meetup_auth.R | 21 ++++++++++++++++---- tests/testthat/test-meetup_auth.R | 32 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-meetup_auth.R diff --git a/DESCRIPTION b/DESCRIPTION index b61d237f..bf2dc3bf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -70,7 +70,8 @@ Suggests: rmarkdown, testthat, vcr, - covr + covr, + webfakes VignetteBuilder: knitr Encoding: UTF-8 diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 759592b4..7174c6f5 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -111,10 +111,16 @@ meetup_auth <- function(token = meetup_token_path(), if (is.null(token)) { - meetup_app <- httr::oauth_app("meetup", key = key, secret = secret) + meetup_app <- httr::oauth_app( + "meetup", + key = key, + secret = secret, + redirect_uri = httr::oauth_callback() + ) + meetup_endpoints <- httr::oauth_endpoint( - authorize = 'https://secure.meetup.com/oauth2/authorize', - access = 'https://secure.meetup.com/oauth2/access' + authorize = paste0(meetup_auth_prefix(), 'oauth2/authorize'), + access = paste0(meetup_auth_prefix(), 'oauth2/access') ) if (!cache && !is.null(token_path)) { @@ -263,7 +269,9 @@ token_available <- function(verbose = TRUE) { #' } meetup_deauth <- function(clear_cache = TRUE, verbose = getOption("meetupr.verbose", rlang::is_interactive())) { - + if (is.null(meetup_token_path())) { + return(NULL) + } if (clear_cache && file.exists(meetup_token_path())) { if (verbose) { message( @@ -356,3 +364,8 @@ save_and_refresh_token <- function(token, path) { appdir_path <- function() { file.path(rappdirs::user_data_dir("meetupr", "meetupr"), "meetupr-token.rds") } + +meetup_auth_prefix <- function() { + + Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/") +} diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R new file mode 100644 index 00000000..d601c049 --- /dev/null +++ b/tests/testthat/test-meetup_auth.R @@ -0,0 +1,32 @@ +test_that("meetup_auth() works", { + +meetup_app <- webfakes::new_app_process( + webfakes::oauth2_resource_app( + refresh_duration = .Machine$integer.max, + access_duration = 10L, + authorize_endpoint = "/oauth2/authorize", + token_endpoint = "/oauth2/access" + ), + opts = webfakes::server_opts(num_threads = 3) +) +meetup_app$start() + +# Register an app +url <- paste0( + meetup_app$url("/register"), + "?name=3P%20app", + "&redirect_uri=", httr::oauth_callback() +) +reg_resp <- httr::GET(url) +reg_resp +regdata <- httr::content(reg_resp) +withr::local_options( + list( + "meetupr.consumer_key" = regdata$client_id[[1]], + "meetupr.consumer_secret" = regdata$client_secret[[1]] + ) + ) +withr::local_envvar(list("MEETUP_AUTH_URL" = meetup_app$url())) +td <- withr::local_tempdir() +meetup_auth(new_user = TRUE, token_path = file.path(td, "token.rds"), use_appdir = FALSE) +}) From e344c4bfe304d840fa03735c49e3cf8ff3275bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 14:12:36 +0100 Subject: [PATCH 02/12] stuff --- tests/testthat/test-meetup_auth.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R index d601c049..4fee1733 100644 --- a/tests/testthat/test-meetup_auth.R +++ b/tests/testthat/test-meetup_auth.R @@ -1,13 +1,13 @@ test_that("meetup_auth() works", { + # New app imitating meetup dot com meetup_app <- webfakes::new_app_process( webfakes::oauth2_resource_app( refresh_duration = .Machine$integer.max, access_duration = 10L, authorize_endpoint = "/oauth2/authorize", token_endpoint = "/oauth2/access" - ), - opts = webfakes::server_opts(num_threads = 3) + ) ) meetup_app$start() @@ -18,7 +18,6 @@ url <- paste0( "&redirect_uri=", httr::oauth_callback() ) reg_resp <- httr::GET(url) -reg_resp regdata <- httr::content(reg_resp) withr::local_options( list( From 434674f3b245ddc5271735bb9f1374303f17f0a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 14:47:09 +0100 Subject: [PATCH 03/12] progress --- R/meetup_auth.R | 8 ++++---- tests/testthat/test-meetup_auth.R | 12 ++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 7174c6f5..44c9022f 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -119,8 +119,8 @@ meetup_auth <- function(token = meetup_token_path(), ) meetup_endpoints <- httr::oauth_endpoint( - authorize = paste0(meetup_auth_prefix(), 'oauth2/authorize'), - access = paste0(meetup_auth_prefix(), 'oauth2/access') + authorize = paste0(meetup_auth_prefix(), 'authorize'), + access = paste0(meetup_auth_prefix(), 'access') ) if (!cache && !is.null(token_path)) { @@ -350,7 +350,7 @@ meetup_token_path <- function() { save_and_refresh_token <- function(token, path) { - if (token$credentials$expires_in < 60) { + if (token$credentials$expires_in < 60 || token$credentials$expiry < 60) { token$refresh() if(!is.null(path)) { @@ -367,5 +367,5 @@ appdir_path <- function() { meetup_auth_prefix <- function() { - Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/") + Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/oauth2/") } diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R index 4fee1733..1103f5c4 100644 --- a/tests/testthat/test-meetup_auth.R +++ b/tests/testthat/test-meetup_auth.R @@ -5,8 +5,8 @@ meetup_app <- webfakes::new_app_process( webfakes::oauth2_resource_app( refresh_duration = .Machine$integer.max, access_duration = 10L, - authorize_endpoint = "/oauth2/authorize", - token_endpoint = "/oauth2/access" + authorize_endpoint = "/authorize", + token_endpoint = "/access" ) ) meetup_app$start() @@ -14,7 +14,7 @@ meetup_app$start() # Register an app url <- paste0( meetup_app$url("/register"), - "?name=3P%20app", + "?name=meetup", "&redirect_uri=", httr::oauth_callback() ) reg_resp <- httr::GET(url) @@ -27,5 +27,9 @@ withr::local_options( ) withr::local_envvar(list("MEETUP_AUTH_URL" = meetup_app$url())) td <- withr::local_tempdir() -meetup_auth(new_user = TRUE, token_path = file.path(td, "token.rds"), use_appdir = FALSE) +meetup_auth( + new_user = TRUE, + token_path = file.path(td, "token.rds"), + use_appdir = FALSE) + }) From d87a6f306b69738e97ba4833bd11457a713bb48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 14:56:23 +0100 Subject: [PATCH 04/12] add --- R/meetup_auth.R | 13 +++++++++++-- tests/testthat/test-meetup_auth.R | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 44c9022f..8f79571a 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -149,7 +149,7 @@ meetup_auth <- function(token = meetup_token_path(), } } - meetup_token <- httr::oauth2.0_token( + meetup_token <- TOKEN_FUNCTION( meetup_endpoints, meetup_app, cache = cache # if FALSE won't be saved, if character will be saved @@ -350,7 +350,7 @@ meetup_token_path <- function() { save_and_refresh_token <- function(token, path) { - if (token$credentials$expires_in < 60 || token$credentials$expiry < 60) { + if (purrr::compact(c(token$credentials$expires_in, token$credentials$expiry)) < 60) { token$refresh() if(!is.null(path)) { @@ -369,3 +369,12 @@ meetup_auth_prefix <- function() { Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/oauth2/") } + +TOKEN_FUNCTION <- function(...) { + if (Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/oauth2/") + != "https://secure.meetup.com/oauth2/") { + return(webfakes::oauth2_httr_login(httr::oauth2.0_token(...))) + } + + return(httr::oauth2.0_token(...)) +} diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R index 1103f5c4..d3563c6d 100644 --- a/tests/testthat/test-meetup_auth.R +++ b/tests/testthat/test-meetup_auth.R @@ -4,7 +4,7 @@ test_that("meetup_auth() works", { meetup_app <- webfakes::new_app_process( webfakes::oauth2_resource_app( refresh_duration = .Machine$integer.max, - access_duration = 10L, + access_duration = 100L, authorize_endpoint = "/authorize", token_endpoint = "/access" ) @@ -27,9 +27,11 @@ withr::local_options( ) withr::local_envvar(list("MEETUP_AUTH_URL" = meetup_app$url())) td <- withr::local_tempdir() -meetup_auth( +token <- meetup_auth( new_user = TRUE, token_path = file.path(td, "token.rds"), use_appdir = FALSE) + expect_s3_class(token, "Token2.0") + }) From 583c09a5d1e45191ce3e2b15ea9387d453df20bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:01:47 +0100 Subject: [PATCH 05/12] at least it is nicer --- R/meetup_auth.R | 3 +-- tests/testthat/test-meetup_auth.R | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 8f79571a..a9bb93c2 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -371,8 +371,7 @@ meetup_auth_prefix <- function() { } TOKEN_FUNCTION <- function(...) { - if (Sys.getenv("MEETUP_AUTH_URL", "https://secure.meetup.com/oauth2/") - != "https://secure.meetup.com/oauth2/") { + if (nzchar(Sys.getenv("MEETUP_TESTTHAT"))) { return(webfakes::oauth2_httr_login(httr::oauth2.0_token(...))) } diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R index d3563c6d..4ffd69c4 100644 --- a/tests/testthat/test-meetup_auth.R +++ b/tests/testthat/test-meetup_auth.R @@ -25,7 +25,13 @@ withr::local_options( "meetupr.consumer_secret" = regdata$client_secret[[1]] ) ) -withr::local_envvar(list("MEETUP_AUTH_URL" = meetup_app$url())) +withr::local_envvar( + list( + "MEETUP_AUTH_URL" = meetup_app$url(), + "MEETUP_TESTTHAT" = TRUE + ) + ) + td <- withr::local_tempdir() token <- meetup_auth( new_user = TRUE, From d75a1fc4e6da57b35576499d81332e4f1cc3404d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:07:18 +0100 Subject: [PATCH 06/12] copy paste --- R/meetup_auth.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index a9bb93c2..55ad1a9e 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -287,7 +287,7 @@ meetup_deauth <- function(clear_cache = TRUE, if (token_available(verbose = FALSE)) { if (verbose) { - message("Removing google token stashed internally in 'meetupr'.") + message("Removing Meetup token stashed internally in 'meetupr'.") } rm("token", envir = .state) } else { From 6b6544cc29956a935da9a624184ab722230e3ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:09:39 +0100 Subject: [PATCH 07/12] what actually happens --- R/meetup_auth.R | 3 +-- man/meetup-auth.Rd | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 55ad1a9e..69f0d3f8 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -257,8 +257,7 @@ token_available <- function(verbose = TRUE) { #' APIs on behalf of the authenticated user. #' #' @param clear_cache logical indicating whether to disable the -#' \code{.httr-oauth} file in working directory, if such exists, by renaming -#' to \code{.httr-oauth-SUSPENDED} +#' token file, if such exists, by apprending "-SUSPENDED" to its name #' @template verbose #' @export #' @rdname meetup-auth diff --git a/man/meetup-auth.Rd b/man/meetup-auth.Rd index e63beaa0..1fc8e7ca 100644 --- a/man/meetup-auth.Rd +++ b/man/meetup-auth.Rd @@ -53,8 +53,7 @@ If \code{cache} is \code{FALSE} this is ignored.} this is ignored.} \item{clear_cache}{logical indicating whether to disable the -\code{.httr-oauth} file in working directory, if such exists, by renaming -to \code{.httr-oauth-SUSPENDED}} +token file, if such exists, by apprending "-SUSPENDED" to its name} } \value{ a \code{request} object (an S3 class provided by \code{httr}) From 6dbabe41b5c7748d316a87756d8714f952a73d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:10:40 +0100 Subject: [PATCH 08/12] fix --- R/meetup_auth.R | 3 +-- man/meetup-auth.Rd | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 69f0d3f8..c7ab0a2a 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -8,8 +8,7 @@ #' Authorize \code{meetupr} via the OAuth API. You will be directed to a web #' browser, asked to sign in to your Meetup account, and to grant \code{meetupr} #' permission to operate on your behalf. By default, these user credentials are -#' saved to a file in your home directory whose path is saved in `.Renviron` -#' as `MEETUPR_PAT`. +#' saved to an app dir as determined by `rappdirs::user_data_dir("meetupr", "meetupr")`. #' If you set `use_appdir` to `FALSE` but `cache` to `TRUE`, #' they are cached in a file named \code{.httr-oauth} in the current working directory. #' To control where the file is saved, use `token_path`. diff --git a/man/meetup-auth.Rd b/man/meetup-auth.Rd index 1fc8e7ca..b6f82c0d 100644 --- a/man/meetup-auth.Rd +++ b/man/meetup-auth.Rd @@ -64,8 +64,7 @@ Either NULL or the path in which the token is saved. Authorize \code{meetupr} via the OAuth API. You will be directed to a web browser, asked to sign in to your Meetup account, and to grant \code{meetupr} permission to operate on your behalf. By default, these user credentials are -saved to a file in your home directory whose path is saved in \code{.Renviron} -as \code{MEETUPR_PAT}. +saved to an app dir as determined by \code{rappdirs::user_data_dir("meetupr", "meetupr")}. If you set \code{use_appdir} to \code{FALSE} but \code{cache} to \code{TRUE}, they are cached in a file named \code{.httr-oauth} in the current working directory. To control where the file is saved, use \code{token_path}. From f88065c859dade2df10510216d619ac7aff53ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:12:36 +0100 Subject: [PATCH 09/12] fix --- R/meetup_auth.R | 3 +-- man/meetup-auth.Rd | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index c7ab0a2a..7b887e4e 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -65,8 +65,7 @@ #' stored as an \code{.rds} file. #' @param new_user logical, defaults to \code{FALSE}. Set to \code{TRUE} if you #' want to wipe the slate clean and re-authenticate with the same or different -#' Meetup account. This disables the \code{.httr-oauth} file in current -#' working directory. +#' Meetup account. This disables the current token that's at `meetup_token_path()`. #' @param key,secret the "Client ID" and "Client secret" for the application; #' defaults to the ID and secret built into the \code{meetupr} package #' @param cache logical indicating if \code{meetupr} should cache diff --git a/man/meetup-auth.Rd b/man/meetup-auth.Rd index b6f82c0d..c64e640e 100644 --- a/man/meetup-auth.Rd +++ b/man/meetup-auth.Rd @@ -33,8 +33,7 @@ stored as an \code{.rds} file.} \item{new_user}{logical, defaults to \code{FALSE}. Set to \code{TRUE} if you want to wipe the slate clean and re-authenticate with the same or different -Meetup account. This disables the \code{.httr-oauth} file in current -working directory.} +Meetup account. This disables the current token that's at \code{meetup_token_path()}.} \item{key, secret}{the "Client ID" and "Client secret" for the application; defaults to the ID and secret built into the \code{meetupr} package} From 8c40befed37297d09f981a203c95fbb7c2b87c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:19:00 +0100 Subject: [PATCH 10/12] clearer --- R/meetup_auth.R | 2 +- man/meetup-auth.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 7b887e4e..233cf167 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -69,7 +69,7 @@ #' @param key,secret the "Client ID" and "Client secret" for the application; #' defaults to the ID and secret built into the \code{meetupr} package #' @param cache logical indicating if \code{meetupr} should cache -#' credentials in the default cache file \code{.httr-oauth} or `token_path`. +#' credentials a file. #' @param use_appdir Logical indicating whether to save the created token #' in app dir as determined by `rappdirs::user_data_dir("meetupr", "meetupr")`. #' If \code{cache} is `FALSE` this is ignored. diff --git a/man/meetup-auth.Rd b/man/meetup-auth.Rd index c64e640e..83b83737 100644 --- a/man/meetup-auth.Rd +++ b/man/meetup-auth.Rd @@ -39,7 +39,7 @@ Meetup account. This disables the current token that's at \code{meetup_token_pat defaults to the ID and secret built into the \code{meetupr} package} \item{cache}{logical indicating if \code{meetupr} should cache -credentials in the default cache file \code{.httr-oauth} or \code{token_path}.} +credentials a file.} \item{verbose}{logical; do you want informative messages? \code{TRUE} by default in interactive sessions. Can be toggled by the \code{meetupr.verbose} option.} From e497d1439a12a8a95aa30745229a38cf435b0a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:20:23 +0100 Subject: [PATCH 11/12] more testing --- tests/testthat/test-meetup_auth.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-meetup_auth.R b/tests/testthat/test-meetup_auth.R index 4ffd69c4..16397ddc 100644 --- a/tests/testthat/test-meetup_auth.R +++ b/tests/testthat/test-meetup_auth.R @@ -39,5 +39,7 @@ token <- meetup_auth( use_appdir = FALSE) expect_s3_class(token, "Token2.0") + expect_true(file.exists(file.path(td, "token.rds"))) + expect_s3_class(readRDS(file.path(td, "token.rds"))[[1]], "Token2.0") }) From 36f563c741b59c805c9ef56ce59e96e1ee0fbb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Sat, 13 Feb 2021 15:35:59 +0100 Subject: [PATCH 12/12] this is what is documented --- R/meetup_auth.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/R/meetup_auth.R b/R/meetup_auth.R index 233cf167..55ec1efc 100644 --- a/R/meetup_auth.R +++ b/R/meetup_auth.R @@ -109,7 +109,7 @@ meetup_auth <- function(token = meetup_token_path(), if (is.null(token)) { - meetup_app <- httr::oauth_app( + meetup_app <- httr::oauth_app( "meetup", key = key, secret = secret, @@ -132,17 +132,15 @@ meetup_auth <- function(token = meetup_token_path(), if (cache) { if (use_appdir) { - if (is.null(token_path)) { - token_path <- appdir_path() - # from https://github.com/r-hub/rhub/blob/5c339d7b95d75172beec85603ee197c2502903b1/R/email.R#L192 - parent <- dirname(token_path) - if (!file.exists(parent)) dir.create(parent, recursive = TRUE) - } + token_path <- appdir_path() + # from https://github.com/r-hub/rhub/blob/5c339d7b95d75172beec85603ee197c2502903b1/R/email.R#L192 + parent <- dirname(token_path) + if (!file.exists(parent)) dir.create(parent, recursive = TRUE) } # In all cases if cache is TRUE we want to set it to the filepath - if (!is.null(token_path)) { + if (!is.null(token_path) && !use_appdir) { cache <- token_path } }