@microsoft/power-apps-native-auth 0.3.2

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.
Files changed (36) hide show
  1. package/LICENSE +112 -0
  2. package/README.md +54 -0
  3. package/android/build.gradle +79 -0
  4. package/android/src/main/AndroidManifest.xml +31 -0
  5. package/android/src/main/java/expo/modules/msalintune/ExpoMsalIntuneModule.kt +488 -0
  6. package/app.plugin.js +4 -0
  7. package/expo-module.config.json +9 -0
  8. package/ios/ExpoMsalIntune.podspec +27 -0
  9. package/ios/ExpoMsalIntuneAppDelegate.swift +31 -0
  10. package/ios/ExpoMsalIntuneModule.swift +382 -0
  11. package/lib/NOTICE.txt +5 -0
  12. package/lib/commonjs/AuthConfig.types.js +1 -0
  13. package/lib/commonjs/ExpoMsalIntune.types.js +1 -0
  14. package/lib/commonjs/ExpoMsalIntuneModule.js +50 -0
  15. package/lib/commonjs/index.js +217 -0
  16. package/lib/commonjs/package.json +1 -0
  17. package/lib/module/AuthConfig.types.js +1 -0
  18. package/lib/module/ExpoMsalIntune.types.js +1 -0
  19. package/lib/module/ExpoMsalIntuneModule.js +44 -0
  20. package/lib/module/index.js +207 -0
  21. package/lib/module/package.json +1 -0
  22. package/lib/typescript/commonjs/AuthConfig.types.d.ts +76 -0
  23. package/lib/typescript/commonjs/ExpoMsalIntune.types.d.ts +80 -0
  24. package/lib/typescript/commonjs/ExpoMsalIntuneModule.d.ts +92 -0
  25. package/lib/typescript/commonjs/index.d.ts +125 -0
  26. package/lib/typescript/commonjs/package.json +1 -0
  27. package/lib/typescript/module/AuthConfig.types.d.ts +76 -0
  28. package/lib/typescript/module/ExpoMsalIntune.types.d.ts +80 -0
  29. package/lib/typescript/module/ExpoMsalIntuneModule.d.ts +92 -0
  30. package/lib/typescript/module/index.d.ts +125 -0
  31. package/lib/typescript/module/package.json +1 -0
  32. package/package.json +93 -0
  33. package/plugin/index.js +528 -0
  34. package/plugin/withMsalRedirectScheme.js +53 -0
  35. package/scripts/setup-android-sdk.js +122 -0
  36. package/scripts/setup-ios-frameworks.js +100 -0
