Skip to content
This repository was archived by the owner on Feb 11, 2023. It is now read-only.

Commit 80ea32e

Browse files
Chase R. Crawford (Tek)Chase R. Crawford (Tek)
authored andcommitted
Fixed rerendering options set and force refresh with token cache
1 parent 60dcb43 commit 80ea32e

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

src/msal-capacitor-plugin/ios/Plugin/Plugin.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import LocalAuthentication
77
public 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, [

src/msal-capacitor-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@assimalign/msal-capacitor-plugin",
33
"version": "1.0.4",
44
"description": "A custom Capacitor Plugin for MSAL targeting Web and IOS Platforms",
5-
"main": "dist/plugin.js",
5+
"main": "dist/index.js",
66
"module": "dist/esm/index.js",
77
"types": "dist/esm/index.d.ts",
88
"scripts": {

src/msal-capacitor-plugin/rollup.config.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ import nodeResolve from '@rollup/plugin-node-resolve';
22

33
export default {
44
input: 'dist/esm/index.js',
5-
output: {
6-
file: 'dist/plugin.js',
7-
format: 'iife',
8-
name: 'capacitorPlugin', // TODO: change this
9-
globals: {
10-
'@capacitor/core': 'capacitorExports',
5+
output: [
6+
{
7+
file: 'dist/plugin.js',
8+
format: 'iife',
9+
name: 'capacitorPlugin', // TODO: change this
10+
globals: {
11+
'@capacitor/core': 'capacitorExports',
12+
},
13+
sourcemap: true
1114
},
12-
sourcemap: true,
13-
},
15+
{
16+
file: 'dist/index.js',
17+
format: 'cjs',
18+
sourcemap: true
19+
}
20+
],
1421
plugins: [
1522
nodeResolve({
1623
// allowlist of dependencies to bundle in

src/msal-capacitor-plugin/src/definitions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface IMsalPluginOptions {
2828
webOptions?: IMsalWebPluginOptions;
2929
iosOptions?: IMsalIosPluginOptions;
3030
androidOptions?: IMsalAndroidPluginOptions;
31+
guardForRerenders?: boolean;
3132
}
3233

3334
export interface IMsalPlugin {
@@ -37,5 +38,5 @@ export interface IMsalPlugin {
3738
logout(): Promise<void>;
3839
acquireUserRoles(): Promise<{results: string[]}>;
3940
acquireAuthenticationResult(): Promise<{results: AuthenticationResult | null}>;
40-
acquireAccessTokenForUser(request?: {scopes: string[]}): Promise<{results: string}>;
41+
acquireAccessTokenForUser(request?: {scopes: string[], forceRefresh?:boolean}): Promise<{results: string}>;
4142
}

src/msal-capacitor-plugin/src/web.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export class MsalPluginWeb extends WebPlugin implements IMsalPlugin {
2525
return new Promise((resolve, reject)=> {
2626
try {
2727
if(options) {
28+
29+
if(options.guardForRerenders === true) {
30+
return
31+
}
32+
2833
this.msalClient = new PublicClientApplication({
2934
auth: {
3035
clientId: options.clientId,
@@ -135,13 +140,14 @@ export class MsalPluginWeb extends WebPlugin implements IMsalPlugin {
135140
})
136141
}
137142

138-
async acquireAccessTokenForUser(request: {scopes: string[]}): Promise<{results: string}> {
143+
async acquireAccessTokenForUser(request: {scopes: string[], forceRefresh?: boolean}): Promise<{results: string}> {
139144
return new Promise(async (resolve, reject)=>{
140145
if(this.msalClient){
141146
try {
142147
let token = await this.msalClient.acquireTokenSilent({
143148
scopes: request.scopes,
144-
account: this.msalResults?.account ?? undefined
149+
account: this.msalResults?.account ?? undefined,
150+
forceRefresh: request.forceRefresh
145151
});
146152
resolve({
147153
results: token.accessToken

0 commit comments

Comments
 (0)