typescript - 如何测试刷新和过期的令牌场景?
我正在使用 msal.js 库来对我的 AAD 账户进行身份验证。我已经实现了处理令牌刷新的逻辑,但我不确定如何最好地手动测试这一点。
try {
const response = await userAgentApplication.acquireTokenSilent(parameters);
jwtIdToken = response.idToken.rawIdToken;
} catch (error) {
if (error instanceof InteractionRequiredAuthError) {
if (type === LoginType.Redirect) {
userAgentApplication.acquireTokenRedirect(parameters);
}
const response = await userAgentApplication.acquireTokenPopup(parameters);
jwtIdToken = response.idToken.rawIdToken;
} else {
// Error was not related to needing interaction
throw error;
}
}
我能够验证使用system配置并将令牌续订偏移设置为大于过期限制的值时,令牌正在正确刷新。
system: {
tokenRenewalOffsetSeconds: 60 * 60
}
我唯一不知道如何测试的情况是,当令牌无法再刷新时,用户在抛出 InteractionRequiredAuthError 错误后需要重新登录。我真的希望我不需要保持会话活跃 24 小时然后再尝试刷新令牌...
答案#1
从应用程序中撤回用户同意——进入应用门户,找到 JS 应用并将其删除。这将移除你对该应用的同意,并使你的刷新令牌失效。也有一个 PowerShell 命令可以执行此操作。