@@ -0,0 +1,528 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * power-apps-native-auth Expo Config Plugin
6
+ *
7
+ * Injects the Intune MAM Gradle plugin into the app's generated Android build
8
+ * files at `expo prebuild` time. This is necessary because `android/` is
9
+ * gitignored and fully regenerated on every prebuild — manual edits to those
10
+ * files do not persist.
11
+ *
12
+ * What this plugin does (Android only, skipped for development-player builds):
13
+ *
14
+ * android/build.gradle (root project):
15
+ * — Adds `classpath files(...)` for the MAM plugin jar downloaded by
16
+ * `npm run setup:android` into node_modules/power-apps-native-auth/android/libs/.
17
+ * — Throws a descriptive GradleException if the jar is missing (directing
18
+ * the developer to run the setup script).
19
+ *
20
+ * android/app/build.gradle:
21
+ * — Applies `com.microsoft.intune.mam` which instruments Activity /
22
+ * Application / ContentProvider subclasses with MAM equivalents at
23
+ * compile time so Intune app-protection policies (PIN, wipe, copy-paste)
24
+ * are enforced at the activity lifecycle level.
25
+ *
26
+ * What this plugin does (iOS):
27
+ *
28
+ * Info.plist:
29
+ * — Appends `msauth.<bundleId>` to `CFBundleURLTypes` so MSAL can route
30
+ * its `msauth.<bundleId>://auth` redirect back to the app. The Intune
31
+ * xcframeworks themselves are declared in ExpoMsalIntune.podspec as
32
+ * vendored_frameworks, so CocoaPods handles linkage.
33
+ *
34
+ * Prerequisites:
35
+ * cd packages/power-apps-native-auth && npm run setup:android
36
+ * (downloads SDK AARs + plugin jar from GitHub into android/libs/)
37
+ */
38
+
39
+ const { withProjectBuildGradle, withAppBuildGradle, withDangerousMod, withStringsXml, withAndroidManifest, withInfoPlist, withEntitlementsPlist } = require('@expo/config-plugins');
40
+ const fs = require('fs');
41
+ const path = require('path');
42
+
43
+ const withMsalRedirectScheme = require('./withMsalRedirectScheme.js');
44
+
45
+ const IS_DEVELOPMENT_PLAYER = process.env.POWER_APPS_DEV_PLAYER === 'true' || process.env.DEV_CLIENT === 'true';
46
+
47
+ // ---------------------------------------------------------------------------
48
+ // Helpers
49
+ // ---------------------------------------------------------------------------
50
+
51
+ /**
52
+ * Idempotent — checks for a sentinel string before inserting so repeated
53
+ * prebuild runs don't duplicate the injected block.
54
+ *
55
+ * @param {string} contents Current file contents
56
+ * @param {string} anchor Exact string to insert after
57
+ * @param {string} injection Text to insert (includes leading newline)
58
+ * @param {string} sentinel String whose presence means already injected
59
+ * @returns {string}
60
+ */
61
+ function insertAfter(contents, anchor, injection, sentinel) {
62
+ if (contents.includes(sentinel)) return contents;
63
+ if (!contents.includes(anchor)) {
64
+ throw new Error(
65
+ `power-apps-native-auth plugin: could not find anchor "${anchor}" in gradle file.\n` +
66
+ 'The file format may have changed — please file a bug.'
67
+ );
68
+ }
69
+ return contents.replace(anchor, anchor + injection);
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Root android/build.gradle — inject MAM plugin classpath
74
+ // ---------------------------------------------------------------------------
75
+
76
+ const MAM_JAR_SENTINEL = 'com.microsoft.intune.mam.build.jar';
77
+
78
+ // Absolute path to android/libs — resolved at prebuild time from __dirname so the plugin
79
+ // works in both a monorepo (apps/dev-player/android → ../../../node_modules) and a
80
+ // standalone project (expo-app-standalone/android → ../node_modules) without any
81
+ // hardcoded depth assumptions.
82
+ const MAM_LIBS_ABS = path.resolve(__dirname, '..', 'android', 'libs').replace(/\\/g, '/');
83
+ const MAM_JAR_ABS = path.join(MAM_LIBS_ABS, 'com.microsoft.intune.mam.build.jar').replace(/\\/g, '/');
84
+
85
+ const MAM_CLASSPATH_BLOCK = `
86
+ // Intune MAM Gradle plugin — injected by power-apps-native-auth config plugin.
87
+ // The jar is downloaded from GitHub by: npm run setup:android
88
+ // (packages/power-apps-native-auth/scripts/setup-android-sdk.js)
89
+ // It is NOT published to any public Maven feed.
90
+ def _mamJar = new File('${MAM_JAR_ABS}')
91
+ if (!_mamJar.exists()) {
92
+ throw new GradleException(
93
+ "Intune MAM plugin jar not found at \${_mamJar.canonicalPath}.\\n" +
94
+ "Run: cd packages/power-apps-native-auth && npm run setup:android"
95
+ )
96
+ }
97
+ classpath files(_mamJar)
98
+ // Javassist is required by the MAM Gradle plugin for bytecode instrumentation
99
+ // but is NOT bundled in the jar — it must be on the buildscript classpath.
100
+ classpath 'org.javassist:javassist:3.29.2-GA'`;
101
+
102
+ // Sentinels / injections for allprojects repositories block (root build.gradle).
103
+ // Injected into allprojects { repositories { } } so every subproject (including
104
+ // :expo which transitively depends on power-apps-native-auth) can resolve:
105
+ // • display-mask (MSAL transitive dep) — only on the Duo SDK feed
106
+ // • Microsoft.Intune.MAM.SDK AARs — only in the local flatDir
107
+ const MAM_DUO_SENTINEL = 'DuoSDK-Public/_packaging/Duo-SDK-Feed';
108
+
109
+ const MAM_REPOSITORIES_SENTINEL = 'power-apps-native-auth repositories block';
110
+
111
+ // Injected inside `allprojects { repositories { ... } }` after jitpack.
112
+ const MAM_REPOSITORIES_BLOCK = `
113
+ // power-apps-native-auth: Duo SDK feed + local AARs — injected by Expo config plugin.
114
+ // Duo SDK feed: hosts display-mask (MSAL transitive dep) not on Google/Central/JitPack.
115
+ maven { url 'https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1' } // ${MAM_DUO_SENTINEL}
116
+ // Intune MAM SDK AARs — not on any Maven feed.
117
+ // Run: cd packages/power-apps-native-auth && npm run setup:android
118
+ flatDir {
119
+ dirs '${MAM_LIBS_ABS}' // power-apps-native-auth repositories block
120
+ }`;
121
+
122
+ // Injects the Duo SDK Maven feed + flatDir into the root build.gradle's
123
+ // allprojects { repositories { } } block so ALL subprojects can resolve:
124
+ // • display-mask (MSAL transitive dep via :expo > :power-apps-native-auth)
125
+ // • Microsoft.Intune.MAM.SDK AARs (from local flatDir)
126
+ // Required in ALL builds (including dev-client).
127
+ function withMamRepositories(config) {
128
+ return withProjectBuildGradle(config, (mod) => {
129
+ mod.modResults.contents = insertAfter(
130
+ mod.modResults.contents,
131
+ "maven { url 'https://www.jitpack.io' }",
132
+ MAM_REPOSITORIES_BLOCK,
133
+ MAM_REPOSITORIES_SENTINEL
134
+ );
135
+ return mod;
136
+ });
137
+ }
138
+
139
+
140
+ // Only needed in release builds — the plugin performs bytecode instrumentation
141
+ // at compile time. Dev-client builds skip this (no instrumentation needed).
142
+ function withMamPluginClasspath(config) {
143
+ return withProjectBuildGradle(config, (mod) => {
144
+ // Inject MAM plugin classpath into buildscript { dependencies { } }.
145
+ // Expo's generated root build.gradle ends its dependencies block with the
146
+ // kotlin classpath — insert the MAM classpath immediately after it.
147
+ mod.modResults.contents = insertAfter(
148
+ mod.modResults.contents,
149
+ "classpath('org.jetbrains.kotlin:kotlin-gradle-plugin')",
150
+ MAM_CLASSPATH_BLOCK,
151
+ MAM_JAR_SENTINEL
152
+ );
153
+ return mod;
154
+ });
155
+ }
156
+
157
+ // ---------------------------------------------------------------------------
158
+ // android/app/build.gradle — apply MAM plugin + SDK dependency
159
+ // ---------------------------------------------------------------------------
160
+
161
+ const MAM_APPLY_SENTINEL = "apply plugin: 'com.microsoft.intune.mam'";
162
+
163
+ const MAM_APPLY_BLOCK = `
164
+ // Intune MAM bytecode instrumentation — injected by power-apps-native-auth config plugin.
165
+ // Transforms Activity/Application/ContentProvider to MAM* equivalents at compile time.
166
+ apply plugin: 'com.microsoft.intune.mam'`;
167
+
168
+ function withMamPluginApply(config) {
169
+ return withAppBuildGradle(config, (mod) => {
170
+ mod.modResults.contents = insertAfter(
171
+ mod.modResults.contents,
172
+ // Expo generates apply plugin: "com.facebook.react" as the last apply line.
173
+ 'apply plugin: "com.facebook.react"',
174
+ MAM_APPLY_BLOCK,
175
+ MAM_APPLY_SENTINEL
176
+ );
177
+ return mod;
178
+ });
179
+ }
180
+
181
+ // Removes the MAM apply block injected by a prior release prebuild.
182
+ // Called in dev-client mode so the MAM plugin is not active during debug builds.
183
+ function withMamPluginCleanup(config) {
184
+ return withAppBuildGradle(config, (mod) => {
185
+ if (mod.modResults.contents.includes(MAM_APPLY_SENTINEL)) {
186
+ mod.modResults.contents = mod.modResults.contents.replace(MAM_APPLY_BLOCK, '');
187
+ }
188
+ return mod;
189
+ });
190
+ }
191
+
192
+ // Removes the MAM classpath block injected by a prior release prebuild.
193
+ function withMamClasspathCleanup(config) {
194
+ return withProjectBuildGradle(config, (mod) => {
195
+ if (mod.modResults.contents.includes(MAM_JAR_SENTINEL)) {
196
+ mod.modResults.contents = mod.modResults.contents.replace(MAM_CLASSPATH_BLOCK, '');
197
+ }
198
+ return mod;
199
+ });
200
+ }
201
+
202
+ const MAM_SDK_DEP_SENTINEL = "name: 'Microsoft.Intune.MAM.SDK', ext: 'aar'";
203
+
204
+ const MAM_SDK_DEP_BLOCK = `
205
+ // Intune MAM SDK — required by the com.microsoft.intune.mam plugin for bytecode rewriting.
206
+ // Served from the flatDir declared in the root build.gradle.
207
+ implementation(name: 'Microsoft.Intune.MAM.SDK', ext: 'aar')
208
+ implementation(name: 'Microsoft.Intune.MAM.SDK.DownlevelStubs', ext: 'aar')`;
209
+
210
+ // Injects the MAM SDK AARs as implementation dependencies of the :app module.
211
+ // Required by the MAM Gradle plugin — it refuses to run without the SDK on the
212
+ // compilation classpath ("The MAM SDK must be specified as a compilation dependency").
213
+ function withMamAppSdkDependency(config) {
214
+ return withAppBuildGradle(config, (mod) => {
215
+ mod.modResults.contents = insertAfter(
216
+ mod.modResults.contents,
217
+ 'implementation("com.facebook.react:react-android")',
218
+ MAM_SDK_DEP_BLOCK,
219
+ MAM_SDK_DEP_SENTINEL
220
+ );
221
+ return mod;
222
+ });
223
+ }
224
+
225
+ // ---------------------------------------------------------------------------
226
+ // android/app/build.gradle — ShrinkWrap product flavor (release only)
227
+ // ---------------------------------------------------------------------------
228
+
229
+ // The production AAB+protoc pipeline (OptimizedWrapBuild.ps1) expects the AAB
230
+ // to be named app-ShrinkWrap-Release.aab. Adding a 'ShrinkWrap' product flavor
231
+ // causes Gradle to produce exactly that filename from bundleShrinkWrapRelease.
232
+
233
+ const SHRINKWRAP_FLAVOR_SENTINEL = 'ShrinkWrap product flavor';
234
+
235
+ const SHRINKWRAP_FLAVOR_BLOCK = `
236
+ // ShrinkWrap product flavor — injected by power-apps-native-auth config plugin.
237
+ // Produces app-ShrinkWrap-Release.aab, the exact filename expected by the
238
+ // production AAB+protoc wrap pipeline (OptimizedWrapBuild.ps1).
239
+ // Build with: ./gradlew bundleShrinkWrapRelease
240
+ flavorDimensions "env"
241
+ productFlavors {
242
+ ShrinkWrap {
243
+ dimension "env"
244
+ }
245
+ }
246
+ `;
247
+
248
+ function withShrinkWrapFlavor(config) {
249
+ return withAppBuildGradle(config, (mod) => {
250
+ if (mod.modResults.contents.includes(SHRINKWRAP_FLAVOR_SENTINEL)) {
251
+ return mod; // idempotent
252
+ }
253
+ // Insert before packagingOptions { which immediately follows buildTypes in
254
+ // Expo's generated app/build.gradle.
255
+ const anchor = ' packagingOptions {';
256
+ if (!mod.modResults.contents.includes(anchor)) {
257
+ throw new Error(
258
+ 'power-apps-native-auth plugin: could not find "packagingOptions {" in app/build.gradle.\n' +
259
+ 'The file format may have changed — please file a bug.'
260
+ );
261
+ }
262
+ mod.modResults.contents = mod.modResults.contents.replace(
263
+ anchor,
264
+ SHRINKWRAP_FLAVOR_BLOCK + anchor
265
+ );
266
+ return mod;
267
+ });
268
+ }
269
+
270
+ // ---------------------------------------------------------------------------
271
+ // android/app/src/main/AndroidManifest.xml — MSAL BrowserTabActivity
272
+ // ---------------------------------------------------------------------------
273
+
274
+ // The production base AAB has BrowserTabActivity + msauth intent filter pre-baked
275
+ // with placeholder values that the wrap script replaces at wrap time:
276
+ // android:host="com.test.wrap" → customer bundle ID (global replace)
277
+ // android:path="/Eut614goUaBrf2TSM7tmnXWv2VA=" → actual keystore signature hash
278
+ // Without this entry, MSAL shows "Intent filter for BrowserTabActivity is missing".
279
+
280
+ const BROWSER_TAB_ACTIVITY_NAME = 'com.microsoft.identity.client.BrowserTabActivity';
281
+ const MSAL_PLACEHOLDER_BUNDLE_ID = 'com.test.wrap';
282
+ const MSAL_PLACEHOLDER_SIG_HASH = 'Eut614goUaBrf2TSM7tmnXWv2VA=';
283
+
284
+ function withMsalBrowserTabActivity(config) {
285
+ return withAndroidManifest(config, (mod) => {
286
+ const app = mod.modResults.manifest.application[0];
287
+ const activities = app.activity ?? [];
288
+
289
+ // Idempotent — skip if already present
290
+ if (activities.some((a) => a.$['android:name'] === BROWSER_TAB_ACTIVITY_NAME)) {
291
+ app.activity = activities;
292
+ return mod;
293
+ }
294
+
295
+ activities.push({
296
+ $: {
297
+ 'android:name': BROWSER_TAB_ACTIVITY_NAME,
298
+ 'android:exported': 'true',
299
+ },
300
+ 'intent-filter': [{
301
+ action: [{ $: { 'android:name': 'android.intent.action.VIEW' } }],
302
+ category: [
303
+ { $: { 'android:name': 'android.intent.category.DEFAULT' } },
304
+ { $: { 'android:name': 'android.intent.category.BROWSABLE' } },
305
+ ],
306
+ data: [{
307
+ $: {
308
+ 'android:scheme': 'msauth',
309
+ 'android:host': MSAL_PLACEHOLDER_BUNDLE_ID,
310
+ 'android:path': `/${MSAL_PLACEHOLDER_SIG_HASH}`,
311
+ },
312
+ }],
313
+ }],
314
+ });
315
+
316
+ app.activity = activities;
317
+ return mod;
318
+ });
319
+ }
320
+
321
+ // ---------------------------------------------------------------------------
322
+ // android/app/src/main/res/values/strings.xml — wrap pipeline placeholders
323
+ // ---------------------------------------------------------------------------
324
+
325
+ // The production AAB+protoc pipeline patches these placeholder strings in
326
+ // resources.pb via protoc decode / text-replace / re-encode. Because protoc
327
+ // recomputes all field lengths on encode, string length changes are safe here
328
+ // (unlike HBC binary patching). The placeholders must exist as literal string
329
+ // values so the pipeline can locate and replace them.
330
+
331
+ const WRAP_PLACEHOLDER_STRINGS = [
332
+ // Launcher display name — pipeline replaces with maker's displayName
333
+ { name: 'app_name', value: 'DISPLAY_NAME_TAG' },
334
+ // MSAL/ADAL config — replaced with customer adalClientId / adalRedirectUri.
335
+ // Android: read at runtime by getWrapConfig() via Resources.getString().
336
+ // iOS: read by the Intune MAM SDK directly from IntuneMAMSettings in Info.plist.
337
+ { name: 'adal_client_id', value: 'ADAL_CLIENT_ID_TAG' },
338
+ { name: 'adal_redirect_uri', value: 'ADAL_REDIRECT_URI_TAG' },
339
+ // AAD tenant ID for the MSAL authority URL. The consumer wrap pipeline
340
+ // patches this with $env:TENANTID (the app owner's tenant — sourced from
341
+ // $rawAppInfo.properties.owner.tenantId in WrapProjectValidations.ps1).
342
+ // For typical customer setups the app owner's tenant equals the AAD app
343
+ // registration tenant, which is what MSAL's authority URL requires.
344
+ { name: 'adal_tenant_id', value: 'ADAL_TENANT_ID_TAG' },
345
+ ];
346
+
347
+ function withWrapStringPlaceholders(config) {
348
+ return withStringsXml(config, (mod) => {
349
+ const strings = mod.modResults.resources.string ?? [];
350
+
351
+ for (const { name, value } of WRAP_PLACEHOLDER_STRINGS) {
352
+ const existing = strings.find((s) => s.$?.name === name);
353
+ if (existing) {
354
+ existing._ = value;
355
+ } else {
356
+ strings.push({ $: { name }, _: value });
357
+ }
358
+ }
359
+
360
+ mod.modResults.resources.string = strings;
361
+ return mod;
362
+ });
363
+ }
364
+
365
+ // ---------------------------------------------------------------------------
366
+ // iOS Info.plist — register msauth URL scheme for MSAL redirect URI validation
367
+ // ---------------------------------------------------------------------------
368
+
369
+ // iOS MSAL SDK (v1.1.0+) validates that the msauth.<bundleId> URL scheme is
370
+ // registered in CFBundleURLTypes at MSALPublicClientApplication init time,
371
+ // even in WKWebView mode. Without this entry, MSAL throws:
372
+ // MSALErrorDomain code=-50000, MSALInternalErrorCodeKey: -42001
373
+ // "The required app scheme '...' is not registered in Info.plist"
374
+ //
375
+ // For production rewrapped builds the wrap pipeline replaces CFBundleURLTypes[0]
376
+ // with the customer's msauth scheme, so the dev-time entry is superseded.
377
+ function withMsalIOSURLScheme(config) {
378
+ return withInfoPlist(config, (mod) => {
379
+ const bundleId = config.ios?.bundleIdentifier ?? 'com.test.wrap';
380
+ const msauthScheme = `msauth.${bundleId}`;
381
+
382
+ if (!Array.isArray(mod.modResults.CFBundleURLTypes)) {
383
+ mod.modResults.CFBundleURLTypes = [];
384
+ }
385
+
386
+ // Idempotent — skip if the scheme is already registered
387
+ if (
388
+ !mod.modResults.CFBundleURLTypes.some(
389
+ (t) =>
390
+ Array.isArray(t.CFBundleURLSchemes) &&
391
+ t.CFBundleURLSchemes.includes(msauthScheme)
392
+ )
393
+ ) {
394
+ mod.modResults.CFBundleURLTypes.push({
395
+ CFBundleURLName: msauthScheme,
396
+ CFBundleURLSchemes: [msauthScheme],
397
+ });
398
+ }
399
+
400
+ return mod;
401
+ });
402
+ }
403
+
404
+ // ---------------------------------------------------------------------------
405
+ // iOS Entitlements — keychain-access-groups for MSAL token cache
406
+ // ---------------------------------------------------------------------------
407
+
408
+ // MSAL iOS defaults to the "com.microsoft.adalcache" keychain access group for
409
+ // token caching. Without a keychain-access-groups entitlement that includes
410
+ // this group the OS returns errSecMissingEntitlement (-34018) at PCA init time.
411
+ //
412
+ // $(AppIdentifierPrefix) is a build-time variable that Xcode expands to
413
+ // "<TeamID>." during code signing, producing e.g.
414
+ // "<TeamID>.com.microsoft.adalcache".
415
+ //
416
+ // For production rewrapped builds the Intune MAM SDK and wrap pipeline set up
417
+ // their own keychain groups; adding this group here is additive and harmless.
418
+ function withMsalIOSEntitlement(config) {
419
+ return withEntitlementsPlist(config, (mod) => {
420
+ const entitlements = mod.modResults;
421
+ const existing = entitlements['keychain-access-groups'] ?? [];
422
+ const msalGroup = '$(AppIdentifierPrefix)com.microsoft.adalcache';
423
+ if (!existing.includes(msalGroup)) {
424
+ entitlements['keychain-access-groups'] = [...existing, msalGroup];
425
+ }
426
+ return mod;
427
+ });
428
+ }
429
+
430
+ // ---------------------------------------------------------------------------
431
+ // android/app/proguard-rules.pro — suppress R8 warnings for optional deps
432
+ // ---------------------------------------------------------------------------
433
+
434
+ const DONTWARN_BLOCK = `
435
+ # ── Injected by power-apps-native-auth config plugin ─────────────────────────────────────────────
436
+ # FindBugs / SpotBugs annotations referenced by Microsoft Identity library — not bundled at runtime.
437
+ -dontwarn edu.umd.cs.findbugs.annotations.**
438
+
439
+ # BouncyCastle optional FIPS / PEM classes referenced by nimbus-jose-jwt — not bundled.
440
+ -dontwarn org.bouncycastle.**
441
+
442
+ # nimbus-jose-jwt references Tink as an optional crypto provider — not bundled at runtime.
443
+ -dontwarn com.google.crypto.tink.**
444
+ # ─────────────────────────────────────────────────────────────────────────────
445
+ `;
446
+
447
+ const DONTWARN_MARKER = 'Injected by power-apps-native-auth config plugin';
448
+
449
+ function withMamProguardDontwarn(config) {
450
+ return withDangerousMod(config, [
451
+ 'android',
452
+ async (cfg) => {
453
+ const proguardPath = path.join(
454
+ cfg.modRequest.platformProjectRoot,
455
+ 'app',
456
+ 'proguard-rules.pro'
457
+ );
458
+
459
+ if (!fs.existsSync(proguardPath)) {
460
+ return cfg;
461
+ }
462
+
463
+ let contents = fs.readFileSync(proguardPath, 'utf8');
464
+
465
+ // Idempotent — only append if not already present
466
+ if (!contents.includes(DONTWARN_MARKER)) {
467
+ contents += DONTWARN_BLOCK;
468
+ fs.writeFileSync(proguardPath, contents, 'utf8');
469
+ }
470
+
471
+ return cfg;
472
+ },
473
+ ]);
474
+ }
475
+
476
+ // ---------------------------------------------------------------------------
477
+ // Composed plugin
478
+ // ---------------------------------------------------------------------------
479
+
480
+ /**
481
+ * @param {import('@expo/config-plugins').ExpoConfig} config
482
+ * @returns {import('@expo/config-plugins').ExpoConfig}
483
+ */
484
+ function withExpoMsalIntune(config) {
485
+ // iOS: register `msauth.<bundleId>` in CFBundleURLTypes for every build —
486
+ // MSAL refuses to initialise without it. See withMsalRedirectScheme.js for
487
+ // the reasoning behind unconditional injection.
488
+ config = withMsalRedirectScheme(config);
489
+
490
+ // Always inject the Maven repositories + flatDir so the native module's AAR
491
+ // dependencies resolve in both dev-client and release builds.
492
+ config = withMamRepositories(config);
493
+
494
+ // The ShrinkWrap flavor is required in both dev-client and release builds
495
+ // because `npm run android` always passes --variant ShrinkWrapDebug.
496
+ config = withShrinkWrapFlavor(config);
497
+
498
+ // iOS: register msauth.<bundleId> URL scheme in CFBundleURLTypes, and add
499
+ // the com.microsoft.adalcache keychain-access-groups entitlement.
500
+ // Both are required by MSAL iOS SDK (v1.1.0+) at PCA init time.
501
+ config = withMsalIOSURLScheme(config);
502
+ config = withMsalIOSEntitlement(config);
503
+
504
+ // In development-player builds, skip the MAM Gradle plugin (no bytecode instrumentation
505
+ // needed) and the proguard rules (R8 is disabled in debug anyway).
506
+ // Also remove any MAM artifacts left by a prior release prebuild so the
507
+ // debugMamifyClasses task is not registered at all.
508
+ if (IS_DEVELOPMENT_PLAYER) {
509
+ config = withMamPluginCleanup(config);
510
+ config = withMamClasspathCleanup(config);
511
+ return config;
512
+ }
513
+
514
+ config = withMamPluginClasspath(config);
515
+ config = withMamPluginApply(config);
516
+ // Inject the MAM SDK AARs as app-level compilation dependencies.
517
+ // The MAM Gradle plugin requires this — it throws RewriteException otherwise.
518
+ config = withMamAppSdkDependency(config);
519
+ config = withMamProguardDontwarn(config);
520
+ // Inject string placeholder values → patched by the AAB+protoc pipeline
521
+ // Inject BrowserTabActivity + msauth intent filter with placeholder values →
522
+ // wrap script replaces host (bundle ID) and path (signature hash) at wrap time
523
+ config = withWrapStringPlaceholders(config);
524
+ config = withMsalBrowserTabActivity(config);
525
+ return config;
526
+ }
527
+
528
+ module.exports = withExpoMsalIntune;
@@ -0,0 +1,53 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Expo config plugin: append `msauth.<bundleId>` to iOS CFBundleURLTypes so
6
+ * MSAL can route its `msauth.<bundleId>://auth` redirect back to the app.
7
+ *
8
+ * Why a plugin and not `ios.infoPlist.CFBundleURLTypes` directly:
9
+ * Expo prebuild auto-adds CFBundleURLTypes entries for the app's `scheme`,
10
+ * bundle id, and the expo-dev-client launcher. Declaring CFBundleURLTypes
11
+ * in `ios.infoPlist` would *replace* that array — breaking deep linking
12
+ * and the dev-launcher. Using `withInfoPlist` we instead append a new
13
+ * entry, keeping Expo's auto-added schemes intact.
14
+ *
15
+ * Why no DEV_CLIENT guard:
16
+ * Without MSAL initialising, the app can't sign in — and MSAL needs this
17
+ * scheme registered in every build that ships its own copy of the SDK.
18
+ * The previous DEV_CLIENT gate caused Expo's incremental prebuild to
19
+ * leave behind a broken Info.plist whenever the env var was missing,
20
+ * surviving until `--clean`. A stale `msauth.<bundleId>` entry in a
21
+ * wrapped artefact is harmless — there's no app listening on it once
22
+ * the bundle id changes — so unconditional injection is the safer
23
+ * default.
24
+ */
25
+
26
+ const { withInfoPlist } = require('@expo/config-plugins');
27
+
28
+ /**
29
+ * @param {import('@expo/config-plugins').ExpoConfig} config
30
+ * @returns {import('@expo/config-plugins').ExpoConfig}
31
+ */
32
+ function withMsalRedirectScheme(config) {
33
+ return withInfoPlist(config, (cfg) => {
34
+ const bundleId = cfg.ios?.bundleIdentifier;
35
+ if (!bundleId) return cfg;
36
+
37
+ const scheme = `msauth.${bundleId}`;
38
+ const urlTypes = cfg.modResults.CFBundleURLTypes ?? [];
39
+
40
+ const exists = urlTypes.some((entry) =>
41
+ Array.isArray(entry?.CFBundleURLSchemes) && entry.CFBundleURLSchemes.includes(scheme),
42
+ );
43
+ if (exists) return cfg;
44
+
45
+ cfg.modResults.CFBundleURLTypes = [
46
+ ...urlTypes,
47
+ { CFBundleURLSchemes: [scheme] },
48
+ ];
49
+ return cfg;
50
+ });
51
+ }
52
+
53
+ module.exports = withMsalRedirectScheme;