Skip to content

Commit ca42d1e

Browse files
committed
- finishes msal node implementation
1 parent 8168337 commit ca42d1e

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

helpers/authHelper.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import { ConfidentialClientApplication } from '@azure/msal-node';
22

33
import { msalConfiguration } from '../constants';
44

5-
const resource = 'https://graph.microsoft.com/';
5+
const resource = 'https://graph.microsoft.com/.default';
66

77
/**
88
* Generate a fully formed uri to use for authentication based on the supplied resource argument
99
* @return {string} a fully formed uri with which authentication can be completed.
1010
*/
1111
export function getAuthUrl() {
12-
return msalConfiguration.authority + '/oauth2/authorize'
13-
+ '?client_id=' + msalConfiguration.clientID
14-
+ '&response_type=code'
15-
+ '&redirect_uri=' + msalConfiguration.redirectUri;
12+
const authContext = new ConfidentialClientApplication({
13+
auth: {
14+
clientId: msalConfiguration.clientID,
15+
authority: msalConfiguration.authority.replace('common', msalConfiguration.tenantID),
16+
clientSecret: msalConfiguration.clientSecret
17+
}
18+
});
19+
return authContext.getAuthCodeUrl({
20+
redirectUri: msalConfiguration.redirectUri
21+
});
1622
}
1723

1824
/**
@@ -21,34 +27,29 @@ export function getAuthUrl() {
2127
* @param {AcquireTokenCallback} callback The callback function.
2228
*/
2329
export function getTokenFromCode(code) {
24-
const authContext = new ConfidentialClientApplication(msalConfiguration.authority); // TODO replace by onbehalf when available
25-
return new Promise((resolve, reject) => {
26-
authContext.acquireTokenWithAuthorizationCode(
27-
code,
28-
msalConfiguration.redirectUri,
29-
resource,
30-
msalConfiguration.clientID,
31-
msalConfiguration.clientSecret,
32-
(err, token) => {
33-
if (err) {
34-
reject(err);
35-
} else {
36-
resolve(token);
37-
}
38-
}
39-
);
30+
const authContext = new ConfidentialClientApplication({
31+
auth: {
32+
clientId: msalConfiguration.clientID,
33+
authority: msalConfiguration.authority.replace('common', msalConfiguration.tenantID),
34+
clientSecret: msalConfiguration.clientSecret
35+
}
36+
});
37+
return authContext.acquireTokenByCode({
38+
code: code,
39+
redirectUri: msalConfiguration.redirectUri,
40+
scopes: [resource]
4041
});
4142
}
4243

4344
export function getAppOnlyToken() {
44-
const authContext = new ConfidentialClientApplication(msalConfiguration.authority.replace('common', msalConfiguration.tenantID));
45-
return new Promise((resolve, reject) => { // TODO replace by client credential flow when available
46-
authContext.acquireTokenWithClientCredentials(resource, msalConfiguration.clientID, msalConfiguration.clientSecret, (err, token) => {
47-
if (err) {
48-
reject(err);
49-
} else {
50-
resolve(token);
51-
}
52-
});
45+
const authContext = new ConfidentialClientApplication({
46+
auth: {
47+
clientId: msalConfiguration.clientID,
48+
authority: msalConfiguration.authority.replace('common', msalConfiguration.tenantID),
49+
clientSecret: msalConfiguration.clientSecret
50+
}
51+
});
52+
return authContext.acquireTokenByClientCredential({
53+
scopes: [resource]
5354
});
5455
}

routes/auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ authRouter.get('/', (req, res) => {
3333
});
3434

3535
// Start authentication flow
36-
authRouter.get('/signin', (req, res) => {
37-
res.redirect(getAuthUrl());
36+
authRouter.get('/signin', async (req, res) => {
37+
const url = await getAuthUrl();
38+
res.redirect(url);
3839
});
3940

4041
// This route gets called at the end of the authentication flow.

0 commit comments

Comments
 (0)