@pnp/cli-microsoft365 10.0.0-beta.7dfc31a → 10.0.0-beta.e925c1c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,283 @@
1
+ import fs from 'fs';
2
+ import request from '../request.js';
3
+ import { odata } from './odata.js';
4
+ async function getCertificateBase64Encoded({ options, logger, debug }) {
5
+ if (options.certificateBase64Encoded) {
6
+ return options.certificateBase64Encoded;
7
+ }
8
+ if (debug) {
9
+ await logger.logToStderr(`Reading existing ${options.certificateFile}...`);
10
+ }
11
+ try {
12
+ return fs.readFileSync(options.certificateFile, { encoding: 'base64' });
13
+ }
14
+ catch (e) {
15
+ throw new Error(`Error reading certificate file: ${e}. Please add the certificate using base64 option '--certificateBase64Encoded'.`);
16
+ }
17
+ }
18
+ async function createServicePrincipal(appId) {
19
+ const requestOptions = {
20
+ url: `https://graph.microsoft.com/v1.0/myorganization/servicePrincipals`,
21
+ headers: {
22
+ 'content-type': 'application/json'
23
+ },
24
+ data: {
25
+ appId: appId
26
+ },
27
+ responseType: 'json'
28
+ };
29
+ return request.post(requestOptions);
30
+ }
31
+ async function grantOAuth2Permission({ appId, resourceId, scopeName }) {
32
+ const grantAdminConsentApplicationRequestOptions = {
33
+ url: `https://graph.microsoft.com/v1.0/myorganization/oauth2PermissionGrants`,
34
+ headers: {
35
+ accept: 'application/json;odata.metadata=none'
36
+ },
37
+ responseType: 'json',
38
+ data: {
39
+ clientId: appId,
40
+ consentType: "AllPrincipals",
41
+ principalId: null,
42
+ resourceId: resourceId,
43
+ scope: scopeName
44
+ }
45
+ };
46
+ return request.post(grantAdminConsentApplicationRequestOptions);
47
+ }
48
+ async function addRoleToServicePrincipal({ objectId, resourceId, appRoleId }) {
49
+ const requestOptions = {
50
+ url: `https://graph.microsoft.com/v1.0/myorganization/servicePrincipals/${objectId}/appRoleAssignments`,
51
+ headers: {
52
+ 'Content-Type': 'application/json'
53
+ },
54
+ responseType: 'json',
55
+ data: {
56
+ appRoleId: appRoleId,
57
+ principalId: objectId,
58
+ resourceId: resourceId
59
+ }
60
+ };
61
+ return request.post(requestOptions);
62
+ }
63
+ async function getRequiredResourceAccessForApis({ servicePrincipals, apis, scopeType, logger, debug }) {
64
+ if (!apis) {
65
+ return [];
66
+ }
67
+ const resolvedApis = [];
68
+ const requestedApis = apis.split(',').map(a => a.trim());
69
+ for (const api of requestedApis) {
70
+ const pos = api.lastIndexOf('/');
71
+ const permissionName = api.substring(pos + 1);
72
+ const servicePrincipalName = api.substring(0, pos);
73
+ if (debug) {
74
+ await logger.logToStderr(`Resolving ${api}...`);
75
+ await logger.logToStderr(`Permission name: ${permissionName}`);
76
+ await logger.logToStderr(`Service principal name: ${servicePrincipalName}`);
77
+ }
78
+ const servicePrincipal = servicePrincipals.find(sp => (sp.servicePrincipalNames.indexOf(servicePrincipalName) > -1 ||
79
+ sp.servicePrincipalNames.indexOf(`${servicePrincipalName}/`) > -1));
80
+ if (!servicePrincipal) {
81
+ throw `Service principal ${servicePrincipalName} not found`;
82
+ }
83
+ const scopesOfType = scopeType === 'Scope' ? servicePrincipal.oauth2PermissionScopes : servicePrincipal.appRoles;
84
+ const permission = scopesOfType.find(scope => scope.value === permissionName);
85
+ if (!permission) {
86
+ throw `Permission ${permissionName} for service principal ${servicePrincipalName} not found`;
87
+ }
88
+ let resolvedApi = resolvedApis.find(a => a.resourceAppId === servicePrincipal.appId);
89
+ if (!resolvedApi) {
90
+ resolvedApi = {
91
+ resourceAppId: servicePrincipal.appId,
92
+ resourceAccess: []
93
+ };
94
+ resolvedApis.push(resolvedApi);
95
+ }
96
+ const resourceAccessPermission = {
97
+ id: permission.id,
98
+ type: scopeType
99
+ };
100
+ resolvedApi.resourceAccess.push(resourceAccessPermission);
101
+ updateAppPermissions({
102
+ spId: servicePrincipal.id,
103
+ resourceAccessPermission,
104
+ oAuth2PermissionValue: permission.value
105
+ });
106
+ }
107
+ return resolvedApis;
108
+ }
109
+ function updateAppPermissions({ spId, resourceAccessPermission, oAuth2PermissionValue }) {
110
+ // During API resolution, we store globally both app role assignments and oauth2permissions
111
+ // So that we'll be able to parse them during the admin consent process
112
+ let existingPermission = entraApp.appPermissions.find(oauth => oauth.resourceId === spId);
113
+ if (!existingPermission) {
114
+ existingPermission = {
115
+ resourceId: spId,
116
+ resourceAccess: [],
117
+ scope: []
118
+ };
119
+ entraApp.appPermissions.push(existingPermission);
120
+ }
121
+ if (resourceAccessPermission.type === 'Scope' && oAuth2PermissionValue && !existingPermission.scope.find(scp => scp === oAuth2PermissionValue)) {
122
+ existingPermission.scope.push(oAuth2PermissionValue);
123
+ }
124
+ if (!existingPermission.resourceAccess.find(res => res.id === resourceAccessPermission.id)) {
125
+ existingPermission.resourceAccess.push(resourceAccessPermission);
126
+ }
127
+ }
128
+ export const entraApp = {
129
+ appPermissions: [],
130
+ createAppRegistration: async ({ options, apis, logger, verbose, debug }) => {
131
+ const applicationInfo = {
132
+ displayName: options.name,
133
+ signInAudience: options.multitenant ? 'AzureADMultipleOrgs' : 'AzureADMyOrg'
134
+ };
135
+ if (apis.length > 0) {
136
+ applicationInfo.requiredResourceAccess = apis;
137
+ }
138
+ if (options.redirectUris) {
139
+ applicationInfo[options.platform] = {
140
+ redirectUris: options.redirectUris.split(',').map(u => u.trim())
141
+ };
142
+ }
143
+ if (options.implicitFlow) {
144
+ if (!applicationInfo.web) {
145
+ applicationInfo.web = {};
146
+ }
147
+ applicationInfo.web.implicitGrantSettings = {
148
+ enableAccessTokenIssuance: true,
149
+ enableIdTokenIssuance: true
150
+ };
151
+ }
152
+ if (options.certificateFile || options.certificateBase64Encoded) {
153
+ const certificateBase64Encoded = await getCertificateBase64Encoded({ options, logger, debug });
154
+ const newKeyCredential = {
155
+ type: 'AsymmetricX509Cert',
156
+ usage: 'Verify',
157
+ displayName: options.certificateDisplayName,
158
+ key: certificateBase64Encoded
159
+ };
160
+ applicationInfo.keyCredentials = [newKeyCredential];
161
+ }
162
+ if (options.allowPublicClientFlows) {
163
+ applicationInfo.isFallbackPublicClient = true;
164
+ }
165
+ if (verbose) {
166
+ await logger.logToStderr(`Creating Microsoft Entra app registration...`);
167
+ }
168
+ const createApplicationRequestOptions = {
169
+ url: `https://graph.microsoft.com/v1.0/myorganization/applications`,
170
+ headers: {
171
+ accept: 'application/json;odata.metadata=none'
172
+ },
173
+ responseType: 'json',
174
+ data: applicationInfo
175
+ };
176
+ return request.post(createApplicationRequestOptions);
177
+ },
178
+ grantAdminConsent: async ({ appInfo, appPermissions, adminConsent, logger, debug }) => {
179
+ if (!adminConsent || appPermissions.length === 0) {
180
+ return appInfo;
181
+ }
182
+ const sp = await createServicePrincipal(appInfo.appId);
183
+ if (debug) {
184
+ await logger.logToStderr("Service principal created, returned object id: " + sp.id);
185
+ }
186
+ const tasks = [];
187
+ appPermissions.forEach(async (permission) => {
188
+ if (permission.scope.length > 0) {
189
+ tasks.push(grantOAuth2Permission({
190
+ appId: sp.id,
191
+ resourceId: permission.resourceId,
192
+ scopeName: permission.scope.join(' ')
193
+ }));
194
+ if (debug) {
195
+ await logger.logToStderr(`Admin consent granted for following resource ${permission.resourceId}, with delegated permissions: ${permission.scope.join(',')}`);
196
+ }
197
+ }
198
+ permission.resourceAccess.filter(access => access.type === "Role").forEach(async (access) => {
199
+ tasks.push(addRoleToServicePrincipal({
200
+ objectId: sp.id,
201
+ resourceId: permission.resourceId,
202
+ appRoleId: access.id
203
+ }));
204
+ if (debug) {
205
+ await logger.logToStderr(`Admin consent granted for following resource ${permission.resourceId}, with application permission: ${access.id}`);
206
+ }
207
+ });
208
+ });
209
+ await Promise.all(tasks);
210
+ return appInfo;
211
+ },
212
+ resolveApis: async ({ options, manifest, logger, verbose, debug }) => {
213
+ if (!options.apisDelegated && !options.apisApplication
214
+ && (typeof manifest?.requiredResourceAccess === 'undefined' || manifest.requiredResourceAccess.length === 0)) {
215
+ return [];
216
+ }
217
+ if (verbose) {
218
+ await logger.logToStderr('Resolving requested APIs...');
219
+ }
220
+ const servicePrincipals = await odata.getAllItems(`https://graph.microsoft.com/v1.0/myorganization/servicePrincipals?$select=appId,appRoles,id,oauth2PermissionScopes,servicePrincipalNames`);
221
+ let resolvedApis = [];
222
+ if (options.apisDelegated || options.apisApplication) {
223
+ resolvedApis = await getRequiredResourceAccessForApis({
224
+ servicePrincipals,
225
+ apis: options.apisDelegated,
226
+ scopeType: 'Scope',
227
+ logger,
228
+ debug
229
+ });
230
+ if (verbose) {
231
+ await logger.logToStderr(`Resolved delegated permissions: ${JSON.stringify(resolvedApis, null, 2)}`);
232
+ }
233
+ const resolvedApplicationApis = await getRequiredResourceAccessForApis({
234
+ servicePrincipals,
235
+ apis: options.apisApplication,
236
+ scopeType: 'Role',
237
+ logger,
238
+ debug
239
+ });
240
+ if (verbose) {
241
+ await logger.logToStderr(`Resolved application permissions: ${JSON.stringify(resolvedApplicationApis, null, 2)}`);
242
+ }
243
+ // merge resolved application APIs onto resolved delegated APIs
244
+ resolvedApplicationApis.forEach(resolvedRequiredResource => {
245
+ const requiredResource = resolvedApis.find(api => api.resourceAppId === resolvedRequiredResource.resourceAppId);
246
+ if (requiredResource) {
247
+ requiredResource.resourceAccess.push(...resolvedRequiredResource.resourceAccess);
248
+ }
249
+ else {
250
+ resolvedApis.push(resolvedRequiredResource);
251
+ }
252
+ });
253
+ }
254
+ else {
255
+ const manifestApis = manifest.requiredResourceAccess;
256
+ manifestApis.forEach(manifestApi => {
257
+ resolvedApis.push(manifestApi);
258
+ const app = servicePrincipals.find(servicePrincipals => servicePrincipals.appId === manifestApi.resourceAppId);
259
+ if (app) {
260
+ manifestApi.resourceAccess.forEach((res => {
261
+ const resourceAccessPermission = {
262
+ id: res.id,
263
+ type: res.type
264
+ };
265
+ const oAuthValue = app.oauth2PermissionScopes.find(scp => scp.id === res.id)?.value;
266
+ updateAppPermissions({
267
+ spId: app.id,
268
+ resourceAccessPermission,
269
+ oAuth2PermissionValue: oAuthValue
270
+ });
271
+ }));
272
+ }
273
+ });
274
+ }
275
+ if (verbose) {
276
+ await logger.logToStderr(`Merged delegated and application permissions: ${JSON.stringify(resolvedApis, null, 2)}`);
277
+ await logger.logToStderr(`App role assignments: ${JSON.stringify(entraApp.appPermissions.flatMap(permission => permission.resourceAccess.filter(access => access.type === "Role")), null, 2)}`);
278
+ await logger.logToStderr(`OAuth2 permissions: ${JSON.stringify(entraApp.appPermissions.flatMap(permission => permission.scope), null, 2)}`);
279
+ }
280
+ return resolvedApis;
281
+ }
282
+ };
283
+ //# sourceMappingURL=entraApp.js.map
@@ -2,6 +2,11 @@ Setting name|Definition|Default value
2
2
  ------------|----------|-------------
