Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a31c560

Browse files
authored
Merge pull request #4 from BritehouseMobile/fix/Android-invalid-auth
Fix/android invalid auth
2 parents 286dd4d + a26fcb0 commit a31c560

File tree

10 files changed

+153
-22
lines changed

10 files changed

+153
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
## 0.1.0
2+
3+
* Fixed issue on error where plugin would not initialise properly on first method call
4+
5+
16
## 0.0.4
27

38
* Fixed dismiss error message on Android
49

10+
511
## 0.0.3
612

713
* Added error response messages to iOS

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Import the [Flutter Microsoft Authentication package](https://pub.dev/packages/f
2929

3030
```
3131
dependencies:
32-
flutter_microsoft_authentication: ^0.0.4
32+
flutter_microsoft_authentication: ^0.1.0
3333
```
3434

3535
### Configuring MSAL for Android

android/src/main/kotlin/za/co/britehouse/flutter_microsoft_authentication/FlutterMicrosoftAuthenticationPlugin.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
3838

3939
override fun onMethodCall(call: MethodCall, result: Result) {
4040

41-
if (call.method == "signOut") {
42-
signOut(result)
43-
return
44-
}
45-
4641
val scopesArg : ArrayList<String>? = call.argument("kScopes")
4742
val scopes: Array<String>? = scopesArg?.toTypedArray()
4843
val authority: String? = call.argument("kAuthority")
@@ -67,12 +62,12 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
6762
return
6863
}
6964

70-
createSingleAccountPublicClientApplication(configPath)
71-
7265
when(call.method){
7366
"acquireTokenInteractively" -> acquireTokenInteractively(scopes, authority, result)
7467
"acquireTokenSilently" -> acquireTokenSilently(scopes, authority, result)
7568
"loadAccount" -> loadAccount(result)
69+
"signOut" -> signOut(result)
70+
"init" -> initPlugin(configPath)
7671
else -> result.notImplemented()
7772
}
7873

@@ -108,6 +103,10 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
108103
}
109104
}
110105

106+
private fun initPlugin(assetPath: String) {
107+
createSingleAccountPublicClientApplication(assetPath)
108+
}
109+
111110
private fun createSingleAccountPublicClientApplication(assetPath: String) {
112111
val configFile = getConfigFile(assetPath)
113112
val context: Context = mainActivity.applicationContext
@@ -122,6 +121,7 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
122121
* This requires "account_mode" : "SINGLE" in the config json file.
123122
*
124123
*/
124+
Log.d(TAG, "INITIALIZED")
125125
mSingleAccountApp = application
126126
}
127127

@@ -132,20 +132,33 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
132132
}
133133

134134
private fun acquireTokenInteractively(scopes: Array<String>, authority: String, result: Result) {
135+
if (mSingleAccountApp == null) {
136+
result.error("MsalClientException", "Account not initialized", null)
137+
}
138+
135139
return mSingleAccountApp!!.signIn(mainActivity, "", scopes, getAuthInteractiveCallback(result))
136140
}
137141

138142
private fun acquireTokenSilently(scopes: Array<String>, authority: String, result: Result) {
143+
if (mSingleAccountApp == null) {
144+
result.error("MsalClientException", "Account not initialized", null)
145+
}
146+
139147
return mSingleAccountApp!!.acquireTokenSilentAsync(scopes, authority, getAuthSilentCallback(result))
140148
}
141149

142150
private fun signOut(result: Result){
151+
if (mSingleAccountApp == null) {
152+
result.error("MsalClientException", "Account not initialized", null)
153+
}
154+
143155
return mSingleAccountApp!!.signOut(object : ISingleAccountPublicClientApplication.SignOutCallback {
144156
override fun onSignOut() {
145157
result.success("SUCCESS")
146158
}
147159

148160
override fun onError(exception: MsalException) {
161+
Log.e(TAG, exception.message)
149162
result.error("ERROR", exception.errorCode, null)
150163
}
151164
})
@@ -227,6 +240,10 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
227240
}
228241

