使用MSAL.js遍历租户

问题描述 投票:0回答:1

我正在使用MSAL.js来登录并使用Graph API。

我创建的Azure AD应用程序是一个多租户,当与特定租户运行时具有正确的权限,因此我可以确认此工作。

障碍是我希望能够遍历所有租户以获取图形API中可用的任何数据。

我试图简单地更改MSAL连接的配置,但是这失败了,它将在每次调用时使用现有的authority值。

此后,我创建了一个带有forEachtenantid数组以使其循环通过,但这产生了相同的结果。

我创建了一个每次都会调用的注销功能,但这想将您从页面重定向到您的注销地址。

我的代码:

function config_app(tenantid, callback, apiUrl) {
    var applicationConfig = {
        auth: {
            clientId: "XXXX-XXXX-XXXX-XXXX",
            authority: "https://login.microsoftonline.com/" + tenantid,
            redirectUri: "https://my.redirecturi.com/fake"
        },
        cache: {
            cacheLocation: "sessionStorage",
            storeAuthStateInCookie: false
        }
    };
    var msalInstance = new Msal.UserAgentApplication(applicationConfig);
    callback(tenantid, applicationConfig, msalInstance, callMSGraph, apiUrl);
}
function sign_in(tenantid, applicationConfig, msalInstance, callback, apiUrl) {
    var scopes = {
        scopes: ["Organization.Read.All"],
        loginHint: "[email protected]"
    };
    msalInstance.acquireTokenSilent(scopes).then(response => {
        callback(response.accessToken, graphAPICallback, apiUrl);
    }).catch(err => {
    });
}
function callMSGraph(accessToken, callback, apiUrl) {
    console.log("calling ms graph");
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200)
            callback(JSON.parse(this.responseText));
    }
    xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
    xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xmlHttp.send();
}
function graphAPICallback(data) {
    $('#o365res').append(JSON.stringify(data, null, 2));
}
config_app('XXX-XXX-XXX-XXX-XXX', sign_in, 'organization');

我试图将一些租户ID添加到数组中并使用以下命令循环遍历:

var clients = ['XXX-XXX-XXX-XXX-XXX','YYYY-YYYY-YYYY-YYYY'];
clients.forEach(function(e) {
    config_app(e, sign_in, 'organization');
});

[经过大量阅读后,我似乎无法在没有重定向的情况下使用注销功能,我宁愿能够通过authority值简单地更改租户,并始终使用相同的令牌来跳过。

我正在使用MSAL v1.2.0

我希望从租户中提取所有组织数据以在单个页面上显示,然后在以后扩展它以从API中提取安全分数和任何其他有价值的数据。

谢谢

javascript msal msal.js
1个回答
0
投票

与MSAL.js的同类开发人员联系后,我研究了解决方案。

GitHub问题:

My original issue

They said it was related/duplicate of this

但是,我发现通过简化功能并将forceRefresh: true,添加到范围,每次通过都可以更改权限并提取所需的数据。

这是我的职能:

function getData(tenantid,apiUrl) {
    return new Promise(resolve => {
        var applicationConfig = {
            auth: {
                clientId: "AZURE-APP-ID-HERE",
                authority: "https://login.microsoftonline.com/" + tenantid,
                redirectUri: "https://my.redirect.uri/uri"
            },
            cache: {
                cacheLocation: "localStorage",
                storeAuthStateInCookie: false
            }
        };
        var msalInstance = new Msal.UserAgentApplication(applicationConfig);
        var scopes = {
            forceRefresh: true,
            scopes: ["Organization.Read.All"],
            loginHint: "[email protected]"
        };
        msalInstance.acquireTokenSilent(scopes).then(response => {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    var data = JSON.parse(this.responseText);
                    $('#o365res').append(JSON.stringify(data, null, 2));
                    resolve();
                }
            }
            xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
            xmlHttp.setRequestHeader('Authorization', 'Bearer ' + response.accessToken);
            xmlHttp.send();
        }).catch(err => { });
    });
}
function getDataChain(clients) {
    const nextClient = clients.shift();
    if (nextClient) {
        return getData(nextClient,'organization').then(_ => getDataChain(clients))
    } else { return Promise.resolve(); }
}

接着是我的客户端ID数组并调用chain函数:

var clients = ['XXX-XXX-XXX-XXX-XXX','YYY-YYY-YYY-YYY-YYY'];
getDataChain(clients).then(_ => console.log("all finished"));
© www.soinside.com 2019 - 2024. All rights reserved.