3
3
  `authType`|Default login method to use when running `m365 login` without the `--authType` option.|`deviceCode`
4
4
  `autoOpenLinksInBrowser`|Automatically open the browser for all commands which return a url and expect the user to copy paste this to the browser. For example when logging in, using `m365 login` in device code mode.|`false`
5
+ `clientId`|ID of the default Entra ID app use by the CLI to authenticate|``
6
+ `clientSecret`|Secret of the default Entra ID app use by the CLI to authenticate|``
7
+ `clientCertificateFile`|Path to the file containing the client certificate to use for authentication|``
8
+ `clientCertificateBase64Encoded`|Base64-encoded client certificate contents|``
9
+ `clientCertificatePassword`|Password to the client certificate file|``
5
10
  `copyDeviceCodeToClipboard`|Automatically copy the device code to the clipboard when running `m365 login` command in device code mode|`false`
6
11
  `csvEscape`|Single character used for escaping; only apply to characters matching the quote and the escape options|`"`
7
12
  `csvHeader`|Display the column names on the first line|`true`
@@ -18,3 +23,4 @@ Setting name|Definition|Default value
18
23
  `promptListPageSize`|By default, lists of choices longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once.|7
19
24
  `showHelpOnFailure`|Automatically display help when executing a command failed|`true`
