Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use shinymanager with passwords encrypted and saved in a PostgreSQL database, you need to follow these steps:

  1. Create a PostgreSQL database to store the encrypted user credentials.
  2. Install the shinymanager and RPostgreSQL packages in R.
  3. Encrypt the passwords using the bcrypt package in R.
  4. Connect to the PostgreSQL database using the RPostgreSQL package.
  5. Create a table in the PostgreSQL database to store the encrypted user credentials.
  6. Insert the encrypted user credentials into the PostgreSQL database.
  7. Use the shinymanager package to authenticate users by querying the user credentials from the PostgreSQL database.
  8. Use the bcrypt package to compare the input password with the encrypted password in the PostgreSQL database.

Here is an example code to illustrate how to do this:

```{r} library(shiny) library(shinymanager) library(RPostgreSQL) library(bcrypt)

Define a salt for password encryption

salt <- "$2a$12$WYjCTiJBnIDomRyf78QySO"

Encrypt passwords

pass1 <- bcrypt("password1", salt = salt) pass2 <- bcrypt("password2", salt = salt) pass3 <- bcrypt("password3", salt = salt)

Connect to the PostgreSQL database

con <- dbConnect(PostgreSQL(), host = "localhost", user = "username", password = "password", dbname = "database")

Create a table to store the encrypted user credentials

dbSendQuery(con, "CREATE TABLE users (username char(32), password char(60))")

Insert the encrypted user credentials into the PostgreSQL database

dbSendQuery(con, paste0("INSERT INTO users VALUES ('user1', '", pass1, "');")) dbSendQuery(con, paste0("INSERT INTO users VALUES ('user2', '", pass2, "');")) dbSendQuery(con, paste0("INSERT INTO users VALUES ('user3', '", pass3, "');"))

Define the authentication function

auth <- function(username, password) { # Get the encrypted password from the PostgreSQL database query <- paste0("SELECT password FROM users WHERE username='", username, "'") result <- dbGetQuery(con, query) encrypted_password <- result[[1]]

# Compare the input password with the encrypted password if (!is.null(encryptedpassword) && bcrypt::bcryptverify(password, encrypted_password)) { return(TRUE) } else { return(FALSE) } }

ui <- fluidPage( headerPanel("Protected page"), mainPanel( verbatimTextOutput("auth_result") ) )

server <- function(input, output, session) {

# Call the shinymanager authentication function resauth <- secureapp(ui, server, authentication = shinymanager::authenticator(auth))

# Print the authentication result output$authresult <- renderText({ if (resauth == TRUE) { "Authentication successful." } else { "Authentication failed." } }) }

shinyApp(ui, server) ```

In this code, the passwords are encrypted using the bcrypt package with a fixed salt. The encrypted passwords are then inserted into a PostgreSQL table called "users". The auth function queries the "users" table to retrieve the encrypted password for the given username, and then compares the input password with the encrypted password using the bcrypt::bcryptverify function. The shinymanager::authenticator function is used to authenticate users based on the auth function. Finally, the secureapp function is used to specify that authentication is required to access the UI.