I wrote a web application using Shiny package of R. The whole application consists of three main files. ui.R, Server.R and app.R.
app.R is the files where I execute in the Azure Kubernetics and it calls subsequently other two files.I'm also managing the Azure authentication in this file. So my app.R looks like :
load.lib <- c("AzureAuth","shiny","shinyjs","httr","config")
# install.lib <- load.lib[!load.lib %in% installed.packages()]
# for(lib in install.lib) install.packages(lib,dependencies=TRUE)
sapply(load.lib,library,character=TRUE)
AADConfig <- config::get(file = "config.yml")
resourceid = AADConfig$resourceid # Application (client) id
tenant = AADConfig$tenant # Directory(tenant) id
app = AADConfig$app # Application (client) id
pass = AADConfig$secret
redirect <- "https://myapp.com"
# resource <- c("https://management.azure.com/.default", resourceid)
clean_url_js <- sprintf(
"
$(document).ready(function(event) {
const nextURL = '%s';
const nextTitle = 'My new page title';
const nextState = { additionalInformation: 'Updated the URL with JS' };
// This will create a new entry in the browser's history, without reloading
window.history.pushState(nextState, nextTitle, nextURL);
});
", redirect
)
###############Importing the app R files#########
# load ui elements
source("ui.R")
# load server function
source("server.R")
#################################################
ui_func <- function(req)
{
opts <- parseQueryString(req$QUERY_STRING)
if(is.null(opts$code))
{
auth_uri <- AzureAuth::build_authorization_uri(resourceid, tenant, app, redirect_uri=redirect)
redir_js <- sprintf("location.replace(\"%s\");", auth_uri)
tags$script(HTML(redir_js))
}
else ui
}
server_func <- function(input, output, session)
{
shinyjs::runjs(clean_url_js)
opts <- parseQueryString(isolate(session$clientData$url_search))
if(is.null(opts$code))
return()
Token <- AzureAuth::get_azure_token(resourceid,
tenant,
app,
password = pass,
auth_type="authorization_code",
authorize_args=list(redirect_uri=redirect),
use_cache=TRUE,
auth_code = opts$code
)
access_role <- AzureAuth::decode_jwt(Token)$payload$groups
return(server(input, output, access_role))
}
# Run the application
shiny::shinyApp(ui = ui_func, server = server_func)
I can get the Token and the application runs fine but there is a one hour timeout for online Shiny apps, which is standard I think from the Azure tokens.
I know I can get my refresh token in Token$credentials$refresh_token but I don't know how to use it to get a new token as after 60 minutes the screen greys out even while users are in the web app.
Note: I decode the Token I get in the app.R and based on users access, I query the database in the server.R. If there is a more efficient way of doing it, please advise.