使用 passport-azure-ad 配置通过 OIDCStrategy 和 BearerStrategy 保护网络应用程序和网络服务器
using passport-azure-ad configuration to secure a web app and web server via OIDCStrategy and BearerStrategy
我有一个由 express.js 服务器(网络应用程序服务器)提供的网络应用程序。 Web 应用服务器通过 passport.js 和 passport-azure-ad npm 包处理用户登录。为此,我正在使用 OIDCStrategy。
我还通过另一台服务器(rest 后端)托管 REST api。我想通过 passport.js 和使用 BearerStrategy 的 passport-azure-ad npm 包来保护这个后端。为此,我想在 Web 应用程序服务器通行证配置中定义一个范围,以便可以通过 cookie 将 Web 应用程序服务器的 access_token 传递到我的 Web 应用程序,并从那里用于访问其余后端。
我的问题:使用我当前的配置,我在尝试使用 access_token 访问我的后端 api 时收到 401 访问被拒绝。 access_token invalid
是错误信息:{"name":"AzureAD: Bearer Strategy", "msg":"authentication failed due to: error: invalid_token","v":0}
我认为我应该在登录时被重定向到权限页面,但事实并非如此。所以我想我的访问令牌实际上是无效的。
Web 应用服务器通行证配置:
passport.use(
new OIDCStrategy(
{
clientID: credsAzureAD.clientID,
identityMetadata: credsAzureAD.identityMetadata, // here is use the web app tenant id
clientSecret: credsAzureAD.clientSecret,
callbackURL: credsAzureAD.returnURL,
redirectUrl: credsAzureAD.returnURL,
skipUserProfile: true,
responseType: 'code',
responseMode: 'query',
scope: 'api://test/Write.Contributor',
useCookieInsteadOfSession: false,
passReqToCallback: false
},
(issuer, sub, profile, accessToken, refreshToken, done) => {
user.accessToken = accessToken;
return done(null, user);
}
)
);
我尝试使用范围,其中 api://test
是我的 REST API 应用程序 ID uri,/Write.Contributor
是我在 azure 活动目录中定义的范围。
我的 REST 后端服务器护照配置:
const options = {
identityMetadata: azureAD.identityMetadata, // here I use the backend server tenant id
clientID: azureAD.clientID,
issuer: azureAD.issuer, // here I use the backend server tenant id
passReqToCallback: false,
};
const bearerStrategy = new BearerStrategy(options, function(token, done) {
done(null, {}, token);
});
我已经通过应用程序注册在 Azure Active Directory 中创建了我的后端服务器,并在上面创建了命名范围和应用程序 ID。我还将我的 Web 应用程序 clientId 作为授权客户端应用程序列入白名单。
我尝试调用以下路由并收到 401:
app.get(
'/testtest',
cors(),
passport.authenticate('oauth-bearer', { session: false }),
function(req, res) {
var claims = req.authInfo;
console.log('User info: ', req.user);
console.log('Validated claims: ', claims);
res.status(200).json({ name: claims['name'] });
}
);
这是来自我的 vue 网络应用程序的休息电话:
let headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.user.accessToken}`,
'cache-control': 'no-cache'
};
const apiClient = axios.create({
baseURL,
headers
});
apiClient.get('/testtest').then( resp => console.log( resp));
我没有获得访问令牌decoding/encoding。
非常感谢任何支持。谢谢!
正如@Jim Xu 在评论中建议的那样,通过 Azure 门户将 REST api 的应用程序范围添加到客户端应用程序有助于解决问题。但我也使用了错误的令牌。
我现在使用验证函数参数列表中的 param
,而不是使用验证函数参数列表中的 accessToken
。
(iss, sub, profile, access_token, refresh_token, params, done) => {
// access_token did not work
// id_token can be used as an accessToken
user.accessToken = params.id_token;
...
}
我有一个由 express.js 服务器(网络应用程序服务器)提供的网络应用程序。 Web 应用服务器通过 passport.js 和 passport-azure-ad npm 包处理用户登录。为此,我正在使用 OIDCStrategy。
我还通过另一台服务器(rest 后端)托管 REST api。我想通过 passport.js 和使用 BearerStrategy 的 passport-azure-ad npm 包来保护这个后端。为此,我想在 Web 应用程序服务器通行证配置中定义一个范围,以便可以通过 cookie 将 Web 应用程序服务器的 access_token 传递到我的 Web 应用程序,并从那里用于访问其余后端。
我的问题:使用我当前的配置,我在尝试使用 access_token 访问我的后端 api 时收到 401 访问被拒绝。 access_token invalid
是错误信息:{"name":"AzureAD: Bearer Strategy", "msg":"authentication failed due to: error: invalid_token","v":0}
我认为我应该在登录时被重定向到权限页面,但事实并非如此。所以我想我的访问令牌实际上是无效的。
Web 应用服务器通行证配置:
passport.use(
new OIDCStrategy(
{
clientID: credsAzureAD.clientID,
identityMetadata: credsAzureAD.identityMetadata, // here is use the web app tenant id
clientSecret: credsAzureAD.clientSecret,
callbackURL: credsAzureAD.returnURL,
redirectUrl: credsAzureAD.returnURL,
skipUserProfile: true,
responseType: 'code',
responseMode: 'query',
scope: 'api://test/Write.Contributor',
useCookieInsteadOfSession: false,
passReqToCallback: false
},
(issuer, sub, profile, accessToken, refreshToken, done) => {
user.accessToken = accessToken;
return done(null, user);
}
)
);
我尝试使用范围,其中 api://test
是我的 REST API 应用程序 ID uri,/Write.Contributor
是我在 azure 活动目录中定义的范围。
我的 REST 后端服务器护照配置:
const options = {
identityMetadata: azureAD.identityMetadata, // here I use the backend server tenant id
clientID: azureAD.clientID,
issuer: azureAD.issuer, // here I use the backend server tenant id
passReqToCallback: false,
};
const bearerStrategy = new BearerStrategy(options, function(token, done) {
done(null, {}, token);
});
我已经通过应用程序注册在 Azure Active Directory 中创建了我的后端服务器,并在上面创建了命名范围和应用程序 ID。我还将我的 Web 应用程序 clientId 作为授权客户端应用程序列入白名单。
我尝试调用以下路由并收到 401:
app.get(
'/testtest',
cors(),
passport.authenticate('oauth-bearer', { session: false }),
function(req, res) {
var claims = req.authInfo;
console.log('User info: ', req.user);
console.log('Validated claims: ', claims);
res.status(200).json({ name: claims['name'] });
}
);
这是来自我的 vue 网络应用程序的休息电话:
let headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.user.accessToken}`,
'cache-control': 'no-cache'
};
const apiClient = axios.create({
baseURL,
headers
});
apiClient.get('/testtest').then( resp => console.log( resp));
我没有获得访问令牌decoding/encoding。
非常感谢任何支持。谢谢!
正如@Jim Xu 在评论中建议的那样,通过 Azure 门户将 REST api 的应用程序范围添加到客户端应用程序有助于解决问题。但我也使用了错误的令牌。
我现在使用验证函数参数列表中的 param
,而不是使用验证函数参数列表中的 accessToken
。
(iss, sub, profile, access_token, refresh_token, params, done) => {
// access_token did not work
// id_token can be used as an accessToken
user.accessToken = params.id_token;
...
}