如何使用passport和passport-azure-ad保护azure函数我不想保护所有的azure函数,有些函数需要在运行代码之前验证access_token。我在网上找到的大多数示例都在使用express,它不能在azure函数中使用。
我尝试了下面的方法,但是代码仍然没有经过身份验证就可以运行。
const passport = require("passport");
const config = require("../shared/config");
const createMongoClient = require("../shared/mongo");
const BearerStrategy = require("passport-azure-ad").BearerStrategy;
const bearerStrategy = new BearerStrategy(config, function (token, done) {
// Send user info using the second argument
done(null, {}, token);
});
passport.use(bearerStrategy);
passport.initialize();
module.exports = async function (context, req) {
passport.authenticate("oauth-bearer", { session: false });
const dish = req.body || {};
if (dish) {
context.res = {
status: 400,
body: "Dish data is required! ",
};
}
const { db, connection } = await createMongoClient();
const Dishes = db.collection("dishes");
try {
const dishes = await Dishes.insertOne(dish);
connection.close();
context.res = {
status: 201,
body: dishes.ops[0],
};
} catch (error) {
context.res = {
status: 500,
body: "Error creating a new Dish",
};
}
};发布于 2020-09-07 16:14:55
由于Azure功能不是由您管理的(您只需提供代码),因此身份验证在另一个地方发生,并且可以通过门户进行配置。
如果只需要保护一些功能的,你最好的选择是将这些功能划分为两个不同的功能应用程序。一个会受到保护,而另一个则不会。
以下是身份验证的官方文档:https://docs.microsoft.com/en-us/azure/app-service/overview-authentication-authorization
在我看来,当使用Linux作为操作系统时没有启用身份验证,所以您的部署必须是在Windows 或上,您可以使用Azure API Management。
我有一个在门户中创建带身份验证的Azure函数的示例。代码本身是用.Net编写的,但是门户中的配置应该完全相同。
https://www.pshul.com/2020/02/07/azure-ad-authentication-from-angular-to-azure-functions/
发布于 2020-11-12 08:55:16
https://stackoverflow.com/questions/63767777
复制相似问题