Skip to content

Commit

Permalink
Adds vignette for Google Auth with Shiny
Browse files Browse the repository at this point in the history
  • Loading branch information
andyquinterom committed Jan 2, 2024
1 parent e1b656e commit ddb107d
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^.github$
^example$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ rsconnect/

example/.Renviron
docs
inst/doc
5 changes: 5 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Description: The ultimate R package for swift and seamless Single Sign-On (SSO)
like Google and Microsoft, tapLock streamlines authentication, enhancing both
user experience and application security. Elevate your R-based projects with
tapLock for a simplified, unified, and secure authentication process.
URL: https://github.com/ixpantia/tapLock
BugReports: https://github.com/ixpantia/tapLock/issues
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand All @@ -27,4 +29,7 @@ Imports:
future,
stats
Suggests:
knitr,
rmarkdown,
shiny
VignetteBuilder: knitr
1 change: 0 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
url: ~
template:
bootstrap: 5

2 changes: 2 additions & 0 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.html
*.R
108 changes: 108 additions & 0 deletions vignettes/en_securing_shiny_google.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: "(English) Securing Shiny with Google Auth"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{(English) Securing Shiny with Google Auth}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

## Introduction

This vignette provides a step-by-step tutorial on how to secure a Shiny application using Google authentication through the tapLock R package. tapLock simplifies the integration of OpenID Connect and OAuth 2.0 into Shiny applications, ensuring robust security with minimal coding effort.

## Prerequisites

Before proceeding, ensure you have the following:
- A basic understanding of R and Shiny.
- A Shiny application ready for deployment.
- Access to Google Developer Console for OAuth credentials.
- (Optional) A server with HTTPS enabled.

## Step 1: Install tapLock

Install tapLock from GitHub using the `pak` package:

```r
pak::pak("ixpantia/taplock")
```

## Step 2: Create Google OAuth Credentials

