datastorm-open / shinymanager

Simple and secure authentification mechanism for single shiny applications.
https://datastorm-open.github.io/shinymanager/
386 stars 79 forks source link

`secure_app` ignores selected values in shiny widgets #55

Open cimentadaj opened 4 years ago

cimentadaj commented 4 years ago

I was trying to replicate your example code but with one tweak: add a selectInput with a predefined selection. In this example I use months and I pre-select 'September' but shinymanager ignores this and returns the default order of the months:

# define some credentials
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 authentification mechanism 
  for single ‘Shiny’ applications.",
  stringsAsFactors = FALSE
)

library(shiny)
library(shinymanager)

ui <- fluidPage(
  selectInput("month", "Months", choices = month.name, selected = "September"),
  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)

ex1

Is this expected?

pvictor commented 4 years ago

Hello, No that's not expected, and it works fine for me, are you sure there's not a problem elsewhere ? If you try this in a fresh R session, the problem still occur ?

Victor

cimentadaj commented 4 years ago

Yep. I just ran this in a fresh R session, used the shiny and azerty credentials and the input is placed on January. Here's my SI from that session:

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Ubuntu 20.04.1 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2020-09-03                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date       lib source                           
#>  askpass        1.1        2019-01-13 [1] CRAN (R 4.0.0)                   
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                   
#>  backports      1.1.8      2020-06-17 [1] CRAN (R 4.0.2)                   
#>  billboarder    0.2.8      2020-01-09 [1] CRAN (R 4.0.2)                   
#>  bit            1.1-15.2   2020-02-10 [1] CRAN (R 4.0.1)                   
#>  bit64          0.9-7      2017-05-08 [1] CRAN (R 4.0.1)                   
#>  blob           1.2.1      2020-01-20 [1] CRAN (R 4.0.0)                   
#>  callr          3.4.3      2020-03-28 [1] CRAN (R 4.0.0)                   
#>  cli            2.0.2      2020-02-28 [1] CRAN (R 4.0.0)                   
#>  colorspace     1.4-1      2019-03-18 [1] CRAN (R 4.0.0)                   
#>  crayon         1.3.4      2017-09-16 [1] CRAN (R 4.0.0)                   
#>  curl           4.3        2019-12-02 [1] CRAN (R 4.0.0)                   
#>  DBI            1.1.0      2019-12-15 [1] CRAN (R 4.0.0)                   
#>  desc           1.2.0      2018-05-01 [1] CRAN (R 4.0.0)                   
#>  devtools       2.3.0      2020-04-10 [1] CRAN (R 4.0.2)                   
#>  digest         0.6.25     2020-02-23 [1] CRAN (R 4.0.0)                   
#>  dplyr          1.0.2      2020-08-18 [1] CRAN (R 4.0.2)                   
#>  DT             0.14       2020-06-24 [1] CRAN (R 4.0.1)                   
#>  ellipsis       0.3.1      2020-05-15 [1] CRAN (R 4.0.0)                   
#>  evaluate       0.14       2019-05-28 [1] CRAN (R 4.0.0)                   
#>  fansi          0.4.1      2020-01-08 [1] CRAN (R 4.0.0)                   
#>  fastmap        1.0.1      2019-10-08 [1] CRAN (R 4.0.0)                   
#>  fs             1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                   
#>  generics       0.0.2      2018-11-29 [1] CRAN (R 4.0.0)                   
#>  ggplot2        3.3.2      2020-06-19 [1] CRAN (R 4.0.2)                   
#>  glue           1.4.2      2020-08-27 [1] CRAN (R 4.0.2)                   
#>  gtable         0.3.0      2019-03-25 [1] CRAN (R 4.0.0)                   
#>  highr          0.8        2019-03-20 [1] CRAN (R 4.0.0)                   
#>  htmltools      0.5.0      2020-06-16 [1] CRAN (R 4.0.1)                   
#>  htmlwidgets    1.5.1      2019-10-08 [1] CRAN (R 4.0.0)                   
#>  httpuv         1.5.4      2020-06-06 [1] CRAN (R 4.0.1)                   
#>  httr           1.4.2      2020-07-20 [1] CRAN (R 4.0.2)                   
#>  jsonlite       1.7.0      2020-06-25 [1] CRAN (R 4.0.2)                   
#>  knitr          1.29       2020-06-23 [1] CRAN (R 4.0.2)                   
#>  later          1.1.0.1    2020-06-05 [1] CRAN (R 4.0.1)                   
#>  lifecycle      0.2.0      2020-03-06 [1] CRAN (R 4.0.0)                   
#>  magrittr       1.5        2014-11-22 [1] CRAN (R 4.0.0)                   
#>  memoise        1.1.0      2017-04-21 [1] CRAN (R 4.0.0)                   
#>  mime           0.9        2020-02-04 [1] CRAN (R 4.0.0)                   
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.0.0)                   
#>  openssl        1.4.2      2020-06-27 [1] CRAN (R 4.0.2)                   
#>  pillar         1.4.6      2020-07-10 [1] CRAN (R 4.0.2)                   
#>  pkgbuild       1.1.0      2020-07-13 [1] CRAN (R 4.0.2)                   
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.0.0)                   
#>  pkgload        1.1.0      2020-05-29 [1] CRAN (R 4.0.0)                   
#>  prettyunits    1.1.1      2020-01-24 [1] CRAN (R 4.0.0)                   
#>  processx       3.4.3      2020-07-05 [1] CRAN (R 4.0.2)                   
#>  promises       1.1.1      2020-06-09 [1] CRAN (R 4.0.1)                   
#>  ps             1.3.4      2020-08-11 [1] CRAN (R 4.0.2)                   
#>  purrr          0.3.4      2020-04-17 [1] CRAN (R 4.0.0)                   
#>  R.methodsS3    1.8.0      2020-02-14 [1] CRAN (R 4.0.0)                   
#>  R.oo           1.23.0     2019-11-03 [1] CRAN (R 4.0.0)                   
#>  R.utils        2.9.2      2019-12-08 [1] CRAN (R 4.0.0)                   
#>  R6             2.4.1      2019-11-12 [1] CRAN (R 4.0.0)                   
#>  Rcpp           1.0.5      2020-07-06 [1] CRAN (R 4.0.2)                   
#>  remotes        2.1.1      2020-02-15 [1] CRAN (R 4.0.0)                   
#>  rlang          0.4.7      2020-07-09 [1] CRAN (R 4.0.2)                   
#>  rmarkdown      2.2        2020-05-31 [1] CRAN (R 4.0.0)                   
#>  rprojroot      1.3-2      2018-01-03 [1] CRAN (R 4.0.0)                   
#>  RSQLite        2.2.0      2020-01-07 [1] CRAN (R 4.0.1)                   
#>  scales         1.1.1      2020-05-11 [1] CRAN (R 4.0.1)                   
#>  scrypt         0.1.3      2019-08-09 [1] CRAN (R 4.0.2)                   
#>  sessioninfo    1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                   
#>  shiny        * 1.5.0      2020-06-23 [1] CRAN (R 4.0.2)                   
#>  shinymanager * 1.0.300    2020-08-24 [1] CRAN (R 4.0.2)                   
#>  stringi        1.4.6      2020-02-17 [1] CRAN (R 4.0.0)                   
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                   
#>  testthat       2.3.2      2020-03-02 [1] CRAN (R 4.0.0)                   
#>  tibble         3.0.3.9000 2020-07-29 [1] Github (tidyverse/tibble@b4eec19)
#>  tidyselect     1.1.0      2020-05-11 [1] CRAN (R 4.0.0)                   
#>  usethis        1.6.1      2020-04-29 [1] CRAN (R 4.0.0)                   
#>  vctrs          0.3.2      2020-07-15 [1] CRAN (R 4.0.2)                   
#>  webshot        0.5.2      2019-11-22 [1] CRAN (R 4.0.2)                   
#>  withr          2.2.0      2020-04-20 [1] CRAN (R 4.0.0)                   
#>  xfun           0.16       2020-07-24 [1] CRAN (R 4.0.2)                   
#>  xml2           1.3.2      2020-04-23 [1] CRAN (R 4.0.0)                   
#>  xtable         1.8-4      2019-04-21 [1] CRAN (R 4.0.0)                   
#>  yaml           2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                   
#> 
#> [1] /usr/local/lib/R/site-library
#> [2] /usr/lib/R/site-library
#> [3] /usr/lib/R/library
pvictor commented 4 years ago

