Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d6a444f
Update eslint settings
tnorling Jun 11, 2020
02ea320
Add b2c sample
tnorling Jun 11, 2020
6d0f45c
Add login tests
tnorling Jun 11, 2020
e64aa03
Add acquireToken tests
tnorling Jun 12, 2020
d8a81f0
Merge branch 'dev' into b2c-E2E-Sample
tnorling Jun 12, 2020
7452390
Add E2E for 2.0
tnorling Jun 12, 2020
34c8c89
Merge branch 'b2c-E2E-Sample' of https://github.com/AzureAD/microsoft…
tnorling Jun 12, 2020
d48b188
Remove telemetry config from 2.0 sample
tnorling Jun 12, 2020
7055edb
Verify contents of cache, not quantity
tnorling Jun 12, 2020
eb57298
Verify contents of cache, not quantity
tnorling Jun 13, 2020
453d54f
More accurately confirm successful login before moving on
tnorling Jun 16, 2020
b3a7c3f
Merge branch 'dev' into b2c-E2E-Sample
tnorling Jun 16, 2020
70673b7
Remove telemetry config
tnorling Jun 17, 2020
d412d92
Merge branch 'dev' into b2c-E2E-Sample
tnorling Jun 17, 2020
d26a6e7
Setup tests to allow for easier lab user selection
tnorling Jun 17, 2020
2bd295b
Setup tests to allow for easier lab user selection
tnorling Jun 17, 2020
c05dfd8
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Jun 19, 2020
bd13710
Update sample
tnorling Jun 19, 2020
e8348ca
Update account logic
tnorling Jun 19, 2020
d843501
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Jul 6, 2020
1f04005
Update browser sample
tnorling Jul 6, 2020
ab946b5
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Jul 17, 2020
e7a96fb
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Jul 27, 2020
5491e4b
Update E2E tests
tnorling Jul 27, 2020
f30c209
Remove 2.0 sample
tnorling Aug 10, 2020
4f2fdba
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Aug 10, 2020
e0bc92e
Revert changes to 2.0 test utils
tnorling Aug 10, 2020
5385d38
Revert changes to 2.0 test utils
tnorling Aug 10, 2020
9b61568
Merge branch 'dev' into b2c-E2E-Sample
tnorling Aug 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update browser sample
  • Loading branch information
tnorling committed Jul 6, 2020
commit 1f04005a9d1ee847049e6928d4c0525af50258bb
110 changes: 44 additions & 66 deletions samples/msal-browser-samples/VanillaJSTestApp2.0/app/b2c/auth.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,71 @@
// Browser check variables
// If you support IE, our recommendation is that you sign-in using Redirect APIs
// If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
const ua = window.navigator.userAgent;
const msie = ua.indexOf("MSIE ");
const msie11 = ua.indexOf("Trident/");
const msedge = ua.indexOf("Edge/");
const isIE = msie > 0 || msie11 > 0;
const isEdge = msedge > 0;

let signInType;
let username = "";

// Create the main myMSALObj instance
// configuration parameters are located at authConfig.js
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
const myMSALObj = new msal.PublicClientApplication(msalConfig);

// Register Callbacks for Redirect flow
myMSALObj.handleRedirectCallback(authRedirectCallBack);
myMSALObj.handleRedirectPromise().then(response => {
if (response) {
handleResponse(response);
}
}).catch(error => {
console.log(error);
});

