Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 92571b8

Browse files
committed
remove app permission from sample
1 parent dfb0a7c commit 92571b8

File tree

2 files changed

+16
-91
lines changed

2 files changed

+16
-91
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
/**
2-
* Indicates whether the access token was issued to a user or an application.
3-
* @param {Object} accessTokenPayload
4-
* @returns {boolean}
5-
*/
6-
const isAppOnlyToken = (accessTokenPayload) => {
7-
/**
8-
* An access token issued by Azure AD will have at least one of the two claims. Access tokens
9-
* issued to a user will have the 'scp' claim. Access tokens issued to an application will have
10-
* the roles claim. Access tokens that contain both claims are issued only to users, where the scp
11-
* claim designates the delegated permissions, while the roles claim designates the user's role.
12-
*
13-
* To determine whether an access token was issued to a user (i.e delegated) or an application
14-
* more easily, we recommend enabling the optional claim 'idtyp'. For more information, see:
15-
* https://docs.microsoft.com/azure/active-directory/develop/access-tokens#user-and-application-tokens
16-
*/
17-
if (!accessTokenPayload.hasOwnProperty('idtyp')) {
18-
if (accessTokenPayload.hasOwnProperty('scp')) {
19-
return false;
20-
} else if (!accessTokenPayload.hasOwnProperty('scp') && accessTokenPayload.hasOwnProperty('roles')) {
21-
return true;
22-
}
23-
}
24-
25-
return accessTokenPayload.idtyp === 'app';
26-
};
27-
281
/**
292
* Ensures that the access token has the specified delegated permissions.
303
* @param {Object} accessTokenPayload: Parsed access token payload
@@ -42,25 +15,6 @@ const isAppOnlyToken = (accessTokenPayload) => {
4215
return false;
4316
}
4417

45-
/**
46-
* Ensures that the access token has the specified application permissions.
47-
* @param {Object} accessTokenPayload: Parsed access token payload
48-
* @param {Array} requiredPermission: list of required permissions
49-
* @returns {boolean}
50-
*/
51-
const hasRequiredApplicationPermissions = (accessTokenPayload, requiredPermission) => {
52-
const normalizedRequiredPermissions = requiredPermission.map(permission => permission.toUpperCase());
53-
54-
if (accessTokenPayload.hasOwnProperty('roles') && accessTokenPayload.roles
55-
.some(claim => normalizedRequiredPermissions.includes(claim.toUpperCase()))) {
56-
return true;
57-
}
58-
59-
return false;
60-
}
61-
6218
module.exports = {
63-
isAppOnlyToken,
6419
hasRequiredDelegatedPermissions,
65-
hasRequiredApplicationPermissions,
6620
}
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,27 @@
11
const { getOboToken } = require('../MsalOnBehalfOfClient');
22
const { getGraphClient } = require('../util/graphClient');
33
const { ResponseType } = require('@microsoft/microsoft-graph-client');
4-
const authConfig = require('../authConfig');
4+
const authConfig = require('../authConfig');
55

66
const {
7-
isAppOnlyToken,
87
hasRequiredDelegatedPermissions,
9-
hasRequiredApplicationPermissions,
108
} = require('../auth/permissionUtils');
119

12-
1310
exports.getProfile = async (req, res, next) => {
14-
if (isAppOnlyToken(req.authInfo)) {
15-
if (
16-
hasRequiredApplicationPermissions(
17-
req.authInfo,
18-
authConfig.resources.middleTierAPI.applicationPermissions.scopes
19-
)
20-
) {
21-
try {
22-
accessToken = await getOboToken(tokenValue);
23-
let graphResponse = await getGraphClient(accessToken).api('/me').responseType(ResponseType.RAW).get();
24-
graphResponse = await graphResponse.json();
25-
res.status(200).send(graphResponse);
26-
} catch (error) {
27-
next(error);
28-
}
29-
}else {
30-
next(new Error('Application does not have the required permissions'));
11+
const userToken = req.get('authorization');
12+
const [bearer, tokenValue] = userToken.split(' ');
13+
14+
let accessToken;
15+
if (hasRequiredDelegatedPermissions(req.authInfo, authConfig.resources.middleTierAPI.delegatedPermissions.scopes)) {
16+
try {
17+
accessToken = await getOboToken(tokenValue);
18+
let graphResponse = await getGraphClient(accessToken).api('/me').responseType(ResponseType.RAW).get();
19+
graphResponse = await graphResponse.json();
20+
res.json(graphResponse);
21+
} catch (error) {
22+
next(error);
3123
}
3224
} else {
33-
const userToken = req.get('authorization');
34-
const [bearer, tokenValue] = userToken.split(' ');
35-
36-
let accessToken;
37-
if (
38-
hasRequiredDelegatedPermissions(
39-
req.authInfo,
40-
authConfig.resources.middleTierAPI.delegatedPermissions.scopes
41-
)
42-
) {
43-
try {
44-
accessToken = await getOboToken(tokenValue);
45-
let graphResponse = await getGraphClient(accessToken).api('/me').responseType(ResponseType.RAW).get();
46-
graphResponse = await graphResponse.json();
47-
res.json(graphResponse);
48-
} catch (error) {
49-
next(error);
50-
}
51-
} else {
52-
next(new Error('User does not have the required permissions'));
53-
}
54-
55-
}
56-
};
25+
next(new Error('User does not have the required permissions'));
26+
}
27+
};

0 commit comments

Comments
 (0)