@@ -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 ( )
@@ -29,16 +34,23 @@ public class MsalPlugin: CAPPlugin {
2934
3035 @objc func setOptions( _ call: CAPPluginCall ) {
3136 do {
32- // Step 01: Validate all Options have been passed from Plugin bridge
37+ // 1. Sometimes Javascripts Apps cause re-renders resulting in options being set again
38+ // This protects from options being set again
39+ if ( call. getBool ( " guardForRerenders " ) == true ) != nil && self . msalHasOptions == true {
40+ return
41+ }
42+
43+ // 2. Validate all Options have been passed from Plugin bridge
3344 guard let authorityUri = call. getString ( " authority " ) else { return }
3445 guard let clientId = call. getString ( " clientId " ) else { return }
3546 guard let redirectUri = call. getString ( " redirectUri " ) else { return }
3647 guard let authorityUrl = URL ( string: authorityUri ) else { return }
3748
38- // Step 02: If made it this point, then set & create Configuraitons, Authority, & Public Client
49+ // 3. If made it this point, then set & create Configuraitons, Authority, & Public Client
3950 let clientAuthority = try MSALAADAuthority ( url: authorityUrl)
4051 let clientConfiguration = MSALPublicClientApplicationConfig ( clientId: clientId, redirectUri: redirectUri, authority: clientAuthority)
4152
53+ // 4. Set iOS Options
4254 if let isoOptions = call. getObject ( " iosOptions " ) {
4355 if isoOptions [ " enableBiometrics " ] == nil {
4456 self . msalUseBiometrics = false
@@ -72,7 +84,6 @@ public class MsalPlugin: CAPPlugin {
7284 self . msalClient = try MSALPublicClientApplication ( configuration: clientConfiguration)
7385 self . msalPopupScopes = call. getArray ( " scopes " , String . self)
7486 self . msalHasOptions = true
75- self . msalAuthenticated = false
7687
7788 // Step 04: Need to resolve void and send response back to the bridge
7889 call. resolve ( )
@@ -84,18 +95,16 @@ public class MsalPlugin: CAPPlugin {
8495 }
8596 }
8697
87- /*
88-
89- */
98+
9099 @objc func acquireAccessTokenForUser( _ call: CAPPluginCall ) {
91- // Step 01: Check if Options have been set
100+ // 1. Check if Options have been set
92101 if self . msalHasOptions == false {
93102 call. reject ( " AcquireAccessTokenForUser Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
94103 return
95104 }
96105
97106 if self . msalAuthenticated == true {
98- // 1 .
107+ // 2 .
99108 let msalParameters = MSALParameters ( )
100109 msalParameters. completionBlockQueue = DispatchQueue . main
101110
@@ -105,17 +114,21 @@ public class MsalPlugin: CAPPlugin {
105114 }
106115 } )
107116
108- // 2 . Validate and set scopes where passed through options
117+ // 3 . Validate and set scopes where passed through options
109118 guard let tokenScopes = call. getArray ( " scopes " , String . self) else {
110119 call. reject ( " AcquireAccessToken Error: No Scopes were provided " )
111120 return
112121 }
113122
114- // 3 . Set Silent Token Parameters
123+ // 4 . Set Silent Token Parameters
115124 let parameters = MSALSilentTokenParameters ( scopes: tokenScopes, account: self . msalAccount!)
116- parameters. forceRefresh = true
117125
118- // 4. Begin acquireing Access Token for OBO Flow and other Open ID Connect Prtocols
126+ if ( ( call. getBool ( " forceRefresh " ) == true ) != nil ) {
127+ parameters. forceRefresh = true
128+ }
129+
130+
131+ // 5. Begin acquireing Access Token for OBO Flow and other Open ID Connect Prtocols
119132 self . msalClient? . acquireTokenSilent ( with: parameters, completionBlock: { ( response, error) in
120133 if let error = error {
121134 call. error ( " AcquireAccessToken Error: Unable to Acquire Access Token " , error, [
0 commit comments