function authRedirectCallBack(error, response) {
if (error) {
console.log(error);
function handleResponse(response) {
if (response !== null) {
username = resp.account.username;
showWelcomeMessage(response.account);
updateUI(response);
} else {
if (response.tokenType === "id_token" && myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
console.log('id_token acquired at: ' + new Date().toString());
showWelcomeMessage(myMSALObj.getAccount());
} else if (response.tokenType === "access_token") {
console.log('access_token acquired at: ' + new Date().toString());
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
} else {
console.log("token type is:" + response.tokenType);
// need to call getAccount here?
const currentAccounts = myMSALObj.getAllAccounts();
if (currentAccounts === null) {
return;
} else if (currentAccounts.length > 1) {
// Add choose account code here
} else if (currentAccounts.length === 1) {
username = currentAccounts[0].username;
showWelcomeMessage(currentAccounts[0]);
}
}
}
}

// Redirect: once login is successful and redirects with tokens, call Graph API
if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
// avoid duplicate code execution on page load in case of iframe and Popup window.
showWelcomeMessage(myMSALObj.getAccount());
}

function signIn(method) {
signInType = isIE ? "loginRedirect" : method;
if (signInType === "loginPopup") {
myMSALObj.loginPopup(loginRequest)
.then(loginResponse => {
console.log(loginResponse);
if (myMSALObj.getAccount()) {
showWelcomeMessage(myMSALObj.getAccount());
}
}).catch(function (error) {
async function signIn(method) {
if (method === "loginPopup") {
await myMSALObj.loginPopup().then(handleResponse).catch(function (error) {
console.log(error);
});
} else if (signInType === "loginRedirect") {
myMSALObj.loginRedirect(loginRequest)
} else if (method === "loginRedirect") {
myMSALObj.loginRedirect();
}
}

function signOut() {
myMSALObj.logout();
const currentAcc = account;
myMSALObj.logout(currentAcc);
}

function getAccessTokenPopup() {
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenPopup(loginRequest).then(response => {
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
}).catch(error => {
console.log(error);
});
}
request = tokenRequest
request.account = myMSALObj.getAccountByUsername(username);
myMSALObj.acquireTokenPopup(request).then(handleResponse).catch(error => {
console.log(error);
});
}

function getAccessTokenRedirect() {
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenRedirect(loginRequest);
}
request = tokenRequest
request.account = myMSALObj.getAccountByUsername(username);
myMSALObj.acquireTokenRedirect(request);
}

function getAccessTokenSilent() {
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenSilent(loginRequest).then(response => {
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
}).catch(error => {
console.log(error);
})
}
request = tokenRequest
request.account = myMSALObj.getAccountByUsername(username);
myMSALObj.acquireTokenSilent(request).then(handleResponse).catch(error => {
console.log(error);
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const msalConfig = {
};

// Add here scopes for id token to be used at MS Identity Platform endpoints.
const loginRequest = {
const tokenRequest = {
scopes: ["https://msidlabb2c.onmicrosoft.com/msidlabb2capi/read"],
forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!-- IE support: add promises polyfill before msal.js -->
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/js/browser/bluebird.min.js"></script>
<script src="./dist/msal.js"></script>
<script src="../lib/msal-browser.js"></script>

<!-- adding Bootstrap 4 for UI components -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ describe("Browser tests", function () {
});

it("Test acquireTokenSilent", async () => {
await page.click("#getAccessTokenPopup");
await page.waitForSelector("#access-token-info");
await page.reload();

await page.waitForSelector("#getAccessTokenSilent");
await page.click("#getAccessTokenSilent");
await page.waitForSelector("#access-token-info");
await takeScreenshot(page, testName, "accessTokenAcquiredSilently");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ function updateUI(response) {
scopes.innerHTML = "<strong>Access Token Acquired for Scopes: </strong>" + response.scopes;

accessTokenDiv.appendChild(scopes);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
}
99 changes: 48 additions & 51 deletions samples/msal-core-samples/VanillaJSTestApp/app/b2c/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,82 @@ let signInType;

// Create the main myMSALObj instance
// configuration parameters are located at authConfig.js
const myMSALObj = new msal.PublicClientApplication(msalConfig);
const myMSALObj = new Msal.UserAgentApplication(msalConfig);

// Register Callbacks for Redirect flow
myMSALObj.handleRedirectPromise().then(response => {
if (response) {
handleResponse(response);
}
}).catch(error => {
console.log(error);
});
myMSALObj.handleRedirectCallback(authRedirectCallBack);

function handleResponse(response) {
if (response !== null) {
showWelcomeMessage(response.account);
function authRedirectCallBack(error, response) {
if (error) {
console.log(error);
} else {
// need to call getAccount here?
const currentAccounts = myMSALObj.getAllAccounts();
if (currentAccounts === null) {
return;
} else if (currentAccounts.length > 1) {
// Add choose account code here
} else if (currentAccounts.length === 1) {
showWelcomeMessage(currentAccounts[0]);
if (response.tokenType === "id_token" && myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
console.log('id_token acquired at: ' + new Date().toString());
showWelcomeMessage(myMSALObj.getAccount());
} else if (response.tokenType === "access_token") {
console.log('access_token acquired at: ' + new Date().toString());
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
} else {
console.log("token type is:" + response.tokenType);
}
}

if (response.tokenType === "Bearer") {
console.log('access_token acquired at: ' + new Date().toString());
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
} else {
console.log("token type is:" + response.tokenType);
}
}

// Redirect: once login is successful and redirects with tokens, call Graph API
let currentAccounts = myMSALObj.getAllAccounts();
let account = ""
if (currentAccounts.length === 1) {
if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
// avoid duplicate code execution on page load in case of iframe and Popup window.
account = currentAccounts[0];
showWelcomeMessage(account);
showWelcomeMessage(myMSALObj.getAccount());
}

async function signIn(method) {
function signIn(method) {
signInType = isIE ? "loginRedirect" : method;
if (signInType === "loginPopup") {
const loginResponse = await myMSALObj.loginPopup(loginRequest).then(handleResponse).catch(function (error) {
myMSALObj.loginPopup(loginRequest)
.then(loginResponse => {
console.log(loginResponse);
if (myMSALObj.getAccount()) {
showWelcomeMessage(myMSALObj.getAccount());
}
}).catch(function (error) {
console.log(error);
});
} else if (signInType === "loginRedirect") {
myMSALObj.loginRedirect(loginRequest);
myMSALObj.loginRedirect(loginRequest)
}
}

function signOut() {
const currentAcc = account;
myMSALObj.logout(currentAcc);
myMSALObj.logout();
}

function getAccessTokenPopup() {
request = loginRequest
request.account = account;
myMSALObj.acquireTokenPopup(request).then(handleResponse).catch(error => {
console.log(error);
});
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenPopup(loginRequest).then(response => {
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
}).catch(error => {
console.log(error);
});
}
}

function getAccessTokenRedirect() {
request = loginRequest
request.account = account;
myMSALObj.acquireTokenRedirect(request);
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenRedirect(loginRequest);
}
}

function getAccessTokenSilent() {
request = loginRequest
request.account = account;
myMSALObj.acquireTokenSilent(request).then(handleResponse).catch(error => {
console.log(error);
})
if (myMSALObj.getAccount()) {
myMSALObj.acquireTokenSilent(loginRequest).then(response => {
updateUI(response);
accessTokenButtonPopup.style.display = 'none';
accessTokenButtonRedirect.style.display = 'none';
}).catch(error => {
console.log(error);
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!-- IE support: add promises polyfill before msal.js -->
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/js/browser/bluebird.min.js"></script>
<script src="../lib/msal-browser.js"></script>
<script src="./dist/msal.js"></script>

<!-- adding Bootstrap 4 for UI components -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ describe("Browser tests", function () {
});

it("Test acquireTokenSilent", async () => {
await page.click("#getAccessTokenPopup");
await page.waitForSelector("#access-token-info");
await page.reload();

await page.waitForSelector("#getAccessTokenSilent");
await page.click("#getAccessTokenSilent");
await page.waitForSelector("#access-token-info");
await takeScreenshot(page, testName, "accessTokenAcquiredSilently");
Expand Down