From b45b4a8afe7c276c16b1a4f7be117b50ac19480e Mon Sep 17 00:00:00 2001 From: Ruben Perez Perez Date: Fri, 11 Oct 2024 21:44:10 +0200 Subject: [PATCH] add function for google authentication automation --- .gitignore | 1 + DESCRIPTION | 4 +++- NAMESPACE | 3 +++ R/google_auth.R | 51 ++++++++++++++++++++++++++++++++++++++++++++++ R/prep_data.R | 3 ++- man/google_auth.Rd | 30 +++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 R/google_auth.R create mode 100644 man/google_auth.Rd diff --git a/.gitignore b/.gitignore index 2e4972c..30b16df 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /data-RB /divewatchrshiny .secrets +.config diff --git a/DESCRIPTION b/DESCRIPTION index 936a4be..80cde9d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,7 +24,9 @@ Imports: dplyr, forcats, tidyr, htmltools, - ggExtra + ggExtra, + gargle, + base64enc Depends: R (>= 2.10) Suggests: diff --git a/NAMESPACE b/NAMESPACE index f074992..5db892f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,12 +7,15 @@ export(divetypes) export(divetypes_platform) export(divewatch) export(example_data) +export(google_auth) export(logbook_highlights) export(logbook_map) export(logged_depths) export(prep_data) +import(base64enc) import(dplyr) import(forcats) +import(gargle) import(ggplot2) import(googlesheets4) import(htmltools) diff --git a/R/google_auth.R b/R/google_auth.R new file mode 100644 index 0000000..879afa8 --- /dev/null +++ b/R/google_auth.R @@ -0,0 +1,51 @@ +#' Uses the edit rights to a Google sheets if they are stored by the user +#' +#' Stores the authentication token in a file and uses it to access the Google sheet +#' +#' This function allows Github Action and a users that has a token stored to access a Google sheet to +#' retrieve the data from a given google sheet +#' +#' @param email email account that will be used to access the Logbook in Google Spreadsheet +#' +#' @author Ruben Perez Perez +#' +#' @import gargle +#' @import googlesheets4 +#' @import base64enc +#' +#' @return It provides access to a given Google sheet if there is a token stored. +#' +#' @export +#' @examples +#' divewatch() +#' \dontrun{ +#' google_auth() +#' } + +google_auth <- function(email = NA) { + + +# Path to save the decoded OAuth token +oauth_token_path <- ".config/gcloud/google-oauth-token.rds" + +# Check if we are running in GitHub Actions by looking for the environment variable +if (Sys.getenv("GITHUB_ACTIONS") == "true") { + # Retrieve the Base64-encoded OAuth token from the environment (GitHub Secrets) + base64_token <- Sys.getenv("GOOGLE_OAUTH_TOKEN_BASE64") + + # Decode the Base64 string and write it to a binary RDS file + decoded_token <- base64decode(base64_token) + writeBin(decoded_token, oauth_token_path) +} + +# Load the OAuth token from the RDS file and authenticate +if (file.exists(oauth_token_path)) { + token <- readRDS(oauth_token_path) + gs4_auth(token = token) +} else { + print("OAuth token not found in directory.") + gs4_auth(email, cache = ".secrets") +} + +return() +} diff --git a/R/prep_data.R b/R/prep_data.R index da56b57..5fc705f 100644 --- a/R/prep_data.R +++ b/R/prep_data.R @@ -38,7 +38,8 @@ prep_data <- function (data = NA, if(!is.na(data)){ # From Google sheets -gs4_auth(email, cache = ".secrets") +# gs4_auth(email, cache = ".secrets") +google_auth() (scuba_log <- read_sheet(data, sheet = "logbook", na = "")) (scuba_geo <- read_sheet(data, sheet = "coordinates", na = "")) (scuba_typ <- read_sheet(data, sheet = "divetypes", na = "")) diff --git a/man/google_auth.Rd b/man/google_auth.Rd new file mode 100644 index 0000000..f263b29 --- /dev/null +++ b/man/google_auth.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/google_auth.R +\name{google_auth} +\alias{google_auth} +\title{Uses the edit rights to a Google sheets if they are stored by the user} +\usage{ +google_auth(email = NA) +} +\arguments{ +\item{email}{email account that will be used to access the Logbook in Google Spreadsheet} +} +\value{ +It provides access to a given Google sheet if there is a token stored. +} +\description{ +Stores the authentication token in a file and uses it to access the Google sheet +} +\details{ +This function allows Github Action and a users that has a token stored to access a Google sheet to +retrieve the data from a given google sheet +} +\examples{ +divewatch() +\dontrun{ +google_auth() +} +} +\author{ +Ruben Perez Perez +}