r语言 - 使用Shiny从Azure刷新令牌



我用r的Shiny包编写了一个web应用程序。整个应用程序由三个主要文件组成。ui。R,服务器。R应用程序。R.

应用程序。R是我在Azure Kubernetics中执行的文件,它随后调用其他两个文件。我还在此文件中管理Azure身份验证。所以我的app。r是这样的:

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)

我可以获得令牌,应用程序运行良好,但在线闪亮应用程序有一个小时的超时,我认为这是Azure令牌的标准。

我知道我可以在token $credentials$refresh_token中获得我的刷新令牌但是我不知道如何使用它来获得一个新的令牌,因为60分钟后屏幕变灰,即使用户在web应用程序中。

注意:我解码我在app.R中获得的令牌,并根据用户访问,我查询server.R中的数据库。如果有更有效的方法,请告诉我。

Azure AD提供的访问令牌的默认生存期在60-90分钟之间,以避免令牌受到各种攻击。

然而,当访问令牌过期时,客户端必须使用刷新令牌(通常是静默的)来获取新的刷新令牌来维持会话。刷新令牌的生命周期通常比访问令牌长(接近90天)。

要获得刷新令牌和访问令牌,您需要请求offline_access

R的Shiny包提供了get_azure_function来与Azure活动目录进行身份验证,该活动目录有不同的参数要传递以获得访问令牌。在这里,在Azure AD v2.0的资源参数中,您可以提供多个作用域,其中每个作用域由URL或GUID以及指定所请求访问类型的路径组成。

您需要在resource参数中传递特殊作用域offline_access,它从Azure AD请求一个刷新令牌以及访问令牌。

使用此作用域,您不需要重新验证即可再次获得令牌。如果令牌的凭证包含刷新令牌,则可以通过调用其refresh()方法自动刷新令牌对象。

#Example to authenticate using Azure resource manager along with refresh token.

token2 <- get_azure_token(c("https://management.azure.com/.default", "offline_access"),
"mytenant", "app_id", version=2)


#requesting multiple scopes (Microsoft Graph)along with refresh token with AAD 2.0

get_azure_token(c("https://graph.microsoft.com/User.Read.All",
"https://graph.microsoft.com/User.ReadWrite.All",
"https://graph.microsoft.com/Directory.ReadWrite.All",
"offline_access"),
"mytenant", "app_id", version=2)

最新更新