Skip to content

Commit

Permalink
Update a few auth inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cansavvy committed Nov 16, 2023
1 parent 58f2472 commit ce082ae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
15 changes: 8 additions & 7 deletions R/auth.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Authorize R package to access endpoints
#' @description This is a function to authorize the R package to access APIs interactively.
#' @param app_name app would you like to authorize? Supported apps are 'google' 'calendly' and 'github'
#' @param token an output from \code{\link{oauth2.0_token}} to set as the authentication token.
#' @param cache Should the token be cached as an .httr-oauth file or API keys stored as global options?
#' @param ... additional arguments to send to \code{\link{oauth2.0_token}}
#' @return OAuth token saved to the environment so the package can use the users' Google data
Expand All @@ -13,7 +12,6 @@
#' authorize()
#' }
authorize <- function(app_name = NULL,
token = NULL,
cache = FALSE,
...) {
if (is.null(app_name)) {
Expand Down Expand Up @@ -112,7 +110,7 @@ delete_creds <- function(app_name = "all") {
#' Use secrets to Authorize R package to access endpoints
#' @description This is a function to authorize metricminer to access calendly, github or google noninteractively from passing in a keys or tokens.
#' @param app_name Which app are you trying to authorize? 'google', 'calendly' or 'github'?
#' @param token For calendly or github, pass in the API key that you have set up from going to https://github.com/settings/tokens/new or https://calendly.com/integrations/api_webhooks respectively.
#' @param token For calendly or github, pass in the API key or Personal Access Token that you have set up from going to https://github.com/settings/tokens/new or https://calendly.com/integrations/api_webhooks respectively.
#' @param cache Should the credentials be cached? TRUE or FALSE?
#' @param access_token For Google, access token can be obtained from running authorize interactively: token <-authorize(); token$credentials$access_token
#' @param refresh_token For Google, refresh token can be obtained from running authorize interactively: token <-authorize(); token$credentials$refresh_token
Expand Down Expand Up @@ -140,10 +138,15 @@ delete_creds <- function(app_name = "all") {
#' }
#'
auth_from_secret <- function(app_name, token, access_token, refresh_token, cache = FALSE) {
if (app_name %in% c("github", "calendly") && is.null(token)) stop("For GitHub and Calendly, token cannot be NULL")

if (app_name %in% c("github", "calendly") && is.null(token)) {
stop("For GitHub and Calendly, token cannot be NULL")
}

if (app_name == "google") {
if (is.null(access_token) || is.null(refresh_token)) stop("For Google auth, need access_token and refresh_token cannot be NULL")
if (is.null(access_token) || is.null(refresh_token)) {
stop("For Google auth, need access_token and refresh_token cannot be NULL")
}

credentials <- list(
access_token = access_token,
Expand All @@ -165,12 +168,10 @@ auth_from_secret <- function(app_name, token, access_token, refresh_token, cache
# If they chose to cache it, we'll store it in the .Rprofile
if (app_name == "calendly" && cache) {
options(calendly = token)
token <- token
}

if (app_name == "github" && cache) {
options(github_api = token)
token <- token
}

if (cache) message("You chose to cache your credentials, if you change your mind, run metricminer::delete_creds(). \n Be careful not to push .httr-oauth or .Rprofile files to GitHub or share it anywhere.")
Expand Down
2 changes: 1 addition & 1 deletion man/auth_from_secret.Rd

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

4 changes: 1 addition & 3 deletions man/authorize.Rd

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

0 comments on commit ce082ae

Please sign in to comment.