我有一个 Angular 10 应用程序,正在使用 MSAL-Angular 包通过 Azure AD 进行身份验证。它使用授权流程成功验证并检索 access_token
和 id_token
。
我还有一个 Express API 后端,我将 access_token(使用 Msal-Angular
中的拦截器)附加到请求的 header 上。我想在允许访问 api 之前验证此 token 以验证 Angular 应用程序是否已通过身份验证。为此,我尝试将 passport-azure-ad
与 BearerStrategy 结合使用。我相信我已正确设置所有内容,但它一直告诉我无法验证 token 。
我发现如果我使用id_token
而不是access_token
,它就可以被验证;通过 Passport 策略并使用 jwt.io。
所以这里真正的问题是我可以在配置中做一些不同的事情来验证使用 Msal-Angular
传递的 access_token
还是我应该编写自己的拦截器发送 id_token
而不是 access_token
?
API Passport 配置代码 - 我在这里要做的就是让护照验证通过,然后填写回调函数。
const options: IBearerStrategyOptionWithRequest = {
identityMetadata:
'https://login.microsoftonline.com/{tenet}.onmicrosoft.com/v2.0/.well-known/openid-configuration',
clientID: '{my_client_id}',
validateIssuer: true,
loggingLevel: 'info',
loggingNoPII: false,
passReqToCallback: true,
};
const bearerStrategy = new BearerStrategy(options,
function (req, token, done) {
logger.debug(req);
logger.debug(token);
return done(null, { name: 'user name' }, token);
});
passport.use(bearerStrategy);
保护路线
function (req, res, next) {passport.authenticate('oauth-bearer', { session: false }), listTasks);
给出的错误:
{
"name":"AzureAD: Bearer Strategy",
"hostname":"my_host",
"pid":36344,
"level":30,
"msg":"authentication failed due to: invalid signature",
"time":"2021-03-02T20:50:02.140Z",
"v":0
}
最佳答案
我明白了。这与我在 access_token 上获取的范围有关。我当时使用 'user.read'
和 'email'
,当我切换到使用 '{client_id}/.default'
时并且可以使用 v1 众所周知的元数据 url 来验证生成的 token 。
总的来说,我对范围仍然有点粗略,但这是我发现导致解决我的问题的更改的页面。
关于angular - Passport-Azure-AD 未验证来自 MSAL Angular 身份验证的 access_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66448108/