1. Go to the [Google Developer Console](https://console.developers.google.com/).
2. Create a new project or select an existing one.
3. Navigate to 'Credentials' and create 'OAuth client ID' credentials.
4. Set the **`Authorized JavaScript origins`** to your Shiny application URL.
5. Set the **`Authorized redirect URIs`** to your Shiny application URL with
the suffix `/login`.
6. Note down the `client_id` and `client_secret`.

## Step 3: Configure Authentication in R

Load tapLock and set up the authentication configuration:

```r
library(taplock)

auth_config <- new_openid_config(
provider = "google",
client_id = Sys.getenv("GOOGLE_CLIENT_ID"),
client_secret = Sys.getenv("GOOGLE_CLIENT_SECRET"),
app_url = Sys.getenv("SHINY_APP_URL")
)
```

Replace `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, and `SHINY_APP_URL` with your actual credentials and application URL in your environment variables.

## Step 4: Modify Shiny Application

Modify your Shiny app to use `sso_shiny_app`:

```r
library(shiny)
library(tapLock)

# Authentication configuration
auth_config <- new_openid_config(
provider = "google",
client_id = Sys.getenv("GOOGLE_CLIENT_ID"),
client_secret = Sys.getenv("GOOGLE_CLIENT_SECRET"),
app_url = Sys.getenv("SHINY_APP_URL")
)

# UI
ui <- fluidPage(
tags$h1("Welcome to the Secure Shiny App"),
textOutput("userInfo")
)

# Server
server <- function(input, output, session) {
output$userInfo <- renderText({
user_email <- get_token_field(token(), "email")
glue::glue("Logged in as: {user_email}")
})
}

# Secure Shiny app with tapLock
sso_shiny_app(auth_config, ui, server)
```

## Step 5: Deploy the Application

Deploy your Shiny application as you normally would. The tapLock package handles the authentication process.
We recommend deploying your application with a solution like Shiny Server
(Open Source or Pro) or with [Faucet](https://github.com/andyquinterom/faucet).
Solutions like Posit Connect already include authentication and do not require
tapLock.

## Conclusion

By following these steps, you have successfully secured your Shiny application with Google authentication using tapLock. This ensures that only authenticated users can access your application, enhancing its security and privacy.
108 changes: 108 additions & 0 deletions vignettes/es_asegura_shiny_con_google.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: "(Español) Asegurando Shiny con Google Auth"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{(Español) Asegurando Shiny con Google Auth}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

## Introducción

Esta viñeta proporciona un tutorial paso a paso sobre cómo asegurar una aplicación Shiny utilizando la autenticación de Google a través del paquete R tapLock. tapLock simplifica la integración de OpenID Connect y OAuth 2.0 en aplicaciones Shiny, asegurando una robusta seguridad con un esfuerzo mínimo de codificación.

## Prerrequisitos

Antes de proceder, asegúrate de tener lo siguiente:
- Un conocimiento básico de R y Shiny.
- Una aplicación Shiny lista para ser desplegada.
- Acceso a Google Developer Console para las credenciales OAuth.
- (Opcional) Un servidor con HTTPS habilitado.

## Paso 1: Instalar tapLock

Instala tapLock desde GitHub usando el paquete `pak`:

```r
pak::pak("ixpantia/taplock")
```

## Paso 2: Crear Credenciales OAuth de Google

1. Ve a [Google Developer Console](https://console.developers.google.com/).
2. Crea un nuevo proyecto o selecciona uno existente.
3. Navega a 'Credenciales' y crea credenciales 'OAuth client ID'.
4. Establece los **`Authorized JavaScript origins`** en la URL de tu aplicación Shiny.
5. Establece los **`Authorized redirect URIs`** en la URL de tu aplicación Shiny con
el sufijo `/login`.
6. Anota el `client_id` y el `client_secret`.

## Paso 3: Configurar Autenticación en R

Carga tapLock y configura la autenticación:

```r
library(taplock)

auth_config <- new_openid_config(
provider = "google",
client_id = Sys.getenv("GOOGLE_CLIENT_ID"),
client_secret = Sys.getenv("GOOGLE_CLIENT_SECRET"),
app_url = Sys.getenv("SHINY_APP_URL")
)
```

Reemplaza `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, y `SHINY_APP_URL` con tus credenciales reales y la URL de tu aplicación en tus variables de entorno.

## Paso 4: Modificar Aplicación Shiny

Modifica tu aplicación Shiny para usar `sso_shiny_app`:

```r
library(shiny)
library(tapLock)

# Configuración de autenticación
auth_config <- new_openid_config(
provider = "google",
client_id = Sys.getenv("GOOGLE_CLIENT_ID"),
client_secret = Sys.getenv("GOOGLE_CLIENT_SECRET"),
app_url = Sys.getenv("SHINY_APP_URL")
)

# UI
ui <- fluidPage(
tags$h1("Bienvenido a la Aplicación Shiny Segura"),
textOutput("userInfo")
)

# Server
server <- function(input, output, session) {
output$userInfo <- renderText({
user_email <- get_token_field(token(), "email")
glue::glue("Conectado como: {user_email}")
})
}

# Asegurar aplicación Shiny con tapLock
sso_shiny_app(auth_config, ui, server)
```

## Paso 5: Desplegar la Aplicación

Despliega tu aplicación Shiny como lo harías normalmente. El paquete tapLock maneja el proceso de autenticación.
Recomendamos desplegar tu aplicación con una solución como Shiny Server
(Open Source o Pro) o con [Faucet](https://github.com/andyquinterom/faucet).
Soluciones como Posit Connect ya incluyen autenticación y no requieren
tapLock.

## Conclusión

Siguiendo estos pasos, has asegurado con éxito tu aplicación Shiny con autenticación de Google utilizando tapLock. Esto asegura que solo los usuarios autenticados puedan acceder a tu aplicación, mejorando su seguridad y privacidad.

0 comments on commit ddb107d

Please sign in to comment.