3

My apache passes LDAP login to variable X-Remote-User in header:

enter image description here

but I don't know how to get it in Shiny app. Any ideas? Maybe solution could be some java script?

Taz
  • 5,755
  • 6
  • 26
  • 63

3 Answers3

3

Ok I resolved it. Firstly include in ui.R js function:

www/js/getLogin.js:

function requestLogin() {
var client = new XMLHttpRequest();
client.open("GET", "yourshinyhostname", true);
client.send();
client.onreadystatechange = function() {
    
var resposne = client.getResponseHeader("X-Remote-User");
Shiny.onInputChange("getLogin", resposne);
}; }

Then you can get X-Remote-User value for example by clicking button:

ui.R:

includeScript("www/js/getLogin.js"),
uiOutput("login_btn"),
verbatimTextOutput("text")

server.R :

output$login_btn <- renderUI({
  
  HREF <- sprintf('
                  <button id="get_login_btn" value="test" onclick="requestLogin();" >
                  <font color="black">
                  <i class="fa fa-user"></i> Get login
                  </font> 
                  </button>
                  ')
HTML(HREF)
}) 

jsOutput <- reactive({
  input$getLogin
})

output$text <- renderPrint({ 
  jsOutput()
})
Taz
  • 5,755
  • 6
  • 26
  • 63
  • Is there a way to do the same thing but without requiring any clicking by the user? – wdkrnls Oct 15 '19 at 14:16
  • @wdkrnls you can try something like this https://gist.github.com/Tazovsky/3e1b68a7fe14dc242b4a85549b2575f2 but I'm not sure it will work. – Taz Oct 15 '19 at 14:42
  • Thanks, but so far no luck. I even tried to add a Shiny.setInputValue("getUsername", response); with no success. – wdkrnls Oct 15 '19 at 15:01
  • @Taz: what is 'www/js/getLogin.js' ? Shoul I write a seperate js-file? – maniA Nov 19 '21 at 08:25
  • @maniA it is JS script starting with `function requestLogin() {...` – Taz Nov 29 '21 at 16:33
0

Add a call to the js function in server.R. This will fetch the header without a button click. For e.g:

output$text <- renderPrint({ 
  js$requestLogin()
  jsOutput()
})
san
  • 1
0

You can access header variables with session$request. Here's an interactive example adapted from GitHub.

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 || !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)
Jeff Bezos
  • 1,929
  • 13
  • 23