Skip to content

Commit 8ba6be8

Browse files
authored
Merge pull request #98 from Azure-Samples/basher-2-1
Update sample call graph API to follow Basher and Zero Trust
2 parents 7a1ac45 + 3fb6462 commit 8ba6be8

File tree

19 files changed

+1529
-617
lines changed

19 files changed

+1529
-617
lines changed

2-Authorization-I/1-call-graph/App/authConfig.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,57 @@
55
*/
66
const msalConfig = {
77
auth: {
8-
clientId: "Enter_the_Application_Id_Here", // This is the ONLY mandatory field that you need to supply.
9-
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", // Defaults to "https://login.microsoftonline.com/common"
10-
redirectUri: "/", // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href
8+
clientId: 'Enter_the_Application_Id_Here', // This is the ONLY mandatory field that you need to supply.
9+
authority: 'https://login.microsoftonline.com/Enter_the_Tenant_Id_Here', // Defaults to "https://login.microsoftonline.com/common"
10+
redirectUri: '/', // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href
11+
postLogoutRedirectUri: '/', //Indicates the page to navigate after logout.
12+
clientCapabilities: ['CP1'], // this lets the resource owner know that this client is capable of handling claims challenge.
1113
},
1214
cache: {
13-
cacheLocation: "localStorage", // This configures where your cache will be stored
15+
cacheLocation: 'localStorage', // This configures where your cache will be stored
1416
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
1517
},
18+
system: {
19+
/**
20+
* Below you can configure MSAL.js logs. For more information, visit:
21+
* https://docs.microsoft.com/azure/active-directory/develop/msal-logging-js
22+
*/
23+
loggerOptions: {
24+
loggerCallback: (level, message, containsPii) => {
25+
if (containsPii) {
26+
return;
27+
}
28+
switch (level) {
29+
case msal.LogLevel.Error:
30+
console.error(message);
31+
return;
32+
case msal.LogLevel.Info:
33+
console.info(message);
34+
return;
35+
case msal.LogLevel.Verbose:
36+
console.debug(message);
37+
return;
38+
case msal.LogLevel.Warning:
39+
console.warn(message);
40+
return;
41+
default:
42+
return;
43+
}
44+
},
45+
},
46+
},
1647
};
1748

1849
// Add here the endpoints for MS Graph API services you would like to use.
1950
const graphConfig = {
2051
graphMeEndpoint: {
21-
uri: "https://graph.microsoft.com/v1.0/me",
22-
scopes: ["User.Read"]
52+
uri: 'https://graph.microsoft.com/v1.0/me',
53+
scopes: ['User.Read'],
54+
},
55+
graphContactsEndpoint: {
56+
uri: 'https://graph.microsoft.com/v1.0/me/contacts',
57+
scopes: ['Contacts.Read'],
2358
},
24-
graphMailEndpoint: {
25-
uri: "https://graph.microsoft.com/v1.0/me/messages",
26-
scopes: ["Mail.Read"]
27-
}
2859
};
2960

