gpt4 book ai didi

使用 Shiny 从 Azure 刷新 token

转载 作者:行者123 更新时间:2023-12-02 06:55:27 31 4
gpt4 key购买 nike

我使用 R 的 Shiny 包编写了一个 Web 应用程序。整个应用程序由三个主要文件组成。 ui.RServer.Rapp.R

app.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)

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

我知道我可以在 Token$credentials$refresh_token 中获取刷新 token ,但我不知道如何使用它来获取新 token ,因为 60 分钟后,即使用户在使用时屏幕也会变灰在网络应用程序中。

注意:我对在 app.R 中获得的 token 进行解码,并根据用户访问情况查询 server.R 中的数据库。如果有更有效的方法,请指教。

最佳答案

Azure AD 提供的访问 token 的默认生命周期为 60-90 分钟,以避免 token 受到各种攻击。

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

要获取刷新 token 和访问 token ,您需要在请求 token 时请求 offline_access 范围。

R 的 Shiny 包提供了 get_azure_function 来通过 Azure Active Directory 进行身份验证,该目录具有不同的参数来传递以获取访问 token 。在这里,在 Azure AD v2.0 的资源参数中,您可以提供多个范围,其中每个范围由 URL 或 GUID 以及指定请求的访问类型的路径组成。

您需要在资源参数中传递特殊范围offline_access,该参数会从 Azure AD 请求刷新 token 以及访问 token 。

使用此范围,您无需重新进行身份验证即可再次获取 token 。如果 token 的凭证包含刷新 token ,则可以通过调用 token 对象的refresh()方法自动刷新 token 对象。

    #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)

关于使用 Shiny 从 Azure 刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72349519/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com