Skip to content

Commit

Permalink
add function for google authentication automation
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpp7 committed Oct 11, 2024
1 parent d225742 commit b45b4a8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/data-RB
/divewatchrshiny
.secrets
.config
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Imports: dplyr,
forcats,
tidyr,
htmltools,
ggExtra
ggExtra,
gargle,
base64enc
Depends:
R (>= 2.10)
Suggests:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions R/google_auth.R
Original file line number Diff line number Diff line change
@@ -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()
}
3 changes: 2 additions & 1 deletion R/prep_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""))
Expand Down
30 changes: 30 additions & 0 deletions man/google_auth.Rd

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

0 comments on commit b45b4a8

Please sign in to comment.