我正在尝试将 Microsoft 令牌集成到我的应用程序中,但我有一个奇怪的错误,即使我每 30 分钟刷新一次令牌,它也会说令牌在 1 天后已过期。 我使用以下代码从用户那里获取令牌
const scope = e.target.dataset.scope;
var url = new URL("https://login.microsoftonline.com/common/oauth2/v2.0/authorize")
const params = {
client_id: '****',
response_type: 'code',
redirect_uri: `${Meteor.settings.public.__BASE_URL}/template/email-import`,
scope: 'user.read mail.read mail.readbasic mail.readwrite Mail.Send',
code_challenge: '****',
code_challenge_method: 'plain',
state: scope
}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
window.location.replace(url);
之后,我使用访问令牌和刷新令牌捕获响应,并使用以下代码每 30 分钟刷新一次
const params = {
client_id: '****',
scope: 'user.read mail.read mail.readbasic mail.readwrite Mail.Send',
redirect_uri: Meteor.settings.public.__JTI_SERVER + '/template/email-import',
grant_type: 'refresh_token',
refresh_token: refreshToken,
}
var formData = new URLSearchParams();
for (var k in params) {
formData.append(k, params[k]);
}
const response = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': Meteor.settings.public.__JTI_SERVER + '/template/email-import'
}
})
const json = await response.json();
if(json.error) {
throw new Error(`Error refreshing the token for company id ${company._id} and email ${company.email_details[scope].user_info.mail}`)
} else {
Company.update(company._id, {$set: {[`email_details.${scope}.microsoft_token`]: json }})
}
老实说,我认为我正确刷新了令牌,因为每次函数运行时我都会获得不同的访问和刷新令牌。但即使如此,令牌似乎会在一天后过期,而且我无法在设置中找到更改此设置的位置。
我得到的完整错误日志是
{
error: 'invalid_grant',
error_description: 'AADSTS700081: The refresh token has expired due to maximum lifetime. The token was issued on 2021-05-23T09:51:53.7700436+00:00 and the maximum allowed lifetime for this application is 1.00:00:00.\r\n' +
'Trace ID: e0ae3ecc-1324-4ae5-823b-ed38e393a400\r\n' +
'Correlation ID: 409fd9ed-64c0-4f81-8ba7-546e6ceb2542\r\n' +
'Timestamp: 2021-05-26 07:30:01Z',
error_codes: [ 700081 ],
timestamp: '2021-05-26 07:30:01Z',
trace_id: 'e0ae3ecc-1324-4ae5-823b-ed38e393a400',
correlation_id: '409fd9ed-64c0-4f81-8ba7-546e6ceb2542',
error_uri: 'https://login.microsoftonline.com/error?code=700081'
}
如果有人可以提供帮助,我们将不胜感激。
提前致谢,
奥斯卡
没有办法解决这个问题。您只能将应用程序类型更改为 SPA 以外的其他类型,例如 Web 应用程序。
对于SPA类型,特意规定了令牌的有效期只有24小时,因此如果攻击者窃取了
refresh_ token
,他就无法根据access_token
不断收到新的refresh_token
。
有关应用程序类型的更多信息: https://learn.microsoft.com/en-us/entra/identity-platform/v2-app-types