Send object from UI to server, while printing a message

This is probably a basic question, but: how can I display some programmatically-generated text to the user, and then send an object to the server?

The context is that I'm trying to get my AzureAuth package for Azure Active Directory OAuth authentication working with Shiny, using the device code flow. How this works is that first, your app contacts the server to get some authorization details: a user code, and a verification code. The user then visits a login page (whose URL is also provided by the server) and enter the user code. This prompts them to login to Azure.

Meanwhile, your app polls the server, passing it the verification code to identify itself. When the user has successfully logged in, the server will then return the token to your app.

The device code flow is really meant for situations where a web browser isn't available, so isn't really necessary for use with Shiny, but I'd still like to get it working. I already have the authorization code flow working, which is the usual way of logging in.

Here is some code I've rigged up. In the UI component, I contact the server to get the user and verification codes. I then pass the object containing the verification code to the server, via the global environment. Points of note:

  • I have to display the provided message to tell the user the code and login URL
  • The get_azure_token function in the server is continuously polling AAD until the user logs in

This works, but I'm sure there's a better way.

devtools::install_github("Azure/AzureAuth")
library(shiny)
library(AzureAuth)

options(shiny.port=8100)

# parameters -- should work for anyone with an Azure account
resource <- "https://management.azure.com"
tenant <- "common"
app <- "04b07795-8ddb-461a-bbee-02f9e1bf7b46"


ui <- fluidPage(
    {
        creds <<- get_device_creds(resource, tenant, app)
        tags$p(creds$message)
    },
    verbatimTextOutput("token")
)

server <- function(input, output, session)
{
    on.exit(rm(creds, envir=globalenv()))
    token <- get_azure_token(resource, tenant, app, auth_type="device_code",
                             use_cache=FALSE, device_creds=creds)

    output$token <- renderPrint(token)
}

shinyApp(ui, server)

Hi,

If I get it correctly, you want people access your Shiny app, then login to Azure (external) while the Shiny app monitors if they successfully logged in?

First question: How does the app know which user it's supposed to track? You have no input in your UI at this point. Second, are you asking the user to give personal info like username/password? I don't think that OK to collect from them through your Shiny app...

Please clarify :slight_smile:
PJ

This is basically explained at the link above. The message that's printed by the UI is "go to https://microsoft.com/devicelogin and enter the code XXXXXX". When you do that, you're prompted to login. After you've logged in, Azure will send the token to the app -- in this case, the get_azure_token function.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.