Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to loop through tenantid's by changing authority url #1289

Closed
1 task done
kfear27 opened this issue Feb 18, 2020 · 6 comments
Closed
1 task done

How to loop through tenantid's by changing authority url #1289

kfear27 opened this issue Feb 18, 2020 · 6 comments
Assignees
Labels
duplicate Duplicate issue. known-issue Issue is already known and is either being investigated or is already fixed. question Customer is asking for a clarification, use case or information.

Comments

@kfear27
Copy link

kfear27 commented Feb 18, 2020

Please follow the issue template below. Failure to do so will result in a delay in answering your question.

Library

  • msal@1.x.x or @azure/msal@1.x.x

Description

I've created a multi tenant App in Azure AD and can use MSAL+Graph API successfully, however I am not attempting to loop through our tenants to collect information, however am unsure of how to do so.

When creating a loop using forEach it doesn't change authority so the returned data from the first connection is returned each time.

See code:

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: "my@email.com"
    };
    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');

Example Loop:

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

Are there any better ways to be doing is where it actually works?

Thanks

@kfear27 kfear27 added the question Customer is asking for a clarification, use case or information. label Feb 18, 2020
@jasonnutter
Copy link
Contributor

@kfear27 Apologies for the delay in responding. @sameerag @pkanher617 can correct me if I'm wrong, but I believe this falls under multi-account scenarios, which isn't currently supported.

@kfear27
Copy link
Author

kfear27 commented Feb 24, 2020

@jasonnutter, thank you for the response. Our Azure AD App is multi tenant capeable, I would have thought this was sufficient to do what is needed ? We do have the permissions to access multiple tenants but each request requires a full refresh. Is there a workaround which can be used to flush sessionStorage/localStorage before each new request so it requests a new token for the new teant id ?

@jasonnutter jasonnutter added the known-issue Issue is already known and is either being investigated or is already fixed. label Feb 26, 2020
@jasonnutter
Copy link
Contributor

@kfear27 Ah, I think we understand your problem now. The underlying root cause (concurrent requests with the same scope but different authorities) is a known issue (#1225).

For now, a potential workaround is to make each of those requests in serial, instead of doing them in parallel.

@kfear27
Copy link
Author

kfear27 commented Feb 27, 2020

Thanks @jasonnutter - Do you have an ETA on any updates where this can be run in parallel ?

@jasonnutter
Copy link
Contributor

@kfear27 Not yet, but we're working on it. I would follow that issues for updates.

Closing as a duplicate.

@jasonnutter jasonnutter added the duplicate Duplicate issue. label Feb 27, 2020
@kfear27
Copy link
Author

kfear27 commented Feb 28, 2020

Just an FYI I found a solution to this.

Functions:

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: "user@email.com"
        };
        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(); }
}

Client ID array & calling the chaining function:

var clients = ['XXX-XXX-XXX-XXX-XXX','YYY-YYY-YYY-YYY-YYY'];
getDataChain(clients).then(_ => console.log("all finished"));

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate Duplicate issue. known-issue Issue is already known and is either being investigated or is already fixed. question Customer is asking for a clarification, use case or information.
Projects
None yet
Development

No branches or pull requests

3 participants