gpt4 book ai didi

node.js - Azure AD - 仅应用程序 token 中缺少角色声明

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:23 26 4
gpt4 key购买 nike

当我尝试从 Nodejs 后端服务器获取仅应用程序 token 时,如所述 here ,有时 token 中缺少 roles 声明,这会导致 Authorization_IdentityNotFoundAuthorization_RequestDenied 错误。

我创建了一个函数来获取仅限应用程序的 token ,该函数将调用 /token 端点,直到 roles 声明在检索到的 token 中可用。

这是函数:

async getApplicationTokenByTenant(
tenantId: string
): Promise<any> {
// post param
const params = {
client_id: process.env.APP_ID,
client_secret: process.env.APP_SECRET,
grant_type: "client_credentials",
scope: "https://graph.microsoft.com/.default"
};

let expectedRes = null;
let retryCount = 0;
// try to get token for max 4 times
while (!expectedRes && retryCount < 4) {
console.log(`Try:${retryCount + 1}`);
// call api
const res = await fetch(
`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
{
method: "POST",
body: form(params),
headers: {
Accept: "application/json",
"Content-Type":
"application/x-www-form-urlencoded"
}
}
);

if (res.status !== 200) {
const exception = await res.json();
throw exception;
}

const response = await res.json();

// Decode token
const decodedToken = jws.decode(
response.access_token
) as any;
console.log("decodedToken");
console.log(decodedToken);

// check if "roles" exist in token
if (!decodedToken.roles) {
// if not wait for 3 seconds before retry
await asyncWait(3000);
retryCount++;
} else {
// got token having roles claim
expectedRes = response;
}
}
if (!expectedRes) {
// did not get expected response after max retry
throw new Error("Unable to get app token with roles");
}
return expectedRes;
}

这是回复:

尝试:1

{
"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"iat": 1544945058,
"nbf": 1544945058,
"exp": 1544948958,
"aio": "42RgYAhLEHI9n/8xtk896Mfyc2cWAwA=",
"app_displayname": "Dev Bot",
"appid": "axxxxxxx-1xxx-4xxx-8bc6-bxxxxxxxxxxx",
"appidacr": "1",
"idp": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"oid": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"sub": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"tid": "8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx",
"uti": "qrPy3iOYp0emoAWhGI6oAA",
"ver": "1.0",
"xms_tcdt": 1540903121
}

尝试:2

{
"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"iat": 1544945062,
"nbf": 1544945062,
"exp": 1544948962,
"aio": "42RgYGBJfLUkMmfnZgFrhl8lThs0AQ==",
"app_displayname": "Dev Bot",
"appid": "axxxxxxx-1xxx-4xxx-8bc6-bxxxxxxxxxxx",
"appidacr": "1",
"idp": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"oid": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"sub": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"tid": "8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx",
"uti": "cGcY4Mdbyk6BkZlOjVSfAA",
"ver": "1.0",
"xms_tcdt": 1540903121
}

尝试:3

{
"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"iat": 1544945070,
"nbf": 1544945070,
"exp": 1544948970,
"aio": "42RgYFgQlXKE/SWHGP+115sO7D/yAwA=",
"app_displayname": "Dev Bot",
"appid": "axxxxxxx-1xxx-4xxx-8bc6-bxxxxxxxxxxx",
"appidacr": "1",
"idp": "https://sts.windows.net/8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx/",
"oid": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"roles": [
"Mail.ReadWrite",
"Group.ReadWrite.All",
"Files.ReadWrite.All",
"Directory.Read.All",
"Mail.Read"
],
"sub": "7xxxxxxx-6xxxx-4xxxx-a94f-cxxxxxxxxxxx",
"tid": "8xxxxxxx-6xxx-4xxx-8xxx-exxxxxxxxxxx",
"uti": "CMiI-hcLlUCpqO6z70ukAA",
"ver": "1.0",
"xms_tcdt": 1540903121
}

如响应所示,我在第三次尝试时获得了预期的 token (具有角色声明)。为什么这在第一次尝试时不起作用?有什么原因吗?

最佳答案

在我这边,我尝试了门户中的管理员同意和管理员同意的 URL 请求,然后立即获得了仅应用程序 token , token 中包含角色。据我所知,更新权限可能需要时间才能起作用。

enter image description here

关于node.js - Azure AD - 仅应用程序 token 中缺少角色声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53800659/

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