@@ -5,6 +5,8 @@ import MSAL
55@objc ( MsalCap)
66public class MsalPlugin : CAPPlugin {
77
8+ typealias AccountCompletion = ( MSALAccount ? ) -> Void
9+
810 var account : MSALAccount ?
911 var authResults : MSALResult ?
1012 var client : MSALPublicClientApplication ?
@@ -24,38 +26,42 @@ public class MsalPlugin: CAPPlugin {
2426
2527 @objc func setOptions( _ call: CAPPluginCall ) {
2628 do {
27- guard let authorityUri = call. getString ( " authority " ) else {
28- return
29- }
30- guard let clientId = call. getString ( " clientId " ) else {
31- return
32- }
33- guard let redirectUri = call. getString ( " redirectUri " ) else {
34- return
35- }
36- guard let authorityUrl = URL ( string: authorityUri ) else {
37- return
38- }
29+ // Step 01: Validate all Options have been passed from Plugin bridge
30+ guard let authorityUri = call. getString ( " authority " ) else { return }
31+ guard let clientId = call. getString ( " clientId " ) else { return }
32+ guard let redirectUri = call. getString ( " redirectUri " ) else { return }
33+ guard let authorityUrl = URL ( string: authorityUri ) else { return }
3934
35+ // Step 02: If made it this point, then set & create Configuraitons, Authority, & Public Client
4036 let clientAuthority = try MSALAADAuthority ( url: authorityUrl)
4137 let clientConfiguration = MSALPublicClientApplicationConfig ( clientId: clientId, redirectUri: redirectUri, authority: clientAuthority)
4238
39+ // Step 03: Set plugin errors
4340 self . client = try MSALPublicClientApplication ( configuration: clientConfiguration)
4441 self . popupScopes = call. getArray ( " scopes " , String . self)
4542 self . hasOptions = true
4643 self . authenticated = false
44+
45+ // Step 04: Need to resolve void and send response back to the bridge
4746 call. resolve ( )
4847
4948 } catch let error {
50- call. error ( " " , error, [
51- " results " : " Unable to set options "
49+ call. error ( " Initialization Error " , error, [
50+ " results " : " Unable to set options in iOS Plugin "
5251 ] )
5352 }
5453 }
5554
5655 @objc func acquireAccessTokenForUser( _ call: CAPPluginCall ) {
57- if self . authenticated == true && self . authResults != nil && self . client != nil {
56+ // Step 01: Check if Options have been set
57+ if self . hasOptions == false {
58+ call. reject ( " AcquireAccessTokenForUser Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
59+ return
60+ }
5861
62+ if self . authenticated == true {
63+
64+ // Step 01:
5965 let msalParameters = MSALParameters ( )
6066 msalParameters. completionBlockQueue = DispatchQueue . main
6167
@@ -65,11 +71,9 @@ public class MsalPlugin: CAPPlugin {
6571 }
6672 } )
6773
74+ // Step
6875 guard let scopes = call. getArray ( " scopes " , String . self) else {
69- return
70- }
71-
72- guard let account = self . account else {
76+ call. reject ( " AcquireAccessToken Error: No Scopes were provided " )
7377 return
7478 }
7579
@@ -94,12 +98,19 @@ public class MsalPlugin: CAPPlugin {
9498 ] )
9599 } )
96100 } else {
97- call. reject ( " User has not been Authenticated yet. Please Login first before calling this request " )
101+ call. reject ( " AcquireAccessToken Error: User has not been Authenticated yet. Please Login first before calling this request" )
98102 }
99103 }
100104
101105 @objc func acquireUserRoles( _ call: CAPPluginCall ) {
102- if self . hasOptions == true && self . authenticated == true {
106+ // Step 01: Check if Options have been set
107+ if self . hasOptions == false {
108+ call. reject ( " AcquireUserRoles Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
109+ return
110+ }
111+
112+ // Step 02: Check if
113+ if self . authenticated == true {
103114 guard let claims = self . account? . accountClaims else {
104115 call. resolve ( [
105116 " results " : [ ]
@@ -116,10 +127,16 @@ public class MsalPlugin: CAPPlugin {
116127 } else {
117128 call. reject ( " Eiether Plugin Confgiuraitons have not been set or the user has not been authenticated " )
118129 }
119-
120130 }
121131
122132 @objc func acquireAuthenticationResult( _ call: CAPPluginCall ) {
133+ // Step 01: Check if Options have been set
134+ if self . Options == false {
135+ call. reject ( " " )
136+ return
137+ }
138+
139+ // Step 02:
123140 guard let response = self . authResults else {
124141 call. resolve ( [
125142 " results " : [ ]
@@ -151,19 +168,27 @@ public class MsalPlugin: CAPPlugin {
151168 }
152169
153170 @objc func isAuthenticated( _ call: CAPPluginCall ) {
171+ // Step 01: Check if Options have been set
172+ if self . hasOptions == false {
173+ call. reject ( " isAuthenticated Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
174+ return
175+ }
176+
154177 call. resolve ( [
155178 " results " : self . authenticated
156179 ] )
157180 }
158181
159182 @objc func login( _ call: CAPPluginCall ) {
183+ // Step 01: Check if Options have been set
160184 if self . hasOptions == false {
161- call. reject ( " Options have not been set " )
185+ call. reject ( " Login Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
162186 return
163187 }
164188
189+ // Step 03: Dispatch
165190 DispatchQueue . main. async {
166- self . loadAccount { ( account) in
191+ self . setCurrentAccount { ( account) in
167192 guard let currentAccount = self . account else {
168193 self . loginInteractive ( call)
169194 return
@@ -174,18 +199,24 @@ public class MsalPlugin: CAPPlugin {
174199 }
175200
176201 @objc func logout( _ call: CAPPluginCall ) {
202+ // Step 01: Check if Options have been set
203+ if self . hasOptions == false {
204+ call. reject ( " Logout Error: MSAL Plugin Options have not been set yet. Please run 'setOptions' " )
205+ return
206+ }
207+
177208 DispatchQueue . main. async {
178209 self . logoutInteractive ( call)
179210 }
180211 }
181212
182213
183- typealias AccountCompletion = ( MSALAccount ? ) -> Void
214+
184215
185216
186217 // This will check to see if there are any avaiable accounts already cached on the device
187218 // If any account is found it will load it and try to up
188- func loadAccount ( completion: AccountCompletion ? = nil ) {
219+ private func setCurrentAccount ( completion: AccountCompletion ? = nil ) {
189220 let msalParameters = MSALParameters ( )
190221 msalParameters. completionBlockQueue = DispatchQueue . main
191222
@@ -227,7 +258,7 @@ public class MsalPlugin: CAPPlugin {
227258 let signoutParameters = MSALSignoutParameters ( webviewParameters: webViewParameters)
228259
229260 signoutParameters. signoutFromBrowser = true
230- applicationContext . signout ( with: currentAccount, signoutParameters: signoutParameters, completionBlock: { ( success, error) in
261+ self . client ? . signout ( with: currentAccount, signoutParameters: signoutParameters, completionBlock: { ( success, error) in
231262 if let error = error {
232263 call. error ( " Unable to signout " , error, [
233264 " unexpectedError " : error. localizedDescription
@@ -246,7 +277,6 @@ public class MsalPlugin: CAPPlugin {
246277
247278
248279 private func loginInteractive( _ call: CAPPluginCall ) {
249-
250280 if self . hasOptions == true {
251281
252282 let webViewParameters = MSALWebviewParameters ( authPresentationViewController: self . bridge. viewController)
@@ -300,7 +330,7 @@ public class MsalPlugin: CAPPlugin {
300330 }
301331 }
302332
303- func loginSilently( _ account : MSALAccount ! , _ call: CAPPluginCall ) {
333+ private func loginSilently( _ account : MSALAccount ! , _ call: CAPPluginCall ) {
304334 guard let applicationContext = self . client else { return }
305335 let parameters = MSALSilentTokenParameters ( scopes: self . popupScopes ?? [ ] , account: account)
306336 applicationContext. acquireTokenSilent ( with: parameters) { ( response, error) in
@@ -362,3 +392,6 @@ public class MsalPlugin: CAPPlugin {
362392 }
363393 }
364394}
395+
396+
397+
0 commit comments