I have put together an app using Golem that uses a js script to obtain an Azure AD Oauth token for a db connection.
The intended use of this app is for when someone presses the "search" action button, it initiates a connection (with Azure AD authentication) to a snowflake database for querying data. I am running this app through the browser by default (it will eventually be deployed to a RS Connect server).
When it is running through the browser, I am being hit by a message that says "invalid oauth token". It's looking looking like the JS script in the UI is not communication with the dbconnect section in the server - how do I go about fixing this? It works when I run it locally though.
The relevant pieces of code are below (credentials blocked out using "XXX"). Apologies for the long winded code.
ui:
mod_01_search_ui <- function(id){
ns <- NS(id)
tagList(
# Leave this function for adding external resources
golem_add_external_resources(),
shinydashboard::dashboardPage(
shinydashboard::dashboardHeader(),
shinydashboard::dashboardSidebar(disable = TRUE),
shinydashboard::dashboardBody(
# oauth JS
tags$script(src="https://alcdn.msauth.net/browser/2.30.0/js/msal-browser.min.js"),
tags$script(HTML("
async function wrapperFunc() {
const msalConfig = {
auth: {
clientId: 'XXX',
authority: 'https://login.microsoftonline.com/XXX'
}
};
const msalInstance = new msal.PublicClientApplication(msalConfig);
const silentRequest = {
scopes: ['api://XXX/session:role-any']
};
const callLogin = async function(silentRequest, msalInstance) {
try {
const loginResponse = await msalInstance.loginPopup(silentRequest);
return loginResponse;
} catch (err) {
console.log(err)
}
}
response = callLogin(silentRequest, msalInstance);
return response;
}
wrapperFunc().then(result => {
Shiny.setInputValue('01_search_1-oauthToken', result['accessToken']);
console.log(result['accessToken']);
});")),
fluidRow(align = 'center',
br(),
actionButton(ns("search"),"Search",icon("paper-plane"),
style="color: #000000; background-color: #ffb80c; border-color:
#ffb80c"))
)
}
server:
mod_01_search_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
library(renv)
data <- eventReactive(input$search, {
connection <- connection <- DBI::dbConnect(
drv = odbc::odbc(),
dsn = "snowflake",
token = input$oauthToken,
authenticator = "oauth")
})
}