Mmmh, without shinymanager there's no problem ? If you replace selectInput by radioButtons, same issue ?

cimentadaj commented 4 years ago

So I did some further testing. I ran the code above but excluding shinymanager and selectInput correctly begins with September as selected. If I include shinymanager, it defaults to January. However, if I switch selectInput for radioButtons, it correctly selects September as default. This is weird.

cimentadaj commented 4 years ago

Just to be 100% sure this is not some quirk from my computer or something, I dockerized this example and ran it with the latest shiny image and shinymanager. In case you want to replicate:

Create folder shinytest

Create file app.R inside shinytest with:

library(shiny)
library(shinymanager)

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 authentification mechanism 
  for single ‘Shiny’ applications.",
  stringsAsFactors = FALSE
)

ui <- fluidPage(
  ## radioButtons("test", "Months", choices = month.name, selected = "September"),
  selectInput("month", "Months", choices = month.name, selected = "September"),
  tags$h2("My secure application"),
  verbatimTextOutput("auth_output")
)

ui <- secure_app(ui)

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

  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )

  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })

}

shinyApp(ui, server)

Create Dockerfile inside shinytest folder with:

FROM rocker/shiny:latest
RUN mkdir shinytest
COPY ./ shinytest
WORKDIR ./shinytest
RUN Rscript -e "install.packages('shinymanager')"
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('./', host = '0.0.0.0', port = 3838)"]

Using the shell, build the image with the code below. Remember to change the paths:

docker build --tag shinytest -f /home/jorge/Downloads/shinytest/Dockerfile /home/jorge/Downloads/shinytest/

Finally, run the container with:

docker container run -i -p 3838:3838 --rm -t shinytest:latest

After logging in at http://0.0.0.0:3838/, I still get the selected value to be January:

shinytest1

pvictor commented 4 years ago

Thanks for that, I will try on my Linux machine as soon as I can. This is a very weird one.

Do you see any errors in the JavaScript console ? What browser do you use ? (I tried Chrome, Firefox and IE without problems)

I've deployed the example on shinyapps.io, don't see any problem neither : https://dreamrs.shinyapps.io/shinymanager55/

Victor

cimentadaj commented 4 years ago

Nop, there's no error from JS. I use Firefox. Let me try it on Chrome.

In the example that you send, can you also upload the one with selectInput? I see the radioButton now and that works for me as well.

pvictor commented 4 years ago

selectInput is just below the radioButton

cimentadaj commented 4 years ago

Sorry about that, just saw it. I also see September. Well, it seems the problem is Firefox because I can see the dockerized version correctly in Chromium. Also I can see your example correctly on both Firefox and Chromium. Not sure what is happening.

pvictor commented 4 years ago

I have Firefox 80.0.1

If you inspect element to see HTML code, do you see this:

image

The line with selected="selected" and the one with option selected

cimentadaj commented 4 years ago

I'm usin Firefox 80.0 64-bit. Here's the image: shinytest2