@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.
- package/LICENSE +112 -0
- package/README.md +54 -0
- package/android/build.gradle +79 -0
- package/android/src/main/AndroidManifest.xml +31 -0
- package/android/src/main/java/expo/modules/msalintune/ExpoMsalIntuneModule.kt +488 -0
- package/app.plugin.js +4 -0
- package/expo-module.config.json +9 -0
- package/ios/ExpoMsalIntune.podspec +27 -0
- package/ios/ExpoMsalIntuneAppDelegate.swift +31 -0
- package/ios/ExpoMsalIntuneModule.swift +382 -0
- package/lib/NOTICE.txt +5 -0
- package/lib/commonjs/AuthConfig.types.js +1 -0
- package/lib/commonjs/ExpoMsalIntune.types.js +1 -0
- package/lib/commonjs/ExpoMsalIntuneModule.js +50 -0
- package/lib/commonjs/index.js +217 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/module/AuthConfig.types.js +1 -0
- package/lib/module/ExpoMsalIntune.types.js +1 -0
- package/lib/module/ExpoMsalIntuneModule.js +44 -0
- package/lib/module/index.js +207 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/commonjs/AuthConfig.types.d.ts +76 -0
- package/lib/typescript/commonjs/ExpoMsalIntune.types.d.ts +80 -0
- package/lib/typescript/commonjs/ExpoMsalIntuneModule.d.ts +92 -0
- package/lib/typescript/commonjs/index.d.ts +125 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/module/AuthConfig.types.d.ts +76 -0
- package/lib/typescript/module/ExpoMsalIntune.types.d.ts +80 -0
- package/lib/typescript/module/ExpoMsalIntuneModule.d.ts +92 -0
- package/lib/typescript/module/index.d.ts +125 -0
- package/lib/typescript/module/package.json +1 -0
- package/package.json +93 -0
- package/plugin/index.js +528 -0
- package/plugin/withMsalRedirectScheme.js +53 -0
- package/scripts/setup-android-sdk.js +122 -0
- package/scripts/setup-ios-frameworks.js +100 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import MSAL
|
|
3
|
+
import IntuneMAMSwift
|
|
4
|
+
|
|
5
|
+
// MARK: - Error helpers
|
|
6
|
+
|
|
7
|
+
private enum MsalIntuneError: Error {
|
|
8
|
+
case notInitialized
|
|
9
|
+
case accountNotFound(String)
|
|
10
|
+
case interactionRequired
|
|
11
|
+
case noResult
|
|
12
|
+
case intuneError(String)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
extension MsalIntuneError: LocalizedError {
|
|
16
|
+
var errorDescription: String? {
|
|
17
|
+
switch self {
|
|
18
|
+
case .notInitialized:
|
|
19
|
+
return "ExpoMsalIntune: call initializeClient() before acquiring tokens"
|
|
20
|
+
case .accountNotFound(let id):
|
|
21
|
+
return "ExpoMsalIntune: account '\(id)' not found in the MSAL cache"
|
|
22
|
+
case .interactionRequired:
|
|
23
|
+
return "ExpoMsalIntune: silent token acquisition failed — user interaction required"
|
|
24
|
+
case .noResult:
|
|
25
|
+
return "ExpoMsalIntune: MSAL returned no result"
|
|
26
|
+
case .intuneError(let msg):
|
|
27
|
+
return "ExpoMsalIntune (Intune): \(msg)"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// MARK: - Module
|
|
33
|
+
|
|
34
|
+
public class ExpoMsalIntuneModule: Module {
|
|
35
|
+
private var pca: MSALPublicClientApplication?
|
|
36
|
+
/// Stored at initializeClient time; controls which WebView type is used for
|
|
37
|
+
/// interactive sign-in. true → WKWebView (no Info.plist URL scheme needed).
|
|
38
|
+
/// false → ASWebAuthenticationSession (requires msauth.{bundleId}://auth scheme).
|
|
39
|
+
private var useEmbeddedWebView: Bool = true
|
|
40
|
+
/// When true, prefer the Microsoft Authenticator broker for auth.
|
|
41
|
+
/// Requires msauth.{bundleId}://auth registered in CFBundleURLTypes (done by
|
|
42
|
+
/// the wrap pipeline). Keep false for dev client builds.
|
|
43
|
+
private var useBroker: Bool = false
|
|
44
|
+
|
|
45
|
+
// MARK: Module definition
|
|
46
|
+
|
|
47
|
+
public func definition() -> ModuleDefinition {
|
|
48
|
+
Name("ExpoMsalIntune")
|
|
49
|
+
|
|
50
|
+
// -----------------------------------------------------------------------
|
|
51
|
+
// initializeClient
|
|
52
|
+
// -----------------------------------------------------------------------
|
|
53
|
+
AsyncFunction("initializeClient") {
|
|
54
|
+
(clientId: String, authority: String, redirectUri: String, useEmbeddedWebView: Bool, useBroker: Bool) in
|
|
55
|
+
|
|
56
|
+
guard let authorityURL = URL(string: authority) else {
|
|
57
|
+
throw NSError(
|
|
58
|
+
domain: "ExpoMsalIntune",
|
|
59
|
+
code: -1,
|
|
60
|
+
userInfo: [NSLocalizedDescriptionKey: "Invalid authority URL: \(authority)"]
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Store so acquireTokenInteractive can use the correct WebView type.
|
|
65
|
+
self.useEmbeddedWebView = useEmbeddedWebView
|
|
66
|
+
self.useBroker = useBroker
|
|
67
|
+
|
|
68
|
+
let msalAuthority = try MSALAuthority(url: authorityURL)
|
|
69
|
+
|
|
70
|
+
// iOS MSAL SDK (v1+) rejects any http(s):// redirect URI at PCA init time
|
|
71
|
+
// with error -42011 "unsupported scheme", even in WKWebView mode. Override
|
|
72
|
+
// any http(s) URI passed from JS (e.g. the nativeclient fallback that may
|
|
73
|
+
// arrive via a stale embedded bundle) with the correct msauth scheme.
|
|
74
|
+
// computeMsalRedirectUri() in JS also returns this value, but the native
|
|
75
|
+
// override here makes the fix robust against stale embedded bundles.
|
|
76
|
+
let iosRedirectUri: String
|
|
77
|
+
if redirectUri.isEmpty || redirectUri.hasPrefix("http://") || redirectUri.hasPrefix("https://") {
|
|
78
|
+
guard let bundleId = Bundle.main.bundleIdentifier else {
|
|
79
|
+
throw MsalIntuneError.intuneError("initializeClient: cannot determine bundle identifier for redirect URI")
|
|
80
|
+
}
|
|
81
|
+
iosRedirectUri = "msauth.\(bundleId)://auth"
|
|
82
|
+
} else {
|
|
83
|
+
iosRedirectUri = redirectUri
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let config = MSALPublicClientApplicationConfig(
|
|
87
|
+
clientId: clientId,
|
|
88
|
+
redirectUri: iosRedirectUri,
|
|
89
|
+
authority: msalAuthority
|
|
90
|
+
)
|
|
91
|
+
// Enable broker (Microsoft Authenticator) when requested.
|
|
92
|
+
// The wrap pipeline adds the required msauth.{bundleId}://auth URL scheme
|
|
93
|
+
// to Info.plist and CFBundleURLTypes — that's what makes the broker
|
|
94
|
+
// callback possible at runtime.
|
|
95
|
+
MSALGlobalConfig.brokerAvailability = useBroker ? .auto : .none
|
|
96
|
+
do {
|
|
97
|
+
self.pca = try MSALPublicClientApplication(configuration: config)
|
|
98
|
+
} catch let err as NSError {
|
|
99
|
+
let details = "domain=\(err.domain) code=\(err.code) userInfo=\(err.userInfo)"
|
|
100
|
+
throw MsalIntuneError.intuneError("MSAL PCA init failed: \(details)")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Persist the resolved MSAL parameters so out-of-process / background
|
|
104
|
+
// consumers (e.g. powerapps-geolocation-control's native sync engine)
|
|
105
|
+
// can recreate a PCA that shares this app's Keychain token cache. Mirrors
|
|
106
|
+
// the Android module, which writes msal_config.json to the cache dir.
|
|
107
|
+
// The geolocation reader expects flat keys: clientId / redirectUri / authority.
|
|
108
|
+
Self.writeMsalConfig(clientId: clientId, redirectUri: iosRedirectUri, authority: authority)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// -----------------------------------------------------------------------
|
|
112
|
+
// acquireTokenInteractive
|
|
113
|
+
// -----------------------------------------------------------------------
|
|
114
|
+
AsyncFunction("acquireTokenInteractive") {
|
|
115
|
+
(scopes: [String], loginHint: String?, prompt: String?) async throws -> [String: Any] in
|
|
116
|
+
|
|
117
|
+
guard let pca = self.pca else { throw MsalIntuneError.notInitialized }
|
|
118
|
+
|
|
119
|
+
return try await withCheckedThrowingContinuation { continuation in
|
|
120
|
+
// topViewController() and pca.acquireToken() both require the main
|
|
121
|
+
// thread. ExpoModulesCore dispatches AsyncFunction closures on a
|
|
122
|
+
// background queue, so we must hop to main explicitly.
|
|
123
|
+
DispatchQueue.main.async {
|
|
124
|
+
guard let viewController = self.topViewController() else {
|
|
125
|
+
continuation.resume(throwing: NSError(
|
|
126
|
+
domain: "ExpoMsalIntune",
|
|
127
|
+
code: -2,
|
|
128
|
+
userInfo: [NSLocalizedDescriptionKey: "No view controller available for interactive sign-in"]
|
|
129
|
+
))
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
|
|
134
|
+
// Both modes use msauth.{bundleId}://auth as the redirect URI (set at PCA
|
|
135
|
+
// init time). The difference is in how the OS handles the callback:
|
|
136
|
+
// .wkWebView (dev / dev-player): MSAL intercepts navigation inside the
|
|
137
|
+
// WKWebView — no CFBundleURLTypes URL scheme registration required.
|
|
138
|
+
// .default (ASWebAuthenticationSession / broker, production): the OS
|
|
139
|
+
// dispatches via the URL scheme handler, so CFBundleURLTypes must
|
|
140
|
+
// include msauth.{bundleId} — added by the wrap pipeline at rewrap time.
|
|
141
|
+
webviewParameters.webviewType = self.useEmbeddedWebView ? .wkWebView : .default
|
|
142
|
+
let params = MSALInteractiveTokenParameters(scopes: scopes, webviewParameters: webviewParameters)
|
|
143
|
+
|
|
144
|
+
if let hint = loginHint { params.loginHint = hint }
|
|
145
|
+
if let promptStr = prompt {
|
|
146
|
+
params.promptType = self.mapPromptType(promptStr)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
pca.acquireToken(with: params) { result, error in
|
|
150
|
+
if let error = error as NSError? {
|
|
151
|
+
let details = "domain=\(error.domain) code=\(error.code) userInfo=\(error.userInfo)"
|
|
152
|
+
continuation.resume(throwing: MsalIntuneError.intuneError("MSAL acquireToken failed: \(details)"))
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
guard let result = result else {
|
|
156
|
+
continuation.resume(throwing: MsalIntuneError.noResult)
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
continuation.resume(returning: self.serializeResult(result))
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// -----------------------------------------------------------------------
|
|
166
|
+
// acquireTokenSilent
|
|
167
|
+
// -----------------------------------------------------------------------
|
|
168
|
+
AsyncFunction("acquireTokenSilent") {
|
|
169
|
+
(scopes: [String], accountIdentifier: String, forceRefresh: Bool) async throws -> [String: Any] in
|
|
170
|
+
|
|
171
|
+
guard let pca = self.pca else { throw MsalIntuneError.notInitialized }
|
|
172
|
+
|
|
173
|
+
let account = try self.findAccount(pca: pca, identifier: accountIdentifier)
|
|
174
|
+
|
|
175
|
+
return try await withCheckedThrowingContinuation { continuation in
|
|
176
|
+
let params = MSALSilentTokenParameters(scopes: scopes, account: account)
|
|
177
|
+
params.forceRefresh = forceRefresh
|
|
178
|
+
|
|
179
|
+
pca.acquireTokenSilent(with: params) { result, error in
|
|
180
|
+
if let error = error {
|
|
181
|
+
let nsError = error as NSError
|
|
182
|
+
// Surface interaction-required errors distinctly so callers can
|
|
183
|
+
// fall back to interactive.
|
|
184
|
+
if nsError.domain == MSALErrorDomain,
|
|
185
|
+
nsError.code == MSALError.interactionRequired.rawValue {
|
|
186
|
+
continuation.resume(throwing: MsalIntuneError.interactionRequired)
|
|
187
|
+
} else {
|
|
188
|
+
continuation.resume(throwing: error)
|
|
189
|
+
}
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
guard let result = result else {
|
|
193
|
+
continuation.resume(throwing: MsalIntuneError.noResult)
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
continuation.resume(returning: self.serializeResult(result))
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// -----------------------------------------------------------------------
|
|
202
|
+
// getAccounts
|
|
203
|
+
// -----------------------------------------------------------------------
|
|
204
|
+
AsyncFunction("getAccounts") { () throws -> [[String: Any]] in
|
|
205
|
+
guard let pca = self.pca else { throw MsalIntuneError.notInitialized }
|
|
206
|
+
let accounts = try pca.allAccounts()
|
|
207
|
+
return accounts.map { self.serializeAccount($0) }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// -----------------------------------------------------------------------
|
|
211
|
+
// signOut
|
|
212
|
+
// -----------------------------------------------------------------------
|
|
213
|
+
AsyncFunction("signOut") {
|
|
214
|
+
(accountIdentifier: String, signoutFromBrowser: Bool) async throws in
|
|
215
|
+
|
|
216
|
+
guard let pca = self.pca else { throw MsalIntuneError.notInitialized }
|
|
217
|
+
let account = try self.findAccount(pca: pca, identifier: accountIdentifier)
|
|
218
|
+
|
|
219
|
+
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
|
|
220
|
+
// pca.signout(with:) must run on the main thread. When signoutFromBrowser
|
|
221
|
+
// is true MSAL needs a view controller to present the browser; we obtain
|
|
222
|
+
// one from topViewController() which is also a UIKit operation.
|
|
223
|
+
DispatchQueue.main.async {
|
|
224
|
+
guard let viewController = self.topViewController() else {
|
|
225
|
+
continuation.resume(throwing: MsalIntuneError.intuneError("No top view controller for signout"))
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
|
|
229
|
+
let params = MSALSignoutParameters(webviewParameters: webviewParameters)
|
|
230
|
+
params.signoutFromBrowser = signoutFromBrowser
|
|
231
|
+
|
|
232
|
+
pca.signout(with: account, signoutParameters: params) { _, error in
|
|
233
|
+
if let error = error {
|
|
234
|
+
continuation.resume(throwing: error)
|
|
235
|
+
} else {
|
|
236
|
+
continuation.resume()
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// -----------------------------------------------------------------------
|
|
244
|
+
// Intune: registerAndEnrollAccount
|
|
245
|
+
// -----------------------------------------------------------------------
|
|
246
|
+
AsyncFunction("registerAndEnrollAccount") {
|
|
247
|
+
(upn: String, _aadId: String, _tenantId: String) throws in
|
|
248
|
+
|
|
249
|
+
// IntuneMAMEnrollmentManager.instance() triggers enrollment.
|
|
250
|
+
// The delegate (set at app level via IntuneMAMEnrollmentDelegate)
|
|
251
|
+
// receives enrollment status updates asynchronously.
|
|
252
|
+
let enrollmentManager = IntuneMAMEnrollmentManager.instance()
|
|
253
|
+
enrollmentManager.registerAndEnrollAccountId(_aadId)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// -----------------------------------------------------------------------
|
|
257
|
+
// Intune: deRegisterAndUnenrollAccount
|
|
258
|
+
// -----------------------------------------------------------------------
|
|
259
|
+
AsyncFunction("deRegisterAndUnenrollAccount") { (upn: String) throws in
|
|
260
|
+
let enrollmentManager = IntuneMAMEnrollmentManager.instance()
|
|
261
|
+
enrollmentManager.deRegisterAndUnenrollAccountId(upn, withWipe: false)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// -----------------------------------------------------------------------
|
|
265
|
+
// getWrapConfig — synchronous.
|
|
266
|
+
// Reads pipeline-patchable values from Info.plist at runtime.
|
|
267
|
+
// The production pipeline patches IntuneMAMSettings.ADALClientId (and
|
|
268
|
+
// related keys) in the app's Info.plist via `plutil -replace`.
|
|
269
|
+
// getWrapConfig() surfaces those patched values to JS, overriding the
|
|
270
|
+
// placeholder values baked into the HBC bundle.
|
|
271
|
+
//
|
|
272
|
+
// Caller (AuthContext) only uses these values in release builds (!__DEV__).
|
|
273
|
+
// Dev client builds continue to use auth.config.json directly.
|
|
274
|
+
// -----------------------------------------------------------------------
|
|
275
|
+
Function("getWrapConfig") { () -> [String: String] in
|
|
276
|
+
let info = Bundle.main.infoDictionary ?? [:]
|
|
277
|
+
let mam = info["IntuneMAMSettings"] as? [String: Any] ?? [:]
|
|
278
|
+
return [
|
|
279
|
+
"clientId": mam["ADALClientId"] as? String ?? "",
|
|
280
|
+
"redirectUri": mam["ADALRedirectUri"] as? String ?? "",
|
|
281
|
+
"tenantId": mam["ADALTenantId"] as? String ?? "",
|
|
282
|
+
]
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// -----------------------------------------------------------------------
|
|
286
|
+
// computeMsalRedirectUri — synchronous.
|
|
287
|
+
// Returns msauth.<bundleId>://auth derived from the installed app's own
|
|
288
|
+
// bundle identifier. This means the redirect URI never has to be baked
|
|
289
|
+
// into the JS bundle (HBC), so the wrap script does not need to patch it.
|
|
290
|
+
// -----------------------------------------------------------------------
|
|
291
|
+
Function("computeMsalRedirectUri") { () -> String in
|
|
292
|
+
let bundleId = Bundle.main.bundleIdentifier ?? "unknown"
|
|
293
|
+
return "msauth.\(bundleId)://auth"
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// MARK: - Private helpers
|
|
298
|
+
|
|
299
|
+
/// Writes the resolved MSAL parameters to <Caches>/msal_config.json so that
|
|
300
|
+
/// out-of-process consumers (the powerapps-geolocation-control native sync
|
|
301
|
+
/// engine) can recreate a PCA sharing this app's Keychain token cache.
|
|
302
|
+
/// Uses the flat key shape (clientId / redirectUri / authority) that the
|
|
303
|
+
/// geolocation MsalAuthService reader expects. Static + best-effort: any
|
|
304
|
+
/// failure is logged and ignored so it never blocks sign-in.
|
|
305
|
+
private static func writeMsalConfig(clientId: String, redirectUri: String, authority: String) {
|
|
306
|
+
guard let cacheDir = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {
|
|
307
|
+
NSLog("[ExpoMsalIntune] writeMsalConfig: no caches directory")
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
let configPath = (cacheDir as NSString).appendingPathComponent("msal_config.json")
|
|
311
|
+
let config: [String: Any] = [
|
|
312
|
+
"clientId": clientId,
|
|
313
|
+
"redirectUri": redirectUri,
|
|
314
|
+
"authority": authority,
|
|
315
|
+
]
|
|
316
|
+
do {
|
|
317
|
+
let data = try JSONSerialization.data(withJSONObject: config, options: [])
|
|
318
|
+
try data.write(to: URL(fileURLWithPath: configPath), options: .atomic)
|
|
319
|
+
NSLog("[ExpoMsalIntune] writeMsalConfig: wrote %@", configPath)
|
|
320
|
+
} catch {
|
|
321
|
+
NSLog("[ExpoMsalIntune] writeMsalConfig: failed: %@", error.localizedDescription)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private func findAccount(pca: MSALPublicClientApplication, identifier: String) throws -> MSALAccount {
|
|
326
|
+
let accounts = try pca.allAccounts()
|
|
327
|
+
guard let account = accounts.first(where: { $0.identifier == identifier }) else {
|
|
328
|
+
throw MsalIntuneError.accountNotFound(identifier)
|
|
329
|
+
}
|
|
330
|
+
return account
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private func serializeAccount(_ account: MSALAccount) -> [String: Any] {
|
|
334
|
+
var claims: String = "{}"
|
|
335
|
+
if let rawClaims = account.accountClaims,
|
|
336
|
+
let data = try? JSONSerialization.data(withJSONObject: rawClaims),
|
|
337
|
+
let str = String(data: data, encoding: .utf8) {
|
|
338
|
+
claims = str
|
|
339
|
+
}
|
|
340
|
+
return [
|
|
341
|
+
"identifier": account.identifier ?? "",
|
|
342
|
+
"username": account.username ?? "",
|
|
343
|
+
"tenantId": account.tenantProfiles?.first?.tenantId ?? "",
|
|
344
|
+
"claims": claims,
|
|
345
|
+
]
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private func serializeResult(_ result: MSALResult) -> [String: Any] {
|
|
349
|
+
let expiresOn = result.expiresOn.map { $0.timeIntervalSince1970 * 1000 } ?? 0
|
|
350
|
+
return [
|
|
351
|
+
"accessToken": result.accessToken,
|
|
352
|
+
"idToken": result.idToken ?? "",
|
|
353
|
+
"expiresOn": expiresOn,
|
|
354
|
+
"scopes": result.scopes,
|
|
355
|
+
"account": serializeAccount(result.account),
|
|
356
|
+
]
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private func mapPromptType(_ prompt: String) -> MSALPromptType {
|
|
360
|
+
switch prompt {
|
|
361
|
+
case "login": return .login
|
|
362
|
+
case "consent": return .consent
|
|
363
|
+
case "select_account": return .selectAccount
|
|
364
|
+
case "none": return .default
|
|
365
|
+
default: return .default
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
private func topViewController() -> UIViewController? {
|
|
370
|
+
guard let windowScene = UIApplication.shared.connectedScenes
|
|
371
|
+
.compactMap({ $0 as? UIWindowScene })
|
|
372
|
+
.first(where: { $0.activationState == .foregroundActive }),
|
|
373
|
+
let window = windowScene.windows.first(where: { $0.isKeyWindow })
|
|
374
|
+
else { return nil }
|
|
375
|
+
|
|
376
|
+
var top = window.rootViewController
|
|
377
|
+
while let presented = top?.presentedViewController {
|
|
378
|
+
top = presented
|
|
379
|
+
}
|
|
380
|
+
return top
|
|
381
|
+
}
|
|
382
|
+
}
|
package/lib/NOTICE.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
exports.mapNativeAccount = mapNativeAccount;
|
|
8
|
+
exports.mapNativeResult = mapNativeResult;
|
|
9
|
+
var _expoModulesCore = require("expo-modules-core");
|
|
10
|
+
/**
|
|
11
|
+
* power-apps-native-auth — native module bindings.
|
|
12
|
+
*
|
|
13
|
+
* This module is resolved at runtime via expo-modules-core. On iOS and
|
|
14
|
+
* Android it delegates to the native implementations; on web or in test
|
|
15
|
+
* environments it throws, which surfaces as a clear error rather than a
|
|
16
|
+
* silent no-op.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// requireNativeModule throws if the native module is not linked — the error
|
|
20
|
+
// message is user-friendly (mentions expo-modules-core and the module name).
|
|
21
|
+
const ExpoMsalIntuneModule = (0, _expoModulesCore.requireNativeModule)('ExpoMsalIntune');
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Helpers: map native shapes to public types
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
function mapNativeAccount(native) {
|
|
28
|
+
let claims = {};
|
|
29
|
+
try {
|
|
30
|
+
claims = JSON.parse(native.claims);
|
|
31
|
+
} catch {
|
|
32
|
+
// leave claims empty if JSON is malformed
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
identifier: native.identifier,
|
|
36
|
+
username: native.username,
|
|
37
|
+
tenantId: native.tenantId,
|
|
38
|
+
claims
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function mapNativeResult(native) {
|
|
42
|
+
return {
|
|
43
|
+
accessToken: native.accessToken,
|
|
44
|
+
idToken: native.idToken || undefined,
|
|
45
|
+
expiresOn: native.expiresOn,
|
|
46
|
+
scopes: native.scopes,
|
|
47
|
+
account: mapNativeAccount(native.account)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var _default = exports.default = ExpoMsalIntuneModule;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PublicClientApplication = void 0;
|
|
7
|
+
exports.buildMsalConfig = buildMsalConfig;
|
|
8
|
+
exports.computeMsalRedirectUri = computeMsalRedirectUri;
|
|
9
|
+
exports.deRegisterAndUnenrollAccount = deRegisterAndUnenrollAccount;
|
|
10
|
+
exports.default = void 0;
|
|
11
|
+
exports.getWrapConfig = getWrapConfig;
|
|
12
|
+
exports.initializeFromConfig = initializeFromConfig;
|
|
13
|
+
exports.registerAndEnrollAccount = registerAndEnrollAccount;
|
|
14
|
+
var _ExpoMsalIntuneModule = _interopRequireWildcard(require("./ExpoMsalIntuneModule.js"));
|
|
15
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
16
|
+
/**
|
|
17
|
+
* power-apps-native-auth — public API.
|
|
18
|
+
*
|
|
19
|
+
* Drop-in replacement for react-native-msal: exports a default
|
|
20
|
+
* `PublicClientApplication` class with the same interface used by
|
|
21
|
+
* power-apps-native-player/src/auth/msalAuth.native.ts, plus Intune MAM
|
|
22
|
+
* helpers for enrollment/unenrollment.
|
|
23
|
+
*
|
|
24
|
+
* Metro alias (metro.config.js):
|
|
25
|
+
* 'react-native-msal' → '@microsoft/power-apps-native-auth'
|
|
26
|
+
*
|
|
27
|
+
* This means the tarball's msalAuth.native.ts continues to work without
|
|
28
|
+
* patching.
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// PublicClientApplication
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Wraps the native MSAL PublicClientApplication. Construct once, call
|
|
38
|
+
* `init()`, then reuse for the app's lifetime.
|
|
39
|
+
*
|
|
40
|
+
* API is intentionally compatible with react-native-msal v4 so that existing
|
|
41
|
+
* callers require zero changes.
|
|
42
|
+
*/
|
|
43
|
+
class PublicClientApplication {
|
|
44
|
+
_initialized = false;
|
|
45
|
+
constructor(config) {
|
|
46
|
+
this._config = config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** True after `init()` has completed successfully. */
|
|
50
|
+
get isInitialized() {
|
|
51
|
+
return this._initialized;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create the native MSAL PCA instance. Must be called before any other
|
|
56
|
+
* method.
|
|
57
|
+
*/
|
|
58
|
+
async init() {
|
|
59
|
+
const useEmbeddedWebView = this._config.androidConfigOptions?.authorization_user_agent === 'WEBVIEW';
|
|
60
|
+
await _ExpoMsalIntuneModule.default.initializeClient(this._config.auth.clientId, this._config.auth.authority, this._config.auth.redirectUri ?? '', useEmbeddedWebView, this._config.useBroker ?? false);
|
|
61
|
+
this._initialized = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Launch an interactive sign-in / consent dialog.
|
|
66
|
+
*/
|
|
67
|
+
async acquireToken(params) {
|
|
68
|
+
if (!this._initialized) {
|
|
69
|
+
throw new Error('ExpoMsalIntune: call init() before acquiring tokens');
|
|
70
|
+
}
|
|
71
|
+
const native = await _ExpoMsalIntuneModule.default.acquireTokenInteractive(params.scopes, params.loginHint ?? null, params.prompt ?? null);
|
|
72
|
+
return (0, _ExpoMsalIntuneModule.mapNativeResult)(native);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Silently acquire a token from the MSAL cache or broker.
|
|
77
|
+
* Falls back to interactive if the cache cannot satisfy the request — the
|
|
78
|
+
* caller should catch `MSALError.interactionRequired` and call
|
|
79
|
+
* `acquireToken` instead.
|
|
80
|
+
*/
|
|
81
|
+
async acquireTokenSilent(params) {
|
|
82
|
+
if (!this._initialized) {
|
|
83
|
+
throw new Error('ExpoMsalIntune: call init() before acquiring tokens');
|
|
84
|
+
}
|
|
85
|
+
const native = await _ExpoMsalIntuneModule.default.acquireTokenSilent(params.scopes, params.account.identifier, params.forceRefresh ?? false);
|
|
86
|
+
return (0, _ExpoMsalIntuneModule.mapNativeResult)(native);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Return all accounts from the MSAL token cache. */
|
|
90
|
+
async getAccounts() {
|
|
91
|
+
const nativeAccounts = await _ExpoMsalIntuneModule.default.getAccounts();
|
|
92
|
+
return nativeAccounts.map(_ExpoMsalIntuneModule.mapNativeAccount);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Remove the account from the MSAL cache and optionally end the browser
|
|
97
|
+
* SSO session.
|
|
98
|
+
*/
|
|
99
|
+
async signOut(params) {
|
|
100
|
+
await _ExpoMsalIntuneModule.default.signOut(params.account.identifier, params.signoutFromBrowser ?? false);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// Intune MAM helpers
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Register a signed-in account with Intune MAM and initiate device/app
|
|
111
|
+
* enrollment. Call this immediately after a successful `acquireToken` to
|
|
112
|
+
* apply any Intune conditional-access policies.
|
|
113
|
+
*
|
|
114
|
+
* On iOS: calls `IntuneMAMEnrollmentManager.registerAndEnrollAccount`.
|
|
115
|
+
* On Android: calls `MAMEnrollmentManager.registerAndEnrollAccount`.
|
|
116
|
+
*/
|
|
117
|
+
exports.PublicClientApplication = PublicClientApplication;
|
|
118
|
+
async function registerAndEnrollAccount(params) {
|
|
119
|
+
await _ExpoMsalIntuneModule.default.registerAndEnrollAccount(params.upn, params.aadId ?? '', params.tenantId ?? '');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Remove an account from Intune MAM enrollment. Call this on sign-out.
|
|
124
|
+
*/
|
|
125
|
+
async function deRegisterAndUnenrollAccount(upn) {
|
|
126
|
+
await _ExpoMsalIntuneModule.default.deRegisterAndUnenrollAccount(upn);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Compute the MSAL redirect URI from the app's own identity at runtime.
|
|
131
|
+
* - Android: `msauth://<packageName>/<base64-sha1-cert-hash>`
|
|
132
|
+
* - iOS: `msauth.<bundleId>://auth`
|
|
133
|
+
*
|
|
134
|
+
* Synchronous. Use this instead of a hardcoded redirect URI in auth.config.json
|
|
135
|
+
* so the wrap script never needs to patch the Hermes bytecode bundle.
|
|
136
|
+
*/
|
|
137
|
+
function computeMsalRedirectUri() {
|
|
138
|
+
return _ExpoMsalIntuneModule.default.computeMsalRedirectUri();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Read pipeline-patchable auth configuration from native platform sources at
|
|
143
|
+
* runtime.
|
|
144
|
+
*
|
|
145
|
+
* - Android: reads `adal_client_id` / `adal_redirect_uri` string resources
|
|
146
|
+
* patched in `resources.pb` by the production AAB+protoc pipeline.
|
|
147
|
+
* - iOS: reads `ADALClientId` / `ADALRedirectUri` from `Info.plist`
|
|
148
|
+
* `IntuneMAMSettings`, patched by the pipeline via `plutil -replace`.
|
|
149
|
+
* The Intune MAM SDK also reads these keys natively for enrollment.
|
|
150
|
+
*
|
|
151
|
+
* Synchronous — no I/O after the first call (OS caches resource lookups).
|
|
152
|
+
*
|
|
153
|
+
* **Usage pattern (in AuthContext):**
|
|
154
|
+
* ```ts
|
|
155
|
+
* const wrapCfg = getWrapConfig();
|
|
156
|
+
* // Placeholder tag means base build not yet wrapped; fall back to auth.config.json.
|
|
157
|
+
* const clientId = (wrapCfg.clientId && !wrapCfg.clientId.includes('_TAG'))
|
|
158
|
+
* ? wrapCfg.clientId
|
|
159
|
+
* : msalConfig.clientId;
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
function getWrapConfig() {
|
|
163
|
+
return _ExpoMsalIntuneModule.default.getWrapConfig();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Default export matches react-native-msal's default export.
|
|
167
|
+
var _default = exports.default = PublicClientApplication; // ---------------------------------------------------------------------------
|
|
168
|
+
// initializeFromConfig
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
/**
|
|
171
|
+
* Create and initialize a `PublicClientApplication` from an `AuthConfig`
|
|
172
|
+
* object — typically imported directly from `auth.config.json`.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* import authConfig from '../auth.config.json';
|
|
177
|
+
* import { initializeFromConfig } from '@microsoft/power-apps-native-auth';
|
|
178
|
+
*
|
|
179
|
+
* const pca = await initializeFromConfig(authConfig);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
async function initializeFromConfig(config) {
|
|
183
|
+
const authority = `https://login.microsoftonline.com/${config.msal.tenantId}`;
|
|
184
|
+
const pca = new PublicClientApplication({
|
|
185
|
+
auth: {
|
|
186
|
+
clientId: config.msal.clientId,
|
|
187
|
+
authority,
|
|
188
|
+
redirectUri: config.msal.redirectUri
|
|
189
|
+
},
|
|
190
|
+
useBroker: config.msal.useBroker ?? false,
|
|
191
|
+
androidConfigOptions: {
|
|
192
|
+
authorization_user_agent: config.msal.useEmbeddedWebView ? 'WEBVIEW' : 'BROWSER'
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
await pca.init();
|
|
196
|
+
return pca;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Build a `MSALPublicClientApplicationConfig` from an `AuthConfig` without
|
|
201
|
+
* creating or initializing a PCA instance. Useful when you need the raw config
|
|
202
|
+
* object (e.g. to pass into a custom auth module).
|
|
203
|
+
*/
|
|
204
|
+
function buildMsalConfig(config) {
|
|
205
|
+
const authority = `https://login.microsoftonline.com/${config.msal.tenantId}`;
|
|
206
|
+
return {
|
|
207
|
+
auth: {
|
|
208
|
+
clientId: config.msal.clientId,
|
|
209
|
+
authority,
|
|
210
|
+
redirectUri: config.msal.redirectUri
|
|
211
|
+
},
|
|
212
|
+
useBroker: config.msal.useBroker ?? false,
|
|
213
|
+
androidConfigOptions: {
|
|
214
|
+
authorization_user_agent: config.msal.useEmbeddedWebView ? 'WEBVIEW' : 'BROWSER'
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|