Skip to content

Commit e0159f8

Browse files
authored
Merge branch 'dev' into custom-domain-msal-core
2 parents 68cbeb4 + 532a9a4 commit e0159f8

File tree

19 files changed

+234
-173
lines changed

19 files changed

+234
-173
lines changed

lib/msal-browser/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type { AuthCallback } from "./types/AuthCallback";
1111

1212
// Common Object Formats
1313
export {
14+
// Account
15+
AccountInfo,
1416
// Request
1517
AuthorizationUrlRequest,
1618
SilentFlowRequest,

lib/msal-browser/src/utils/BrowserConstants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const BrowserConstants = {
3333
export enum HTTP_REQUEST_TYPE {
3434
GET = "GET",
3535
POST = "POST"
36-
};
36+
}
3737

3838
/**
3939
* Temporary cache keys for MSAL, deleted after any request.
@@ -49,4 +49,4 @@ export enum TemporaryCacheKeys {
4949
URL_HASH = "urlHash",
5050
REQUEST_PARAMS = "request.params",
5151
SCOPES = "scopes"
52-
};
52+
}

samples/VanillaJSTestApp2.0/app/default/auth.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
2222

2323
function handleResponse(resp) {
2424
if (resp !== null) {
25+
username = resp.account.username;
2526
showWelcomeMessage(resp.account);
2627
} else {
2728
// need to call getAccount here?
@@ -31,6 +32,7 @@ function handleResponse(resp) {
3132
} else if (currentAccounts.length > 1) {
3233
// Add choose account code here
3334
} else if (currentAccounts.length === 1) {
35+
username = currentAccounts[0].username;
3436
showWelcomeMessage(currentAccounts[0]);
3537
}
3638
}
@@ -51,6 +53,7 @@ function signOut() {
5153
const logoutRequest = {
5254
account: myMSALObj.getAccountByUsername(username)
5355
};
56+
5457
myMSALObj.logout(logoutRequest);
5558
}
5659

@@ -83,4 +86,3 @@ async function getTokenRedirect(request, account) {
8386
}
8487
});
8588
}
86-

samples/VanillaJSTestApp2.0/app/default/authConfig.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const msalConfig = {
1212

1313
// Add here scopes for id token to be used at MS Identity Platform endpoints.
1414
const loginRequest = {
15-
scopes: ["User.Read"]
15+
scopes: ["User.Read"],
16+
redirectUri: "http://localhost:30662/"
1617
};
1718

1819
// Add here the endpoints for MS Graph API services you would like to use.
@@ -24,9 +25,11 @@ const graphConfig = {
2425
// Add here scopes for access token to be used at MS Graph API endpoints.
2526
const tokenRequest = {
2627
scopes: ["Mail.Read"],
28+
redirectUri: "http://localhost:30662/",
2729
forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
2830
};
2931

3032
const silentRequest = {
31-
scopes: ["3fba556e-5d4a-48e3-8e1a-fd57c12cb82e", "User.Read", "Mail.Read"]
33+
scopes: ["openid", "profile", "User.Read", "Mail.Read"],
34+
redirectUri: "http://localhost:30662/"
3235
};

samples/VanillaJSTestApp2.0/app/default/graph.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,25 @@ async function readMail() {
4040
mailButton.style.display = 'none';
4141
}
4242
}
43+
44+
async function seeProfileRedirect() {
45+
const currentAcc = myMSALObj.getAccountByUsername(username);
46+
if (currentAcc) {
47+
const response = await getTokenRedirect(loginRequest, currentAcc).catch(error => {
48+
console.log(error);
49+
});
50+
callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, updateUI);
51+
profileButton.style.display = 'none';
52+
}
53+
}
54+
55+
async function readMailRedirect() {
56+
const currentAcc = myMSALObj.getAccountByUsername(username);
57+
if (currentAcc) {
58+
const response = await getTokenRedirect(tokenRequest, currentAcc).catch(error => {
59+
console.log(error);
60+
});
61+
callMSGraph(graphConfig.graphMailEndpoint, response.accessToken, updateUI);
62+
mailButton.style.display = 'none';
63+
}
64+
}

samples/VanillaJSTestApp2.0/app/default/ui.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const profileDiv = document.getElementById("profile-div");
99
function showWelcomeMessage(account) {
1010
// Reconfiguring DOM elements
1111
cardDiv.style.display = 'initial';
12-
username = account.username;
1312
welcomeDiv.innerHTML = `Welcome ${username}`;
1413
signInButton.nextElementSibling.style.display = 'none';
1514
signInButton.setAttribute("onclick", "signOut();");

samples/VanillaJSTestApp2.0/app/multipleResources/auth.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,58 @@ const isIE = msie > 0 || msie11 > 0;
99
const isEdge = msedge > 0;
1010

1111
let signInType;
12+
let username = "";
1213

1314
// Create the main myMSALObj instance
1415
// configuration parameters are located at authConfig.js
1516
const myMSALObj = new msal.PublicClientApplication(msalConfig);
1617

17-
// Register Callbacks for Redirect flow
18-
myMSALObj.handleRedirectCallback(authRedirectCallBack);
18+
// Redirect: once login is successful and redirects with tokens, call Graph API
19+
myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
20+
console.error(err);
21+
});
1922

20-
function authRedirectCallBack(error, response) {
21-
if (error) {
22-
console.log(error);
23+
// Redirect: once login is successful and redirects with tokens, call Graph API
24+
function handleResponse(resp) {
25+
if (resp !== null) {
26+
username = resp.account.username;
27+
showWelcomeMessage(resp.account);
28+
getTokenRedirect(tokenRequest, resp.account);
2329
} else {
24-
if (myMSALObj.getAccount()) {
25-
console.log('id_token acquired at: ' + new Date().toString());
26-
showWelcomeMessage(myMSALObj.getAccount());
27-
getTokenRedirect(loginRequest);
28-
} else if (response.tokenType === "Bearer") {
29-
console.log('access_token acquired at: ' + new Date().toString());
30-
} else {
31-
console.log("token type is:" + response.tokenType);
30+
// need to call getAccount here?
31+
const currentAccounts = myMSALObj.getAllAccounts();
32+
if (currentAccounts === null) {
33+
return;
34+
} else if (currentAccounts.length > 1) {
35+
// Add choose account code here
36+
} else if (currentAccounts.length === 1) {
37+
username = currentAccounts[0].username;
38+
showWelcomeMessage(currentAccounts[0]);
39+
getTokenRedirect(tokenRequest, currentAccounts[0]);
3240
}
3341
}
3442
}
3543

36-
// Redirect: once login is successful and redirects with tokens, call Graph API
37-
if (myMSALObj.getAccount()) {
38-
// avoid duplicate code execution on page load in case of iframe and Popup window.
39-
showWelcomeMessage(myMSALObj.getAccount());
40-
}
41-
4244
async function signIn(method) {
4345
signInType = isIE ? "loginRedirect" : method;
4446
if (signInType === "loginPopup") {
45-
const loginResponse = await myMSALObj.loginPopup(loginRequest).catch(function (error) {
47+
return myMSALObj.loginPopup(loginRequest).then(handleResponse).catch(function (error) {
4648
console.log(error);
4749
});
48-
console.log(loginResponse);
49-
if (myMSALObj.getAccount()) {
50-
showWelcomeMessage(myMSALObj.getAccount());
51-
}
5250
} else if (signInType === "loginRedirect") {
53-
myMSALObj.loginRedirect(loginRequest)
51+
return myMSALObj.loginRedirect(loginRequest)
5452
}
5553
}
5654

5755
function signOut() {
58-
myMSALObj.logout();
56+
const logoutRequest = {
57+
account: myMSALObj.getAccountByUsername(username)
58+
};
59+
myMSALObj.logout(logoutRequest);
5960
}
6061

61-
async function getTokenPopup(request) {
62+
async function getTokenPopup(request, account) {
63+
request.account = account;
6264
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
6365
console.log("silent token acquisition fails.");
6466
if (error instanceof msal.InteractionRequiredAuthError) {
@@ -67,22 +69,21 @@ async function getTokenPopup(request) {
6769
console.error(error);
6870
});
6971
} else {
70-
return myMSALObj.acquireTokenPopup(request).catch(error => {
71-
console.error(error);
72-
});
72+
console.error(error);
7373
}
7474
});
7575
}
7676

7777
// This function can be removed if you do not need to support IE
78-
async function getTokenRedirect(request) {
78+
async function getTokenRedirect(request, account) {
79+
request.account = account;
7980
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
8081
console.log("silent token acquisition fails.");
8182
if (error instanceof msal.InteractionRequiredAuthError) {
83+
// fallback to interaction when silent call fails
8284
console.log("acquiring token using redirect");
8385
myMSALObj.acquireTokenRedirect(request);
8486
} else {
85-
myMSALObj.acquireTokenRedirect(request);
8687
console.error(error);
8788
}
8889
});

samples/VanillaJSTestApp2.0/app/multipleResources/authConfig.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ const msalConfig = {
1212

1313
// Add here scopes for id token to be used at MS Identity Platform endpoints.
1414
const loginRequest = {
15-
scopes: ["User.Read"]
15+
scopes: ["User.Read", "https://msidlab0.spoppe.com/user.read"],
16+
redirectUri: "http://localhost:30662/"
1617
};
1718

1819
// Add here the endpoints for MS Graph API services you would like to use.
1920
const graphConfig = {
20-
graphMeEndpoint: "https://graph.microsoft-ppe.com/v1.0/me"
21+
graphMeEndpoint: "https://graph.microsoft-ppe.com/v1.0/me",
22+
graphMailEndpoint: "https://graph.microsoft-ppe.com/v1.0/me/messages"
2123
};
2224

2325
// Add here scopes for access token to be used at MS Graph API endpoints.
2426
const tokenRequest = {
2527
scopes: ["https://msidlab0.spoppe.com/User.Read"],
28+
redirectUri: "http://localhost:30662/",
2629
forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
27-
};
30+
};
31+
32+
const silentRequest = {
33+
scopes: ["openid", "profile", "User.Read", "Mail.Read"],
34+
redirectUri: "http://localhost:30662/"
35+
};

samples/VanillaJSTestApp2.0/app/multipleResources/graph.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ function callMSGraph(endpoint, accessToken, callback) {
2020
}
2121

2222
async function seeProfile() {
23-
if (myMSALObj.getAccount()) {
24-
const response = await getTokenPopup(loginRequest).catch(error => {
23+
const currentAcc = myMSALObj.getAccountByUsername(username);
24+
if (currentAcc) {
25+
const response = await getTokenPopup(loginRequest, currentAcc).catch(error => {
2526
console.log(error);
2627
});
2728
callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, updateUI);
@@ -30,8 +31,9 @@ async function seeProfile() {
3031
}
3132

3233
async function acquireSecondToken() {
33-
if (myMSALObj.getAccount()) {
34-
const response = await getTokenPopup(tokenRequest).catch(error => {
34+
const currentAcc = myMSALObj.getAccountByUsername(username);
35+
if (currentAcc) {
36+
const response = await getTokenPopup(tokenRequest, currentAcc).catch(error => {
3537
console.log(error);
3638
});
3739
updateUI("Second Token Acquired", "")

samples/VanillaJSTestApp2.0/app/multipleResources/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const profileDiv = document.getElementById("profile-div");
99
function showWelcomeMessage(account) {
1010
// Reconfiguring DOM elements
1111
cardDiv.style.display = 'initial';
12-
welcomeDiv.innerHTML = `Welcome ${account.name}`;
12+
welcomeDiv.innerHTML = `Welcome ${account.username}`;
1313
signInButton.nextElementSibling.style.display = 'none';
1414
signInButton.setAttribute("onclick", "signOut();");
1515
signInButton.setAttribute('class', "btn btn-success")

0 commit comments

Comments
 (0)