Ask Your Question
3

How can shinymanager be used with passwords encrypted and saved in a postgresql database?

asked 2023-05-01 07:24:52 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-01 07:43:01 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-01 07:24:52 +0000

Seen: 18 times

Last updated: May 01 '23