@@ -7,14 +7,19 @@ import LocalAuthentication
77public class MsalPlugin : CAPPlugin {
88
99 typealias AccountCompletion = ( MSALAccount ? ) -> Void
10+
11+ var msalInitializationCount = Int ( )
1012
1113 var msalAccount : MSALAccount ?
1214 var msalResults : MSALResult ?
1315 var msalClient : MSALPublicClientApplication ?
16+ // Struct Initialization Default is False, setting this in setOptions resulted in error due to reset of options when a rerender occurrs
17+ //
1418 var msalAuthenticated = Bool ( )
1519 var msalUseBiometrics = Bool ( )
1620 var msalPopupScopes : [ String ] ?
1721 var msalHasOptions = Bool ( )
22+
1823 var msalLocalAuthContext = LAContext ( )
1924
2025 var dateFormatter = DateFormatter ( )
@@ -28,39 +33,48 @@ public class MsalPlugin: CAPPlugin {
2833
2934 @objc func setOptions( _ call: CAPPluginCall ) {
3035 do {
31- // Step 01: Validate all Options have been passed from Plugin bridge
36+ // 1. Sometimes Javascripts Apps cause re-renders resulting in options being set again
37+ // This protects from options being set again
38+ if ( call. getBool ( " guardForRerenders " ) ?? false ) == true && self . msalHasOptions == true {
39+ return
40+ }
41+
42+ // 2. Validate all Options have been passed from Plugin bridge
3243 guard let authorityUri = call. getString ( " authority " ) else { return }
3344 guard let clientId = call. getString ( " clientId " ) else { return }
3445 guard let redirectUri = call. getString ( " redirectUri " ) else { return }
3546 guard let authorityUrl = URL ( string: authorityUri ) else { return }
3647
37- // Step 02: If made it this point, then set & create Configuraitons, Authority, & Public Client
48+ // 3. If made it this point, then set & create Configuraitons, Authority, & Public Client
3849 let clientAuthority = try MSALAADAuthority ( url: authorityUrl)
3950 let clientConfiguration = MSALPublicClientApplicationConfig ( clientId: clientId, redirectUri: redirectUri, authority: clientAuthority)
4051
52+ // 4. Set iOS Options
4153 if let isoOptions = call. getObject ( " iosOptions " ) {
4254 if isoOptions [ " enableBiometrics " ] == nil {
4355 self . msalUseBiometrics = false
4456 } else {
4557 self . msalUseBiometrics = isoOptions [ " enableBiometrics " ] as! Bool ;
4658 }
59+
60+ var tokenCache = " "
4761
48- var tokenCache = " "
62+ #if os(iOS)
63+ if isoOptions [ " keyShareLocation " ] == nil {
64+ tokenCache = " com.microsoft.adalcache "
65+ } else {
66+ tokenCache = isoOptions [ " keyShareLocation " ] as! String
67+ }
68+ #else
69+ if isoOptions [ " keyShareLocation " ] == nil {
70+ tokenCache = " com.microsoft.identity.universalstorage "
71+ } else {
72+ tokenCache = isoOptions [ " keyShareLocation " ] as! String
73+ }
74+ #endif
75+ clientConfiguration. cacheConfig. keychainSharingGroup = tokenCache
4976
50- #if os(iOS)
51- if isoOptions [ " keyShareLocation " ] == nil {
52- tokenCache = " com.microsoft.adalcache "
53- } else {
54- tokenCache = isoOptions [ " keyShareLocation " ] as! String
55- }
56- #else
57- if isoOptions [ " keyShareLocation " ] == nil {
58- tokenCache = " com.microsoft.identity.universalstorage "
59- } else {
60- tokenCache = isoOptions [ " keyShareLocation " ] as! String
61- }
62- #endif
63- clientConfiguration. cacheConfig. keychainSharingGroup = tokenCache
77+
6478 } else {
6579 self . msalUseBiometrics = false
6680 }
@@ -69,7 +83,6 @@ public class MsalPlugin: CAPPlugin {
6983 self . msalClient = try MSALPublicClientApplication ( configuration: clientConfiguration)
7084 self . msalPopupScopes = call. getArray ( " scopes " , String . self)
7185 self . msalHasOptions = true
72- self . msalAuthenticated = false
7386
7487 // Step 04: Need to resolve void and send response back to the bridge
7588 call. resolve ( )
@@ -81,18 +94,16 @@ public class MsalPlugin: CAPPlugin {
8194 }
8295 }
8396
84- /*
85-
86- */
97+
8798 @objc func acquireAccessTokenForUser( _ call: CAPPluginCall ) {
88- // Step 01: Check if Options have been set
99+ // 1. Check if Options have been set
89100 if self . msalHasOptions == false {
90101 call. reject ( " AcquireAccessTokenForUser Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
91102 return
92103 }
93104
94105 if self . msalAuthenticated == true {
95- // 1 .
106+ // 2 .
96107 let msalParameters = MSALParameters ( )
97108 msalParameters. completionBlockQueue = DispatchQueue . main
98109
@@ -102,17 +113,20 @@ public class MsalPlugin: CAPPlugin {
102113 }
103114 } )
104115
105- // 2 . Validate and set scopes where passed through options
116+ // 3 . Validate and set scopes where passed through options
106117 guard let tokenScopes = call. getArray ( " scopes " , String . self) else {
107118 call. reject ( " AcquireAccessToken Error: No Scopes were provided " )
108119 return
109120 }
110121
111- // 3 . Set Silent Token Parameters
122+ // 4 . Set Silent Token Parameters
112123 let parameters = MSALSilentTokenParameters ( scopes: tokenScopes, account: self . msalAccount!)
113- parameters. forceRefresh = true
124+ if ( call. getBool ( " forceRefresh " ) ?? false ) == true {
125+ parameters. forceRefresh = true
126+ }
127+
114128
115- // 4 . Begin acquireing Access Token for OBO Flow and other Open ID Connect Prtocols
129+ // 5 . Begin acquireing Access Token for OBO Flow and other Open ID Connect Prtocols
116130 self . msalClient? . acquireTokenSilent ( with: parameters, completionBlock: { ( response, error) in
117131 if let error = error {
118132 call. error ( " AcquireAccessToken Error: Unable to Acquire Access Token " , error, [
@@ -208,7 +222,7 @@ public class MsalPlugin: CAPPlugin {
208222 if success {
209223 DispatchQueue . main. async {
210224 self . setCurrentAccount { ( account) in
211- guard let currentAccount = self . msalAccount else {
225+ guard let currentAccount = account else {
212226 self . loginInteractive ( call)
213227 return
214228 }
@@ -254,6 +268,10 @@ public class MsalPlugin: CAPPlugin {
254268
255269 self . msalClient? . getCurrentAccount ( with: msalParameters, completionBlock: { ( currentAccount, previousAccount, error) in
256270 if let error = error {
271+ if let completion = completion {
272+ completion ( nil )
273+ }
274+
257275 return
258276 }
259277
@@ -312,7 +330,8 @@ public class MsalPlugin: CAPPlugin {
312330 #endif
313331
314332 let parameters = MSALInteractiveTokenParameters ( scopes: self . msalPopupScopes!, webviewParameters: webViewParameters)
315- parameters. promptType = . selectAccount
333+ parameters. promptType = . default
334+ parameters. completionBlockQueue = DispatchQueue . main
316335
317336 // 3. Acquire Token view Redirect Login through Microsft Identity Platform
318337 self . msalClient? . acquireToken ( with: parameters) { ( response, error) in
0 commit comments