### Install shinymanager Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Installation instructions for CRAN and development versions. ```r install.packages("shinymanager") ``` ```r remotes::install_github("datastorm-open/shinymanager") ``` -------------------------------- ### Password module implementation example Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-password.html A complete example showing how to trigger and handle the password change module in a Shiny application. ```R if (interactive()) { library(shiny) library(shinymanager) ui <- fluidPage( tags$h2("Change password module"), actionButton( inputId = "ask", label = "Ask to change password" ), verbatimTextOutput(outputId = "res_pwd") ) server <- function(input, output, session) { observeEvent(input$ask, { insertUI( selector = "body", ui = tags$div( id = "module-pwd", pwd_ui(id = "pwd") ) ) }) output$res_pwd <- renderPrint({ reactiveValuesToList(pwd_out) }) pwd_out <- callModule( module = pwd_server, id = "pwd", user = reactiveValues(user = "me"), update_pwd = function(user, pwd) { # store the password somewhere list(result = TRUE) } ) observeEvent(pwd_out$relog, { removeUI(selector = "#module-pwd") }) } shinyApp(ui, server) } ``` -------------------------------- ### Initialize Authentication Module Source: https://github.com/datastorm-open/shinymanager/blob/master/man/module-authentication.Rd Basic setup for the authentication module within an interactive Shiny session. ```R if (interactive()) { library(shiny) library(shinymanager) ``` -------------------------------- ### ShinyManager Authentication Setup Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/article/article.html Basic implementation of the secure authentication UI and server logic in a Shiny application. ```R library(shiny) library(shinymanager) ui <- secure_app( ui = fluidPage( "Hello, secure world!" ) ) server <- function(input, output, session) { res_auth <- secure_server( check_credentials = check_credentials( data.frame( user = "admin", password = "password", stringsAsFactors = FALSE ) ) ) } shinyApp(ui, server) ``` -------------------------------- ### Configure inputs for user management Source: https://github.com/datastorm-open/shinymanager/blob/master/man/secure-app.Rd Example of defining a custom input configuration for the admin console using a named list. ```R list(group = list( fun = "selectInput", args = list( choices = c("all", "restricted"), multiple = TRUE, selected = c("all", "restricted") ) ) ) ``` -------------------------------- ### Implement Authentication in Shiny Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/article/article.html Basic setup for securing a Shiny application with authentication. ```R library(shiny) library(shinymanager) # Define credentials credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("shiny", "12345"), stringsAsFactors = FALSE ) ui <- secure_app(fluidPage( "Authenticated content" )) server <- function(input, output, session) { res_auth <- secure_server(check_credentials = check_credentials(credentials)) } shinyApp(ui, server) ``` -------------------------------- ### Validate credentials using external databases Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/check_credentials.html Examples of initializing credential checks using SQLite files or SQL configuration files. ```R ## With a SQLite database: check_credentials("credentials.sqlite", passphrase = "supersecret") ## With a SQL database: check_credentials("config_db.yml") ``` -------------------------------- ### Implement a FAB button in a Shiny application Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/fab_button.html A complete example demonstrating how to integrate fab_button with actionButtons and capture their inputs in a Shiny server function. ```R library(shiny) library(shinymanager) ui <- fluidPage( tags$h1("FAB button"), tags$p("FAB button:"), verbatimTextOutput(outputId = "res_fab"), tags$p("Logout button:"), verbatimTextOutput(outputId = "res_logout"), tags$p("Info button:"), verbatimTextOutput(outputId = "res_info"), fab_button( actionButton( inputId = "logout", label = "Logout", icon = icon("arrow-right-from-bracket") ), actionButton( inputId = "info", label = "Information", icon = icon("info") ), inputId = "fab" ) ) server <- function(input, output, session) { output$res_fab <- renderPrint({ input$fab }) output$res_logout <- renderPrint({ input$logout }) output$res_info <- renderPrint({ input$info }) } if (interactive()) { shinyApp(ui, server) } ``` -------------------------------- ### create_db(credentials_data, sqlite_path, passphrase = NULL, flags = RSQLite::SQLITE_RWC) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/create_db.html Creates a SQLite database with credentials data. The credentials data frame must contain 'user' and 'password' columns, and can optionally include 'admin', 'start', 'expire', and 'applications' columns. ```APIDOC ## create_db ### Description Creates a SQLite database with credentials data protected by an optional password. ### Signature `create_db(credentials_data, sqlite_path, passphrase = NULL, flags = RSQLite::SQLITE_RWC)` ### Parameters - **credentials_data** (data.frame) - Required - A data.frame containing user information. Required columns: 'user', 'password'. Optional columns: 'admin', 'start', 'expire', 'applications'. - **sqlite_path** (string) - Required - The file path where the SQLite database will be created. - **passphrase** (string) - Optional - A password used to encrypt/protect the data inside the database. - **flags** (integer) - Optional - RSQLite flags for database connection (default: RSQLite::SQLITE_RWC). ### Example ```r credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), stringsAsFactors = FALSE ) create_db( credentials_data = credentials, sqlite_path = "/path/to/database.sqlite", passphrase = "secret" ) ``` ``` -------------------------------- ### Manage encrypted user credentials Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/db-crypted.html Example of updating user and password management tables in a persistent SQLite database. ```R # add some users to database if (FALSE) { conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = "path/to/database.sqlite") # update "credentials" table current_user <- read_db_decrypt( conn, name = "credentials", passphrase = key_get("R-shinymanager-key", "obiwankenobi") ) add_user <- data.frame(user = "new", password = "pwdToChange", start = NA, expire = NA, admin = TRUE) new_users <- rbind.data.frame(current_user, add_user) write_db_encrypt( conn, value = new_users, name = "credentials", key_get("R-shinymanager-key", "obiwankenobi") ) # update "pwd_mngt" table pwd_mngt <- read_db_decrypt( conn, name = "pwd_mngt", passphrase = key_get("R-shinymanager-key", "obiwankenobi") ) pwd_mngt <- rbind.data.frame( pwd_mngt, data.frame(user = "new", must_change = T, have_changed = F, date_change = "") ) write_db_encrypt( conn, value = pwd_mngt, name = "pwd_mngt", passphrase = key_get("R-shinymanager-key", "obiwankenobi") ) } DBI::dbDisconnect(conn) ``` -------------------------------- ### Customize shinymanager labels Source: https://github.com/datastorm-open/shinymanager/blob/master/man/custom-labels.Rd Examples of overriding default authentication labels using named arguments or a named list. ```R # In global.R for example: set_labels( language = "en", "Please authenticate" = "You have to login", "Username:" = "What's your name:", "Password:" = "Enter your password:" ) ``` ```R # same with named list set_labels( language = "en", list( "Please authenticate" = "You have to login", "Username:" = "What's your name:", "Password:" = "Enter your password:" ) ) ``` -------------------------------- ### Initialize and use SQL credentials database Source: https://github.com/datastorm-open/shinymanager/blob/master/man/create_sql_db.Rd Demonstrates how to define credentials, initialize the SQL database via a configuration file, and integrate authentication into a Shiny application. ```R library(shiny) library(shinymanager) #### init the SQL Database # first edit the .yml configuration file system.file("sql_config/pg_template.yml", package = "shinymanager") # Init Credentials data credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), # password will automatically be hashed stringsAsFactors = FALSE ) # Create SQL database create_sql_db( credentials_data = credentials, config_path = "path/to/your_sql_configuration.yml" ) ### Use in shiny ui <- fluidPage( tags$h2("My secure application"), verbatimTextOutput("auth_output") ) # Wrap your UI with secure_app ui <- secure_app(ui, choose_language = TRUE) server <- function(input, output, session) { # call the server part # check_credentials returns a function to authenticate users res_auth <- secure_server( check_credentials = check_credentials(db = "path/to/your_sql_configuration.yml") ) output$auth_output <- renderPrint({ reactiveValuesToList(res_auth) }) observe({ print(input$shinymanager_where) print(input$shinymanager_language) }) # your classic server logic } shinyApp(ui, server) ``` -------------------------------- ### create_sql_db(credentials_data, config_path) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/create_sql_db.html Initializes a SQL database with user credentials data based on a provided configuration file. ```APIDOC ## create_sql_db(credentials_data, config_path) ### Description Creates a SQL database (Postgres, MSSQL, MySQL, etc.) with credentials data using the DBI interface. ### Parameters - **credentials_data** (data.frame) - Required - A data frame containing user information. Must include 'user' and 'password' columns. Optional columns include 'admin', 'start', 'expire', 'applications', and any additional custom columns. - **config_path** (string) - Required - The file path to the YAML configuration file containing database connection details. ``` -------------------------------- ### Access Customization Documentation Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Access help files for customizing the authentication UI and labels. ```R ?secure_app ?auth_ui ?set_labels ``` -------------------------------- ### Initialize SQLite Database with Credentials Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/demo_app/www/script/init_db.md Defines a data frame of user credentials and creates a SQLite database file with a specified passphrase. ```r require(shinymanager) credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("shiny", "shinymanager"), # password will automatically be hashed admin = c(FALSE, TRUE), # utilisateurs avec droits d'admin ? stringsAsFactors = FALSE ) create_db( credentials_data = credentials, sqlite_path = "database.sqlite", # elle sera crée passphrase = "passphrase_wihtout_keyring" ) ``` -------------------------------- ### Access shinymanager Help Documentation Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Access help files for core shinymanager functions. ```R require(shinymanager) # Init and using sqlite database ?create_db # Init and using SQL database ?create_sql_db # shiny integration ?secure_app ?auth_ui # ui definition # change labels / language ?set_labels ``` -------------------------------- ### Function signature for create_db Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/create_db.html The standard signature for initializing a credentials database. ```R create_db( credentials_data, sqlite_path, passphrase = NULL, flags = RSQLite::SQLITE_RWC ) ``` -------------------------------- ### Implement Basic Authentication in Shiny Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Use a data frame to define user credentials and wrap the UI and server logic with secure_app and secure_server. ```r # define some basic credentials (on data.frame) credentials <- data.frame( user = c("shiny", "shinymanager"), # mandatory password = c("azerty", "12345"), # mandatory start = c("2019-04-15"), # optinal (all others) expire = c(NA, "2019-12-31"), admin = c(FALSE, TRUE), comment = "Simple and secure authentication mechanism for single ‘Shiny’ applications.", stringsAsFactors = FALSE ) library(shiny) library(shinymanager) ui <- fluidPage( tags$h2("My secure application"), verbatimTextOutput("auth_output") ) # Wrap your UI with secure_app ui <- secure_app(ui) server <- function(input, output, session) { # call the server part # check_credentials returns a function to authenticate users res_auth <- secure_server( check_credentials = check_credentials(credentials) ) output$auth_output <- renderPrint({ reactiveValuesToList(res_auth) }) # your classic server logic } shinyApp(ui, server) ``` -------------------------------- ### Initialize SQLite Database and Secure Shiny App Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/articles/SQLite_Admin.html Defines user credentials, sets up a secure database with a passphrase, and integrates the authentication layer into the Shiny UI and server. ```R # Credentials data credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), # password will automatically be hashed admin = c(FALSE, TRUE), stringsAsFactors = FALSE ) # you can use keyring package to set database key library(keyring) key_set("R-shinymanager-key", "obiwankenobi") # Init the database create_db( credentials_data = credentials, sqlite_path = "path/to/database.sqlite", # will be created passphrase = key_get("R-shinymanager-key", "obiwankenobi") # passphrase = "passphrase_wihtout_keyring" ) # Wrap your UI with secure_app, enabled admin mode or not ui <- secure_app(ui, enable_admin = TRUE) server <- function(input, output, session) { # check_credentials directly on sqlite db res_auth <- secure_server( check_credentials = check_credentials( "path/to/database.sqlite", passphrase = key_get("R-shinymanager-key", "obiwankenobi") # passphrase = "passphrase_wihtout_keyring" ) ) output$auth_output <- renderPrint({ reactiveValuesToList(res_auth) }) # your classic server logic ... } ``` -------------------------------- ### Implement secure_server in Shiny Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/demo_app/www/script/server.md Use this server function to initialize authentication with a SQLite database. Ensure the database file path and passphrase are correctly configured. ```r server <- function(input, output, session) { res_auth <- secure_server( check_credentials = check_credentials( "database.sqlite", passphrase = "passphrase_wihtout_keyring" ) ) # user info output$auth_output <- renderPrint({ reactiveValuesToList(res_auth) }) # shinymanager input output$shinymanager_language <- renderPrint({ input$shinymanager_language }) output$shinymanager_where <- renderPrint({ input$shinymanager_where }) } ``` -------------------------------- ### secure_app(ui, ..., enable_admin = FALSE, head_auth = NULL, theme = NULL, language = 'en', fab_position = 'bottom-right') Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/secure-app.html Wraps the UI of a Shiny application to include an authentication layer. ```APIDOC ## secure_app ### Description Wraps the UI of a Shiny application to include an authentication layer. ### Parameters - **ui** (UI) - Required - UI of the application. - **...** (Arguments) - Optional - Arguments passed to auth_ui. - **enable_admin** (logical) - Optional - Enable or not access to admin mode. - **head_auth** (Tag/list) - Optional - Tag or list of tags to use in the of the authentication page. - **theme** (string) - Optional - Alternative Bootstrap stylesheet. - **language** (string) - Optional - Language to use for labels. - **fab_position** (string) - Optional - Position for the FAB button. ``` -------------------------------- ### pwd_server(input, output, session, user, update_pwd, validate_pwd = NULL, use_token = FALSE, lan = NULL) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-password.html Server logic for the password change module. ```APIDOC ## pwd_server ### Description Handles the server-side logic for password validation and updating. ### Parameters - **input, output, session** (object) - Required - Standard Shiny server arguments. - **user** (reactiveValues) - Required - A reactiveValues object with a slot 'user' referring to the user. - **update_pwd** (function) - Required - A function to perform an action when changing password is successful. Receives 'user' and 'password' arguments. - **validate_pwd** (function) - Optional - A function to validate the password entered by the user. - **use_token** (boolean) - Optional - Add a token in the URL to check authentication (default: FALSE). ``` -------------------------------- ### Configure Authentication UI and Server Source: https://github.com/datastorm-open/shinymanager/blob/master/man/module-authentication.Rd Integrate the authentication module into the UI and server functions of a Shiny application. ```R ui <- fluidPage( # authentication module auth_ui( id = "auth", # add image on top ? tags_top = tags$div( tags$h4("Demo", style = "align:center"), tags$img( src = "https://www.r-project.org/logo/Rlogo.png", width = 100 ) ), # add information on bottom ? tags_bottom = tags$div( tags$p( "For any question, please contact ", tags$a( href = "mailto:someone@example.com?Subject=Shiny%20aManager", target="_top", "administrator" ) ) ), # change auth ui background ? # https://developer.mozilla.org/fr/docs/Web/CSS/background background = "linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)), url('https://www.r-project.org/logo/Rlogo.png');", # set language ? lan = use_language("fr") ), # result of authentication verbatimTextOutput(outputId = "res_auth"), # classic app headerPanel('Iris k-means clustering'), sidebarPanel( selectInput('xcol', 'X Variable', names(iris)), selectInput('ycol', 'Y Variable', names(iris), selected=names(iris)[[2]]), numericInput('clusters', 'Cluster count', 3, min = 1, max = 9) ), mainPanel( plotOutput('plot1') ) ) server <- function(input, output, session) { # authentication module auth <- callModule( module = auth_server, id = "auth", check_credentials = check_credentials(credentials) ) output$res_auth <- renderPrint({ reactiveValuesToList(auth) }) # classic app selectedData <- reactive({ req(auth$result) # <---- dependency on authentication result iris[, c(input$xcol, input$ycol)] }) clusters <- reactive({ kmeans(selectedData(), input$clusters) }) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) par(mar = c(5.1, 4.1, 0, 1)) plot(selectedData(), col = clusters()$cluster, pch = 20, cex = 3) points(clusters()$centers, pch = 4, cex = 4, lwd = 4) }) } shinyApp(ui, server) ``` -------------------------------- ### secure_server(check_credentials, timeout = 15, inputs_list = NULL, max_users = NULL, fileEncoding = '', keep_token = FALSE, validate_pwd = NULL, session = shiny::getDefaultReactiveDomain()) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/secure-app.html Wraps the server logic of a Shiny application to handle authentication credentials and session management. ```APIDOC ## secure_server ### Description Wraps the server logic of a Shiny application to handle authentication credentials and session management. ### Parameters - **check_credentials** (function) - Required - Function passed to auth_server. - **timeout** (numeric) - Optional - Timeout session (minutes) before logout if sleeping. - **inputs_list** (list) - Optional - Configuration for editing users information. - **max_users** (integer) - Optional - Maximum of users in sql credentials. - **fileEncoding** (character) - Optional - Encoding of logs downloaded file. - **keep_token** (logical) - Optional - Keep the token used to authenticate in the URL. - **validate_pwd** (function) - Optional - A function to validate the password entered by the user. - **session** (session) - Optional - Shiny session. ``` -------------------------------- ### Configure SQL Database Authentication Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Use the DBI interface to manage credentials in an external SQL database with a YAML configuration file. ```r library(shiny) library(shinymanager) #### init the SQL Database # first edit the .yml configuration file system.file("sql_config/pg_template.yml", package = "shinymanager") # Init Credentials data credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), # password will automatically be hashed stringsAsFactors = FALSE ) # Create SQL database create_sql_db( credentials_data = credentials, config_path = "path/to/your_sql_configuration.yml" ) ### Use in shiny ui <- fluidPage( tags$h2("My secure application"), verbatimTextOutput("auth_output") ) # Wrap your UI with secure_app ui <- secure_app(ui, choose_language = TRUE) server <- function(input, output, session) { # call the server part # check_credentials returns a function to authenticate users res_auth <- secure_server( check_credentials = check_credentials(db = "path/to/your_sql_configuration.yml") ) # your classic server logic } shinyApp(ui, server) ``` -------------------------------- ### Secure a Shiny UI with Custom Branding Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/demo_app/www/script/ui.md Wraps a standard navbarPage UI with authentication, adding custom logos, footer links, and background styling. ```r ui <- navbarPage( # your app ) secure_app(ui, enable_admin = TRUE, language = "fr", choose_language = TRUE, status = "warning", # top tags_top = tags$div( tags$img( src = "img/logoDS.png", width = 100 ) ), # bottom tags_bottom = tags$div( tags$p( a(href = "https://github.com/datastorm-open/shinymanager", target = "_blank", img(src = "img/github.png", class = "ribbon", style = "height: 60px") ), tags$p("user : shiny / pwd : shiny or shinymanager / shinymanager (admin)"), ), align = "center" ), # ui background ? # https://developer.mozilla.org/fr/docs/Web/CSS/background background = "center/20% url('figures/shinymanager.png');" ) ``` -------------------------------- ### Handle UI Lag with Global Observer Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Use an observer to gate application logic until the authentication state is confirmed, preventing errors from null inputs. ```R server <- function(input, output, session) { auth_out <- secure_server(....) observe({ if(is.null(input$shinymanager_where) || (!is.null(input$shinymanager_where) && input$shinymanager_where %in% "application")){ # your server app code } }) } ``` -------------------------------- ### UI and Utility Functions Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/index.html Helper functions for UI customization, labels, and password generation. ```APIDOC ## set_labels() / get_labels() ### Description Modify or retrieve shinymanager labels to use custom text. ## use_language() ### Description Apply specific language labels to the shinymanager interface. ## fab_button() ### Description Create a Floating Action Button (FAB) for the UI. ## generate_pwd() ### Description Generate a simple password. ``` -------------------------------- ### Encrypt and decrypt data in SQLite Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/db-crypted.html Demonstrates basic usage of encryption and decryption with an in-memory SQLite database. ```R # connect to database conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") # write to database write_db_encrypt(conn, value = head(iris), name = "iris", passphrase = "supersecret") # read read_db_decrypt(conn = conn, name = "iris", passphrase = "supersecret") #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa # with wrong passphrase if (FALSE) { read_db_decrypt(conn = conn, name = "iris", passphrase = "forgotten") } # with DBI method you'll get a crypted blob DBI::dbReadTable(conn = conn, name = "iris") #> value iv #> 1 blob[624 B] blob[16 B] ``` -------------------------------- ### auth_server Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-authentication.html Defines the server logic for the authentication module. ```APIDOC ## auth_server(input, output, session, check_credentials, use_token = FALSE, lan = NULL) ### Description Handles the server-side authentication logic, including credential verification. ### Parameters - **input, output, session** (object) - Required - Standard Shiny server arguments. - **check_credentials** (function) - Required - Function with two arguments (user, password) that returns a list with authentication results. - **use_token** (logical) - Optional - Add a token in the URL to check authentication (default: FALSE). - **lan** (object) - Optional - A language object. ### Value A reactiveValues object containing: - **result** (logical) - Result of authentication. - **user** (character) - Name of connected user. - **user_info** (list) - Information about the user. ``` -------------------------------- ### Define Credentials with Hashed Passwords Source: https://github.com/datastorm-open/shinymanager/blob/master/man/module-authentication.Rd Create a data frame of user credentials using scrypt for password hashing. ```R credentials <- data.frame( user = c("fanny", "victor"), password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")), is_hashed_password = TRUE, comment = c("alsace", "auvergne"), stringsAsFactors = FALSE ) ``` -------------------------------- ### check_credentials(db, passphrase = NULL) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/check_credentials.html Creates an authentication function that takes a username and password as input and returns a list containing authentication results and user information. ```APIDOC ## check_credentials(db, passphrase = NULL) ### Description Creates a function that validates user credentials. This function is intended to be used within the shinymanager authentication module. ### Parameters - **db** (data.frame, character) - Required - A data.frame containing credentials, a path to a SQLite database, or a path to a .yaml configuration for a SQL database. - **passphrase** (character) - Optional - The passphrase required to decrypt the SQLite database. ### Returns - **function** - A function that accepts two arguments: `user` and `password`. ### Authentication Result (Returned by the generated function) - **result** (logical) - Whether the authentication was successful. - **expired** (logical) - Whether the user account has expired. - **authorized** (logical) - Whether the user is authorized to access the application. - **user_info** (list) - The row from the credentials source corresponding to the user. ### Example ```r # Using a data.frame credentials <- data.frame( user = c("fanny", "victor"), password = c("azerty", "12345"), stringsAsFactors = FALSE ) # Validate a user auth_func <- check_credentials(credentials) auth_func("fanny", "azerty") ``` ``` -------------------------------- ### Authentication module functions Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-authentication.html Definitions for the UI and server components of the authentication module. ```R auth_ui( id, status = "primary", tags_top = NULL, tags_bottom = NULL, background = NULL, choose_language = NULL, lan = NULL, ... ) ``` ```R auth_server( input, output, session, check_credentials, use_token = FALSE, lan = NULL ) ``` -------------------------------- ### Implement Authentication in Shiny Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/article/article.html Use the secure_app function to wrap your Shiny UI and enforce authentication. ```R library(shiny) library(shinymanager) ui <- secure_app(fluidPage( "Hello, authenticated user!" )) server <- function(input, output, session) { res_auth <- secure_server(check_credentials = check_credentials(data.frame(user = "admin", password = "password"))) } shinyApp(ui, server) ``` -------------------------------- ### auth_ui Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-authentication.html Defines the user interface for the authentication module. ```APIDOC ## auth_ui(id, status = "primary", tags_top = NULL, tags_bottom = NULL, background = NULL, choose_language = NULL, lan = NULL, ...) ### Description Creates the UI components for the authentication module. ### Parameters - **id** (character) - Required - Module's id. - **status** (character) - Optional - Bootstrap status to use for the panel and the button (default: "primary"). - **tags_top** (tags) - Optional - UI elements to display on top of the module. - **tags_bottom** (tags) - Optional - UI elements to display on bottom of the module. - **background** (css) - Optional - CSS for authentication background. - **choose_language** (logical/character) - Optional - Add language selection on top. - **lan** (object) - Optional - A language object. ``` -------------------------------- ### Secure Credentials with Encrypted SQLite Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Store credentials in an AES-encrypted SQLite database using scrypt hashing. Requires the keyring package for secure key management. ```r # Init DB using credentials data credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), # password will automatically be hashed admin = c(FALSE, TRUE), stringsAsFactors = FALSE ) # you can use keyring package to set database key library(keyring) key_set("R-shinymanager-key", "obiwankenobi") # Init the database create_db( credentials_data = credentials, sqlite_path = "path/to/database.sqlite", # will be created passphrase = key_get("R-shinymanager-key", "obiwankenobi") # passphrase = "passphrase_wihtout_keyring" ) # Wrap your UI with secure_app, enabled admin mode or not ui <- secure_app(ui, enable_admin = TRUE) server <- function(input, output, session) { # check_credentials directly on sqlite db res_auth <- secure_server( check_credentials = check_credentials( "path/to/database.sqlite", passphrase = key_get("R-shinymanager-key", "obiwankenobi") # passphrase = "passphrase_wihtout_keyring" ) ) output$auth_output <- renderPrint({ reactiveValuesToList(res_auth) }) # your classic server logic ... } ``` -------------------------------- ### pwd_ui(id, tag_img = NULL, status = "primary", lan = NULL) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-password.html UI component for the password change module. ```APIDOC ## pwd_ui ### Description Generates the UI elements for the password change module. ### Parameters - **id** (string) - Required - Module's id. - **tag_img** (tags$img) - Optional - A tag to be displayed on the authentication module. - **status** (string) - Optional - Bootstrap status to use for the panel and the button (default: "primary"). - **lan** (object) - Optional - A language object. ``` -------------------------------- ### Validate credentials using a data.frame Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/check_credentials.html Demonstrates checking user credentials against a local data.frame, including handling of correct, incorrect, and non-existent users. ```R # data.frame with credentials info credentials <- data.frame( user = c("fanny", "victor"), password = c("azerty", "12345"), stringsAsFactors = FALSE ) # check a user check_credentials(credentials)("fanny", "azerty") #> $result #> [1] TRUE #> #> $expired #> [1] FALSE #> #> $authorized #> [1] TRUE #> #> $user_info #> user #> 1 fanny #> check_credentials(credentials)("fanny", "azert") #> $result #> [1] FALSE #> #> $expired #> [1] FALSE #> #> $authorized #> [1] TRUE #> #> $user_info #> user #> 1 fanny #> check_credentials(credentials)("fannyyy", "azerty") #> $result #> [1] FALSE #> #> $expired #> [1] FALSE #> #> $authorized #> [1] FALSE #> #> $user_info #> NULL #> ``` -------------------------------- ### Initialize ShinyManager Authentication Source: https://github.com/datastorm-open/shinymanager/blob/master/inst/article/article.html Use this snippet to wrap your Shiny UI with authentication logic. Ensure the credentials data frame is properly defined before calling this function. ```R ui <- secure_app(ui) server <- function(input, output, session) { res_auth <- secure_server( check_credentials = check_credentials(credentials) ) } ``` -------------------------------- ### Configure Password Failure Limit Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Set the maximum number of failed login attempts before locking an account. ```R options("shinymanager.pwd_failure_limit") ``` ```R options("shinymanager.pwd_failure_limit" = 5) ``` -------------------------------- ### auth_ui(id, status, tags_top, tags_bottom, background, choose_language, lan, ...) Source: https://github.com/datastorm-open/shinymanager/blob/master/man/module-authentication.Rd Renders the user interface for the authentication module within a Shiny application. ```APIDOC ## auth_ui ### Description Generates the UI elements for the authentication screen. ### Parameters - **id** (character) - Required - Module's id. - **status** (character) - Optional - Bootstrap status for the panel and button (default: "primary"). - **tags_top** (tags) - Optional - UI elements to display above the module. - **tags_bottom** (tags) - Optional - UI elements to display below the module. - **background** (css) - Optional - CSS for the authentication background. - **choose_language** (logical/character) - Optional - Enables language selection. - **lan** (object) - Optional - Language object. ``` -------------------------------- ### Authenticate with data.frame credentials Source: https://github.com/datastorm-open/shinymanager/blob/master/man/check_credentials.Rd Use a data frame to define users and passwords for authentication. ```R # data.frame with credentials info credentials <- data.frame( user = c("fanny", "victor"), password = c("azerty", "12345"), stringsAsFactors = FALSE ) # check a user check_credentials(credentials)("fanny", "azerty") check_credentials(credentials)("fanny", "azert") check_credentials(credentials)("fannyyy", "azerty") ``` -------------------------------- ### SQL Database Authentication Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/index.html Use the DBI interface to manage credentials in an external SQL database. ```R library(shiny) library(shinymanager) #### init the SQL Database # first edit the .yml configuration file system.file("sql_config/pg_template.yml", package = "shinymanager") # Init Credentials data credentials <- data.frame( user = c("shiny", "shinymanager"), password = c("azerty", "12345"), # password will automatically be hashed stringsAsFactors = FALSE ) # Create SQL database create_db_sql( credentials_data = credentials, config_path = "path/to/your_sql_configuration.yml" ) ### Use in shiny ui <- fluidPage( tags$h2("My secure application"), verbatimTextOutput("auth_output") ) # Wrap your UI with secure_app ui <- secure_app(ui, choose_language = TRUE) server <- function(input, output, session) { # call the server part # check_credentials returns a function to authenticate users res_auth <- secure_server( check_credentials = check_credentials(db = "path/to/your_sql_configuration.yml") ) # your classic server logic } shinyApp(ui, server) ``` -------------------------------- ### Implement Authentication Module in Shiny Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/module-authentication.html Integrates the authentication module into a Shiny app, including credential hashing, UI customization, and server-side dependency management. ```R if (interactive()) { library(shiny) library(shinymanager) # data.frame with credentials info # credentials <- data.frame( # user = c("fanny", "victor"), # password = c("azerty", "12345"), # comment = c("alsace", "auvergne"), # stringsAsFactors = FALSE # ) # you can hash the password using scrypt # and adding a column is_hashed_password # data.frame with credentials info credentials <- data.frame( user = c("fanny", "victor"), password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")), is_hashed_password = TRUE, comment = c("alsace", "auvergne"), stringsAsFactors = FALSE ) # app ui <- fluidPage( # authentication module auth_ui( id = "auth", # add image on top ? tags_top = tags$div( tags$h4("Demo", style = "align:center"), tags$img( src = "https://www.r-project.org/logo/Rlogo.png", width = 100 ) ), # add information on bottom ? tags_bottom = tags$div( tags$p( "For any question, please contact ", tags$a( href = "mailto:someone@example.com?Subject=Shiny%20aManager", target="_top", "administrator" ) ) ), # change auth ui background ? # https://developer.mozilla.org/fr/docs/Web/CSS/background background = "linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)), url('https://www.r-project.org/logo/Rlogo.png');", # set language ? lan = use_language("fr") ), # result of authentication verbatimTextOutput(outputId = "res_auth"), # classic app headerPanel('Iris k-means clustering'), sidebarPanel( selectInput('xcol', 'X Variable', names(iris)), selectInput('ycol', 'Y Variable', names(iris), selected=names(iris)[[2]]), numericInput('clusters', 'Cluster count', 3, min = 1, max = 9) ), mainPanel( plotOutput('plot1') ) ) server <- function(input, output, session) { # authentication module auth <- callModule( module = auth_server, id = "auth", check_credentials = check_credentials(credentials) ) output$res_auth <- renderPrint({ reactiveValuesToList(auth) }) # classic app selectedData <- reactive({ req(auth$result) # <---- dependency on authentication result iris[, c(input$xcol, input$ycol)] }) clusters <- reactive({ kmeans(selectedData(), input$clusters) }) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) par(mar = c(5.1, 4.1, 0, 1)) plot(selectedData(), col = clusters()$cluster, pch = 20, cex = 3) points(clusters()$centers, pch = 4, cex = 4, lwd = 4) }) } shinyApp(ui, server) } ``` -------------------------------- ### fab_button(..., position, animation, toggle, inputId, label) Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/fab_button.html Creates a fixed floating action button in the page corner with additional buttons inside. ```APIDOC ## fab_button ### Description Creates a fixed floating action button in the page corner. It can contain multiple action buttons that are revealed based on the specified toggle behavior. ### Signature `fab_button(..., position = c("bottom-right", "top-right", "bottom-left", "top-left", "none"), animation = c("slidein", "slidein-spring", "fountain", "zoomin"), toggle = c("hover", "click"), inputId = NULL, label = NULL)` ### Arguments - **...** (actionButton) - Required - actionButton(s) to be used as floating buttons. - **position** (character) - Optional - Position for the button. Defaults to "bottom-right". - **animation** (character) - Optional - Animation when displaying floating buttons. Defaults to "slidein". - **toggle** (character) - Optional - Display floating buttons when main button is clicked or hovered. Defaults to "hover". - **inputId** (character) - Optional - Id for the FAB button (acts like an actionButton). - **label** (character) - Optional - Label for the main button. ``` -------------------------------- ### Implement Custom Authentication Function Source: https://github.com/datastorm-open/shinymanager/blob/master/README.md Define a custom credential check function that connects to an external database and returns a list containing authentication results and user information. ```R require(RPostgreSQL) library(shiny) library(shinymanager) library(DBI) library(glue) dbname = "*****" host = "localhost" port = ***** user = "*****" password = "******" con <- dbConnect(dbDriver("PostgreSQL"), dbname = dbname , host = host, port = port , user = user, password = password ) DBI::dbWriteTable(con, "my_table", data.frame( user = c("test"), password = c("123"), stringsAsFactors = FALSE )) # or a config .yml file or others arguments my_custom_check_creds <- function(dbname, host, port, db_user, db_password) { # finally one function of user and password function(user, password) { con <- dbConnect(dbDriver("PostgreSQL"), dbname = dbname, host = host, port = port, user = db_user, password = db_password) on.exit(dbDisconnect(con)) req <- glue_sql("SELECT * FROM my_table WHERE \"user\" = ({user}) AND \"password\" = ({password})", user = user, password = password, .con = con ) req <- dbSendQuery(con, req) res <- dbFetch(req) if (nrow(res) > 0) { list(result = TRUE, user_info = list(user = user, something = 123)) } else { list(result = FALSE) } } } ui <- fluidPage( tags$h2("My secure application"), verbatimTextOutput("auth_output") ) ui <- secure_app(ui) server <- function(input, output, session) { res_auth <- secure_server( check_credentials = my_custom_check_creds( dbname = "******", host = "*****", port = ****, db_user = "*****", db_password = "*******" ) ) auth_output <- reactive({ reactiveValuesToList(res_auth) }) # access info observe({ print(auth_output()) }) } shinyApp(ui, server) ``` -------------------------------- ### Label management functions Source: https://github.com/datastorm-open/shinymanager/blob/master/docs/reference/custom-labels.html Functions for managing and retrieving label configurations. ```R set_labels(language, ...) get_labels(language = "en") ```