Passing username and groups as HTTP headers in 3.1.0

Hi, I’m very excited about the new container pre-initialization and sharing features 3.1.0!

I can see from the docs that when this is enabled “ShinyProxy does not pass the username and groups using environment variables, however, they are passed to the app using HTTP headers.”

Does anyone have a code example of how these can be retrieved server-side from R when the app is launched?

Thanks!

Hi Paul, I’m also very excited about the new container pre-initialization and sharing features. To answer your question, I found this nice minimal code example here at the end:
javascript - get response header variable in Shiny - Stack Overflow

library(shiny)

server <- function(input, output, session) {
    
    output$summary <- renderText({
        ls(env=session$request)
    })
    
    output$headers <- renderUI({
        selectInput("header", "Header:", ls(env=session$request))
    })
    
    output$value <- renderText({
        if (nchar(input$header) < 1 || is.null(input$header) || !exists(input$header, envir=session$request)){
            return("NULL");
        }
        return (get(input$header, envir=session$request));
    })
    
}

ui <- pageWithSidebar(
    headerPanel("Shiny Client Data"),
    sidebarPanel(
        uiOutput("headers")
    ),
    mainPanel(
        h3("Headers passed into Shiny"),
        verbatimTextOutput("summary"),
        h3("Value of specified header"),
        verbatimTextOutput("value")
    )
)

shinyApp(ui, server)

When starting from within shinyproxy you should see:

http_header1

# Added by default
get("HTTP_X_SP_USERGROUPS", envir=session$request)
get("HTTP_X_SP_USERID", envir=session$request)

You can add additional headers.
I’m using OpenID Connect and added this to my app configuration:

http-headers:
        X_SP_GIVEN_NAME: "#{oidcUser.givenName}"
        X_SP_FAMILY_NAME: "#{oidcUser.familyName}"
        X_SP_EMAIL: "#{oidcUser.email}"
        X_SP_PREFFERED_USERNAME: "#{oidcUser.preferredUsername}"
2 Likes

Thanks, is there any way to pass the headers locally for development. Ideally, analogous adding variables to .Renviron. I think there should be a way to add headers using runApp or rmarkdown::run, but it isn’t obvious. I don’t use the run button, but I know developers who do, ideally it would be transparent.

1 Like

Hello, here is an example using devmode.

library(shiny)

ui <- fluidPage(
  textOutput("sp_userid"),
  textOutput("sp_usergroups")
)

server <- function(input, output, session) {
    if(in_devmode()){
      session$request$HTTP_X_SP_USERGROUPS <- get_devmode_option("sp_usergroups")
      session$request$HTTP_X_SP_USERID <- get_devmode_option("sp_userid")
    }
    output$sp_userid <- renderText({
      session$request$HTTP_X_SP_USERID
    })
    output$sp_usergroups <- renderText({
      session$request$HTTP_X_SP_USERGROUPS
    })
}

# Run the application
devmode(TRUE)
register_devmode_option("sp_usergroups", devmode_default = "USERS,ADMINS")
register_devmode_option("sp_userid", devmode_default = "my_username")
shinyApp(ui = ui, server = server)
2 Likes

We just published a demo for this: https://github.com/openanalytics/shinyproxy-shiny-demo-auth .

2 Likes