229242
private fun loadAccount(result: Result) {
243+
if (mSingleAccountApp == null) {
244+
result.error("MsalClientException", "Account not initialized", null)
245+
}
246+
230247
return mSingleAccountApp!!.getCurrentAccountAsync(object :
231248
ISingleAccountPublicClientApplication.CurrentAccountCallback {
232249
override fun onAccountLoaded(activeAccount: IAccount?) {
@@ -239,6 +256,7 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler {
239256
if (currentAccount == null) {
240257
// Perform a cleanup task as the signed-in account changed.
241258
Log.d(TAG, "No Account")
259+
result.success(null)
242260
}
243261
}
244262

example/.flutter-plugins-dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_microsoft_authentication","dependencies":[]}]}

example/android/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.gradle.jvmargs=-Xmx1536M
22

3+
android.enableR8=true

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class _MyAppState extends State<MyApp> {
3333
kScopes: ["User.Read", "User.ReadBasic.All"],
3434
androidConfigAssetPath: "assets/android_auth_config.json"
3535
);
36+
print('INITIALIZED FMA');
3637
}
3738

3839
Future<void> _acquireTokenInteractively() async {

example/pubspec.lock

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
archive:
5+
dependency: transitive
6+
description:
7+
name: archive
8+
url: "https://pub.dartlang.org"
9+
source: hosted
10+
version: "2.0.11"
11+
args:
12+
dependency: transitive
13+
description:
14+
name: args
15+
url: "https://pub.dartlang.org"
16+
source: hosted
17+
version: "1.5.2"
418
async:
519
dependency: transitive
620
description:
721
name: async
822
url: "https://pub.dartlang.org"
923
source: hosted
10-
version: "2.3.0"
24+
version: "2.4.0"
1125
boolean_selector:
1226
dependency: transitive
1327
description:
@@ -29,6 +43,20 @@ packages:
2943
url: "https://pub.dartlang.org"
3044
source: hosted
3145
version: "1.14.11"
46+
convert:
47+
dependency: transitive
48+
description:
49+
name: convert
50+
url: "https://pub.dartlang.org"
51+
source: hosted
52+
version: "2.1.1"
53+
crypto:
54+
dependency: transitive
55+
description:
56+
name: crypto
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "2.1.3"
3260
cupertino_icons:
3361
dependency: "direct main"
3462
description:
@@ -47,7 +75,7 @@ packages:
4775
path: ".."
4876
relative: true
4977
source: path
50-
version: "0.0.3"
78+
version: "0.0.4"
5179
flutter_test:
5280
dependency: "direct dev"
5381
description: flutter
@@ -67,20 +95,27 @@ packages:
6795
url: "https://pub.dartlang.org"
6896
source: hosted
6997
version: "3.1.3"
98+
image:
99+
dependency: transitive
100+
description:
101+
name: image
102+
url: "https://pub.dartlang.org"
103+
source: hosted
104+
version: "2.1.4"
70105
matcher:
71106
dependency: transitive
72107
description:
73108
name: matcher
74109
url: "https://pub.dartlang.org"
75110
source: hosted
76-
version: "0.12.5"
111+
version: "0.12.6"
77112
meta:
78113
dependency: transitive
79114
description:
80115
name: meta
81116
url: "https://pub.dartlang.org"
82117
source: hosted
83-
version: "1.1.7"
118+
version: "1.1.8"
84119
path:
85120
dependency: transitive
86121
description:
@@ -95,6 +130,13 @@ packages:
95130
url: "https://pub.dartlang.org"
96131
source: hosted
97132
version: "1.8.0+1"
133+
petitparser:
134+
dependency: transitive
135+
description:
136+
name: petitparser
137+
url: "https://pub.dartlang.org"
138+
source: hosted
139+
version: "2.4.0"
98140
quiver:
99141
dependency: transitive
100142
description:
@@ -148,7 +190,7 @@ packages:
148190
name: test_api
149191
url: "https://pub.dartlang.org"
150192
source: hosted
151-
version: "0.2.5"
193+
version: "0.2.11"
152194
typed_data:
153195
dependency: transitive
154196
description:
@@ -163,5 +205,12 @@ packages:
163205
url: "https://pub.dartlang.org"
164206
source: hosted
165207
version: "2.0.8"
208+
xml:
209+
dependency: transitive
210+
description:
211+
name: xml
212+
url: "https://pub.dartlang.org"
213+
source: hosted
214+
version: "3.5.0"
166215
sdks:
167-
dart: ">=2.2.2 <3.0.0"
216+
dart: ">=2.4.0 <3.0.0"

lib/flutter_microsoft_authentication.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class FlutterMicrosoftAuthentication {
1919
_kAuthority = kAuthority;
2020
_kScopes = kScopes;
2121
_androidConfigAssetPath = androidConfigAssetPath;
22+
23+
if (Platform.isAndroid)
24+
_channel.invokeMethod("init", _createMethodcallArguments());
2225
}
2326

2427
Map<String, dynamic> _createMethodcallArguments() {
@@ -34,24 +37,28 @@ class FlutterMicrosoftAuthentication {
3437
return res;
3538
}
3639

40+
/// Acquire auth token with interactive flow.
3741
Future<String> get acquireTokenInteractively async {
3842
final String token = await _channel.invokeMethod(
3943
'acquireTokenInteractively', _createMethodcallArguments());
4044
return token;
4145
}
4246

47+
/// Acquire auth token silently.
4348
Future<String> get acquireTokenSilently async {
4449
final String token = await _channel.invokeMethod(
4550
'acquireTokenSilently', _createMethodcallArguments());
4651
return token;
4752
}
4853

54+
/// Android only. Get username of current active account.
4955
Future<String> get loadAccount async {
5056
final result = await _channel.invokeMethod(
5157
'loadAccount', _createMethodcallArguments());
5258
return result;
5359
}
5460

61+
/// Sign out of current active account.
5562
Future<String> get signOut async {
5663
final String token =
5764
await _channel.invokeMethod('signOut', _createMethodcallArguments());

0 commit comments

Comments
 (0)