3061
/**
@@ -41,5 +72,6 @@ const loginRequest = {
4172
if (typeof exports !== 'undefined') {
4273
module.exports = {
4374
msalConfig: msalConfig,
75+
graphConfig: graphConfig
4476
};
4577
}

2-Authorization-I/1-call-graph/App/authPopup.js

Lines changed: 98 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,152 @@
22
// configuration parameters are located at authConfig.js
33
const myMSALObj = new msal.PublicClientApplication(msalConfig);
44

5-
let username = "";
5+
let username = '';
6+
7+
/**
8+
* This method adds an event callback function to the MSAL object
9+
* to handle the response from redirect flow. For more information, visit:
10+
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/events.md
11+
*/
12+
myMSALObj.addEventCallback((event) => {
13+
if (
14+
(event.eventType === msal.EventType.LOGIN_SUCCESS ||
15+
event.eventType === msal.EventType.ACQUIRE_TOKEN_SUCCESS) &&
16+
event.payload.account
17+
) {
18+
const account = event.payload.account;
19+
myMSALObj.setActiveAccount(account);
20+
}
621

7-
function selectAccount() {
22+
if (event.eventType === msal.EventType.LOGOUT_SUCCESS) {
23+
if (myMSALObj.getAllAccounts().length > 0) {
24+
myMSALObj.setActiveAccount(myMSALObj.getAllAccounts()[0]);
25+
}
26+
}
27+
});
828

29+
function selectAccount() {
930
/**
10-
* See here for more info on account retrieval:
31+
* See here for more info on account retrieval:
1132
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
1233
*/
13-
1434
const currentAccounts = myMSALObj.getAllAccounts();
15-
1635
if (currentAccounts === null) {
1736
return;
18-
} else if (currentAccounts.length > 1) {
37+
} else if (currentAccounts.length >= 1) {
1938
// Add choose account code here
20-
console.warn("Multiple accounts detected.");
21-
} else if (currentAccounts.length === 1) {
22-
username = currentAccounts[0].username;
23-
showWelcomeMessage(username);
39+
username = myMSALObj.getActiveAccount().username;
40+
showWelcomeMessage(username, currentAccounts);
2441
}
2542
}
2643

27-
function handleResponse(response) {
44+
async function addAnotherAccount(event) {
45+
if (event.target.innerHTML.includes('@')) {
46+
const username = event.target.innerHTML;
47+
const account = myMSALObj.getAllAccounts().find((account) => account.username === username);
48+
const activeAccount = myMSALObj.getActiveAccount();
49+
if (account && activeAccount.homeAccountId != account.homeAccountId) {
50+
try {
51+
myMSALObj.setActiveAccount(account);
52+
let res = await myMSALObj.ssoSilent({
53+
...loginRequest,
54+
account: account,
55+
});
56+
closeModal();
57+
handleResponse(res);
58+
window.location.reload();
59+
} catch (error) {
60+
if (error instanceof msal.InteractionRequiredAuthError) {
61+
let res = await myMSALObj.loginPopup({
62+
...loginRequest,
63+
prompt: 'login',
64+
});
65+
handleResponse(res);
66+
window.location.reload();
67+
}
68+
}
69+
} else {
70+
closeModal();
71+
}
72+
} else {
73+
try {
74+
myMSALObj.setActiveAccount(null);
75+
const res = await myMSALObj.loginPopup({
76+
...loginRequest,
77+
prompt: 'login',
78+
});
79+
handleResponse(res);
80+
closeModal();
81+
window.location.reload();
82+
} catch (error) {
83+
console.log(error);
84+
}
85+
}
86+
}
2887

88+
function handleResponse(response) {
2989
/**
3090
* To see the full list of response object properties, visit:
3191
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
3292
*/
3393

3494
if (response !== null) {
95+
const accounts = myMSALObj.getAllAccounts();
3596
username = response.account.username;
36-
showWelcomeMessage(username);
97+
showWelcomeMessage(username, accounts);
3798
} else {
3899
selectAccount();
39100
}
40101
}
41102

42103
function signIn() {
43-
44104
/**
45105
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
46106
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
47107
*/
48108

49-
myMSALObj.loginPopup(loginRequest)
109+
myMSALObj
110+
.loginPopup(loginRequest)
50111
.then(handleResponse)
51-
.catch(error => {
112+
.catch((error) => {
52113
console.error(error);
53114
});
54115
}
55116

56117
function signOut() {
57-
58118
/**
59119
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
60120
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
61121
*/
122+
const account = myMSALObj.getAccountByUsername(username);
62123
const logoutRequest = {
63-
account: myMSALObj.getAccountByUsername(username)
124+
account: account,
125+
mainWindowRedirectUri: '/',
64126
};
65-
66-
myMSALObj.logout(logoutRequest);
127+
clearStorage(account);
128+
myMSALObj.logoutPopup(logoutRequest).catch((error) => {
129+
console.log(error);
130+
});
67131
}
68132

69133
function seeProfile() {
70-
71-
getGraphClient({
72-
account: myMSALObj.getAccountByUsername(username),
73-
scopes: graphConfig.graphMeEndpoint.scopes,
74-
interactionType: msal.InteractionType.Popup
75-
}).api('/me').get()
76-
.then((response) => {
77-
return updateUI(response, graphConfig.graphMeEndpoint.uri);
78-
}).catch((error) => {
79-
console.log(error);
80-
});
134+
callGraph(
135+
username,
136+
graphConfig.graphMeEndpoint.scopes,
137+
graphConfig.graphMeEndpoint.uri,
138+
msal.InteractionType.Popup,
139+
myMSALObj
140+
);
81141
}
82142

83-
function readMail() {
84-
85-
getGraphClient({
86-
account: myMSALObj.getAccountByUsername(username),
87-
scopes: graphConfig.graphMailEndpoint.scopes,
88-
interactionType: msal.InteractionType.Popup
89-
}).api('/me/messages').get()
90-
.then((response) => {
91-
return updateUI(response, graphConfig.graphMailEndpoint.uri);
92-
}).catch((error) => {
93-
console.log(error);
94-
});
143+
function readContacts() {
144+
callGraph(
145+
username,
146+
graphConfig.graphContactsEndpoint.scopes,
147+
graphConfig.graphContactsEndpoint.uri,
148+
msal.InteractionType.Popup,
149+
myMSALObj
150+
);
95151
}
96152

97153
selectAccount();

0 commit comments

Comments
 (0)