20
25
  `showSpinner`|Display spinner when executing commands|`true`
26
+ `tenantId`|ID of the default tenant to use when authenticating with|``
@@ -18,6 +18,9 @@ m365 setup [options]
18
18
 
19
19
  `--scripting`
20
20
  : Configure CLI for Microsoft 365 for use in scripts without prompting for additional information.
21
+
22
+ `--skipApp`
23
+ : Skip configuring an Entra app for use with CLI for Microsoft 365.
21
24
  ```
22
25
 
23
26
  <Global />
@@ -28,6 +31,10 @@ The `m365 setup` command is a wizard that helps you configure the CLI for Micros
28
31
 
29
32
  The command will ask you the following questions:
30
33
 
34
+ - _CLI for Microsoft 365 requires a Microsoft Entra app. Do you want to create a new app registration or use an existing one?_
35
+
36
+ You can choose between using an existing Entra app or creating a new one. If you choose to create a new app, the CLI will ask you to choose between a minimal and a full set of permissions. It then signs in as Azure CLI to your tenant, creates a new app registration, and stores its information in the CLI configuration.
37
+
31
38
  - _How do you plan to use the CLI?_
32
39
 
33
40
  You can choose between **interactive** and **scripting** use. In interactive mode, the CLI for Microsoft 365 will prompt you for additional information when needed, automatically open links browser, automatically show help on errors and show spinners. In **scripting** mode, the CLI will not use interactivity to prevent blocking your scripts.
@@ -71,24 +78,30 @@ The `m365 setup` command uses the following presets:
71
78
 
72
79
  ## Examples
73
80
 
74
- Configure CLI for Microsoft based on your preferences interactively
81
+ Configure CLI for Microsoft 365 based on your preferences interactively
75
82
 
76
83
  ```sh
77
84
  m365 setup
78
85
  ```
79
86
 
80
- Configure CLI for Microsoft for interactive use without prompting for additional information
87
+ Configure CLI for Microsoft 365 for interactive use without prompting for additional information
81
88
 
82
89
  ```sh
83
90
  m365 setup --interactive
84
91
  ```
85
92
 
86
- Configure CLI for Microsoft for use in scripts without prompting for additional information
93
+ Configure CLI for Microsoft 365 for use in scripts without prompting for additional information
87
94
 
88
95
  ```sh
89
96
  m365 setup --scripting
90
97
  ```
91
98
 
99
+ Configure CLI for Microsoft 365 without setting up an Entra app
100
+
101
+ ```sh
102
+ m365 setup --skipApp
103
+ ```
104
+
92
105
  ## Response
93
106
 
94
107
  The command won't return a response on success.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnp/cli-microsoft365",
3
- "version": "10.0.0-beta.7dfc31a",
3
+ "version": "10.0.0-beta.e925c1c",
4
4
  "description": "Manage Microsoft 365 and SharePoint Framework projects on any platform",
5
5
  "license": "MIT",
6
6
  "main": "./dist/api.js",