有没有办法使用像 Passport 这样的库在 NodeJS 中实现 Azure AD on-behalf-of (OBO) 流程?

Is there a way to implement the Azure AD on-behalf-of (OBO) flow in NodeJS using a library like Passport?

我找到了有关如何使用 passport-azure-ad 保护 NodeJS 中的 WebAPI 的示例(例如,使用 BearerStrategy),但我还没有找到任何实施 OBO 流程的示例特别是在 NodeJS 中。

在我的情况下,我有一个客户端应用程序在授权 header 中将不记名身份验证令牌发送到我的 NodeJS 服务。如果我的理解是正确的,如果我想让我的 NodeJS 服务以用户身份调用 MS Graph API,我必须将令牌换成另一个令牌作为 OBO 流程的一部分。

在我为使用 .NET 的服务找到的示例中,有一个用于此目的的库(并且您使用 Bearer 令牌作为断言调用类似 AcquireTokenAsync 的东西)。如果服务是 NodeJS 而不是 .NET,是否应该使用类似的库?

我知道可以通过发出 HTTP requests directly 来完成,我只是不知道这是否是在 NodeJS 中的 preferred/only 方法。

谢谢!

其实adal-node包是不支持代流的。要实现它,我们必须进行新的 HTTP 调用并在请求中传递断言。我建议您读取服务中的传入令牌,并使用断言对 (https://login.microsoftonline.com/b2bc09c8-9386-47b1-8aexxxxxxxxxx/oauth2/token) 端点进行新的 http 调用以获取 MS Graph API.

的令牌

下面是使用postman代流获取token的截图

下面是node.js

中使用代流获取访问令牌的代码

var qs = require("querystring");

var http = require("https");

var options = { "method": "POST", "hostname": [ "login", "microsoftonline", "com" ], "path": [ "b2bc09c8-9386-47xxxxxxxx", "oauth2", "token" ], "headers": { "Content-Type": "application/x-www-form-urlencoded", "cache-control": "no-cache", "Postman-Token": "739540c9-1e3d-4d74-bxxxxxxx" } };

var req = http.request(options, function (res) { 变量块 = [];

res.on("data", function (chunk) {
    chunks.push(chunk);
});

res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
});

});

req.write(qs.stringify({ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', client_id: 'd1538209-a56f-4301-863dxxxxxxxxxxxxx', 资源:'https://graph.microsoft.com/', client_secret: 'NITLQThsHDlPb0FR+8oXxxxxxxxxxxxxxxxxxxx', 范围:'openid', 断言:'incoming access token from native app', requested_token_use: 'on_behalf_of', 未定义:未定义 })); req.end();

您可以提取访问令牌并将其用于不记名请求中的资源。 希望这能解决您的问题。