Skip to content

Commit 767687e

Browse files
Merge pull request #279 from Azure-Samples/srahaman/move-auth-config-to-server
Move auth config to server side
2 parents d3b1963 + 6ec8ebb commit 767687e

File tree

3 files changed

+65
-23
lines changed

3 files changed

+65
-23
lines changed

Project/oAuthConfig.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
const authConfig = {
2-
auth: {
3-
clientId: 'ENTER_CLIENT_ID',
4-
authority: 'https://login.microsoftonline.com/ENTER_TENANT_ID'
2+
configuration: {
3+
auth: {
4+
clientId: 'ENTER_CLIENT_ID',
5+
authority: 'https://login.microsoftonline.com/common'
6+
}
7+
},
8+
scopes: {
9+
m365Login: [
10+
"https://auth.msft.communication.azure.com/.default"
11+
],
12+
popUpLogin: [
13+
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
14+
"https://auth.msft.communication.azure.com/Teams.ManageChats"
15+
]
516
}
617
};
7-
// Add here scopes for id token to be used at MS Identity Platform endpoints.
8-
const authScopes = {
9-
popUpLogin: [],
10-
m365Login: []
11-
};
1218

1319
const entraCredentialConfig = {
1420
tenantId: 'ENTER_TENANT_ID',
1521
clientId: 'ENTER_CLIENT_ID',
1622
resourceEndpoint: 'ACS_RESOURCE_ENDPOINT' // e.g., 'https://contoso.unitedstates.communication.azure.com/'
1723
};
1824

19-
module.exports = {authConfig, authScopes, entraCredentialConfig }
25+
module.exports = { authConfig, entraCredentialConfig }

Project/src/Utils/Utils.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@azure/communication-common';
88
import { InteractiveBrowserCredential } from '@azure/identity';
99
import { PublicClientApplication } from "@azure/msal-browser";
10-
import { authConfig, authScopes } from "../../oAuthConfig"
1110
import axios from 'axios';
1211

1312
export const utils = {
@@ -64,8 +63,40 @@ export const utils = {
6463
throw new Error('Failed to get ACS User Acccess token for the given OneSignal Registration Token');
6564
},
6665
teamsPopupLogin: async () => {
67-
const oAuthObj = new PublicClientApplication(authConfig);
68-
const popupLoginRespoonse = await oAuthObj.loginPopup({scopes: authScopes.popUpLogin});
66+
/*
67+
Ideally authConfig could be stored in a config file or environment variable:
68+
const authConfig = {
69+
configuration: {
70+
auth: {
71+
clientId: 'ENTER_CLIENT_ID',
72+
authority: 'https://login.microsoftonline.com/common'
73+
}
74+
},
75+
scopes: {
76+
m365Login: [
77+
"https://auth.msft.communication.azure.com/.default"
78+
],
79+
popUpLogin: [
80+
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
81+
"https://auth.msft.communication.azure.com/Teams.ManageChats"
82+
]
83+
}
84+
};
85+
*/
86+
const fetchAuthConfig = async () => {
87+
const response = await axios({
88+
url: 'authConfig',
89+
method: 'GET'
90+
});
91+
if (response.status !== 200) {
92+
throw new Error('Failed to get auth configs');
93+
}
94+
return response.data;
95+
}
96+
const authConfig = await fetchAuthConfig();
97+
98+
const oAuthObj = new PublicClientApplication(authConfig.configuration);
99+
const popupLoginResponse = await oAuthObj.loginPopup({scopes: authConfig.scopes.popUpLogin});
69100
const response = await axios({
70101
url: 'teamsPopupLogin',
71102
method: 'POST',
@@ -74,8 +105,8 @@ export const utils = {
74105
'Content-type': 'application/json'
75106
},
76107
data: JSON.stringify({
77-
aadToken: popupLoginRespoonse.accessToken,
78-
userObjectId: popupLoginRespoonse.uniqueId
108+
aadToken: popupLoginResponse.accessToken,
109+
userObjectId: popupLoginResponse.uniqueId
79110
})
80111
});
81112
if (response.status === 200) {

Project/webpack.config.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const axios = require("axios");
77
const bodyParser = require('body-parser');
88
const msal = require('@azure/msal-node');
99

10-
const {authConfig, authScopes, entraCredentialConfig} = require('./oAuthConfig');
11-
const clientId = authConfig.auth.clientId;
10+
const {authConfig, entraCredentialConfig} = require('./oAuthConfig');
11+
const clientId = authConfig.configuration.auth.clientId;
1212

1313

1414
if(!config || !config.connectionString || config.connectionString.indexOf('endpoint=') === -1)
@@ -216,11 +216,7 @@ module.exports = {
216216
devServer.app.get('/entraConfig', async (req, res) => {
217217
try {
218218
res.setHeader('Content-Type', 'application/json');
219-
res.status(200).json({
220-
tenantId: entraCredentialConfig.tenantId,
221-
clientId: entraCredentialConfig.clientId,
222-
resourceEndpoint: entraCredentialConfig.resourceEndpoint
223-
});
219+
res.status(200).json(entraCredentialConfig);
224220
} catch (e) {
225221
console.error(e);
226222
res.sendStatus(400);
@@ -231,8 +227,8 @@ module.exports = {
231227
const email = req.body.email;
232228
const password = req.body.password;
233229

234-
const pca = new msal.PublicClientApplication(authConfig);
235-
let tokenRequest = {scopes: authScopes.m365Login}
230+
const pca = new msal.PublicClientApplication(authConfig.configuration);
231+
let tokenRequest = {scopes: authConfig.scopes.m365Login}
236232

237233
tokenRequest.username = email;
238234
tokenRequest.password = password;
@@ -249,6 +245,15 @@ module.exports = {
249245
res.sendStatus(400);
250246
}
251247
});
248+
devServer.app.get('/authConfig', async (req, res) => {
249+
try {
250+
res.setHeader('Content-Type', 'application/json');
251+
res.status(200).json(authConfig);
252+
} catch (e) {
253+
console.error(e);
254+
res.sendStatus(400);
255+
}
256+
});
252257
devServer.app.post('/createRoom', async (req, res) => {
253258
try {
254259
let participants = [];

0 commit comments

Comments
 (0)