12
12
using Android . Content . PM ;
13
13
using Android . Util ;
14
14
using Java . Security ;
15
- using Java . Util . Concurrent ;
16
15
using Signature = Android . Content . PM . Signature ;
17
16
using Microsoft . Identity . Client . Core ;
18
17
using Microsoft . Identity . Client . Internal . Broker ;
19
18
using Microsoft . Identity . Client . Utils ;
20
- using Microsoft . Identity . Json . Linq ;
21
- using System . Threading . Tasks ;
22
- using OperationCanceledException = Android . Accounts . OperationCanceledException ;
23
- using AndroidUri = Android . Net . Uri ;
24
- using Android . Database ;
25
- using Microsoft . Identity . Json . Utilities ;
26
- using System . Threading ;
27
19
using Microsoft . Identity . Client . OAuth2 ;
28
- using Microsoft . Identity . Client . Http ;
29
20
using AndroidNative = Android ;
30
- using System . Linq ;
21
+ using System . Text . Json ;
31
22
32
23
namespace Microsoft . Identity . Client . Platforms . Android . Broker
33
24
{
@@ -48,7 +39,7 @@ public AndroidBrokerHelper(Context androidContext, ILoggerAdapter logger)
48
39
_androidContext = androidContext ?? throw new ArgumentNullException ( nameof ( androidContext ) ) ;
49
40
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
50
41
51
- _logger . Verbose ( ( ) => "[Android broker] Getting the Android context for broker request. " ) ;
42
+ _logger . Verbose ( ( ) => "[Android broker] Getting the Android context for broker request. " ) ;
52
43
AndroidAccountManager = AccountManager . Get ( _androidContext ) ;
53
44
}
54
45
@@ -57,7 +48,7 @@ public bool IsBrokerInstalledAndInvokable(AuthorityType authorityType)
57
48
using ( _logger . LogMethodDuration ( ) )
58
49
{
59
50
bool canInvoke = CanSwitchToBroker ( ) ;
60
- _logger . Verbose ( ( ) => "[Android broker] Can invoke broker? " + canInvoke ) ;
51
+ _logger . Verbose ( ( ) => "[Android broker] Can invoke broker? " + canInvoke ) ;
61
52
62
53
return canInvoke ;
63
54
}
@@ -74,7 +65,7 @@ private bool CanSwitchToBroker()
74
65
75
66
//Force this to return true for broker test app
76
67
var authenticator = GetInstalledAuthenticator ( ) ;
77
- return authenticator != null
68
+ return authenticator != null
78
69
&& ! packageName . Equals ( BrokerConstants . PackageName , StringComparison . OrdinalIgnoreCase )
79
70
&& ! packageName
80
71
. Equals ( BrokerConstants . AzureAuthenticatorAppPackageName , StringComparison . OrdinalIgnoreCase ) ;
@@ -120,30 +111,29 @@ public BrokerRequest UpdateBrokerRequestWithAccountData(string accountData, Brok
120
111
string homeAccountId = brokerRequest . HomeAccountId ;
121
112
string localAccountId = brokerRequest . LocalAccountId ;
122
113
123
- dynamic AccountDataList = JArray . Parse ( accountData ) ;
114
+ var accountDataList = JsonHelper . DeserializeFromJson < List < AccountData > > ( accountData ) ;
124
115
125
- foreach ( JObject account in AccountDataList )
126
- {
127
- var accountInfo = account [ BrokerResponseConst . Account ] ;
128
- var accountInfoHomeAccountID = accountInfo [ BrokerResponseConst . HomeAccountId ] ? . ToString ( ) ;
129
- var accountInfoLocalAccountID = accountInfo [ BrokerResponseConst . LocalAccountId ] ? . ToString ( ) ;
116
+ foreach ( AccountData account in accountDataList )
117
+ {
118
+ AccountInfo accountInfo = account . Account ;
119
+ string accountInfoHomeAccountID = accountInfo . HomeAccountId ;
120
+ string accountInfoLocalAccountID = accountInfo . LocalAccountId ;
130
121
131
- if ( string . Equals ( accountInfo [ BrokerResponseConst . UserName ] . ToString ( ) , username , StringComparison . OrdinalIgnoreCase ) )
132
- {
133
- // TODO: broker request should be immutable!
134
- brokerRequest . HomeAccountId = accountInfoHomeAccountID ;
135
- brokerRequest . LocalAccountId = accountInfoLocalAccountID ;
136
- _logger . Info ( "[Android broker] Found broker account in Android account manager using the provided login hint. " ) ;
137
- return brokerRequest ;
138
- }
122
+ if ( string . Equals ( accountInfo . UserName , username , StringComparison . OrdinalIgnoreCase ) )
123
+ {
124
+ brokerRequest . HomeAccountId = accountInfoHomeAccountID ;
125
+ brokerRequest . LocalAccountId = accountInfoLocalAccountID ;
126
+ _logger . Info ( "[Android broker] Found broker account in Android account manager using the provided login hint. " ) ;
127
+ return brokerRequest ;
128
+ }
139
129
140
- if ( string . Equals ( accountInfoHomeAccountID , homeAccountId , StringComparison . Ordinal ) &&
141
- string . Equals ( accountInfoLocalAccountID , localAccountId , StringComparison . Ordinal ) )
142
- {
143
- _logger . Info ( "[Android broker] Found broker account in Android account manager using the provided account. " ) ;
144
- return brokerRequest ;
145
- }
130
+ if ( string . Equals ( accountInfoHomeAccountID , homeAccountId , StringComparison . Ordinal ) &&
131
+ string . Equals ( accountInfoLocalAccountID , localAccountId , StringComparison . Ordinal ) )
132
+ {
133
+ _logger . Info ( "[Android broker] Found broker account in Android account manager using the provided account. " ) ;
134
+ return brokerRequest ;
146
135
}
136
+ }
147
137
148
138
_logger . Info ( "[Android broker] The requested account does not exist in the Android account manager. " ) ;
149
139
throw new MsalUiRequiredException ( MsalError . NoAndroidBrokerAccountFound , MsalErrorMessage . NoAndroidBrokerAccountFound ) ;
@@ -158,24 +148,24 @@ public IReadOnlyList<IAccount> ExtractBrokerAccountsFromAccountData(string accou
158
148
159
149
if ( ! string . IsNullOrEmpty ( accountData ) )
160
150
{
161
- dynamic authResult = JArray . Parse ( accountData ) ;
151
+ var accountDataList = JsonHelper . DeserializeFromJson < List < AccountData > > ( accountData ) ;
162
152
163
- foreach ( JObject account in authResult )
153
+ foreach ( AccountData account in accountDataList )
164
154
{
165
- if ( account . ContainsKey ( BrokerResponseConst . Account ) )
155
+ var accountInfo = account . Account ;
156
+
157
+ if ( accountInfo != null && accountInfo . HomeAccountId != null )
166
158
{
167
- var accountInfo = account [ BrokerResponseConst . Account ] ;
168
159
IAccount iAccount = new Account (
169
- accountInfo . Value < string > ( BrokerResponseConst . HomeAccountId ) ?? string . Empty ,
170
- accountInfo . Value < string > ( BrokerResponseConst . UserName ) ?? string . Empty ,
171
- accountInfo . Value < string > ( BrokerResponseConst . Environment ) ?? string . Empty ) ;
160
+ accountInfo . HomeAccountId ,
161
+ accountInfo . UserName ?? string . Empty ,
162
+ accountInfo . Environment ?? string . Empty ) ;
172
163
brokerAccounts . Add ( iAccount ) ;
173
164
}
174
165
}
175
166
}
176
167
177
168
_logger . Info ( ( ) => "[Android broker] Found " + brokerAccounts . Count + " accounts in the account manager. " ) ;
178
-
179
169
return brokerAccounts ;
180
170
}
181
171
@@ -258,7 +248,7 @@ public Bundle CreateSilentBrokerBundle(BrokerRequest brokerRequest)
258
248
public Bundle CreateBrokerAccountBundle ( BrokerRequest brokerRequest )
259
249
{
260
250
_logger . InfoPii (
261
- ( ) => "[Android broker] CreateBrokerAccountBundle: " + JsonHelper . SerializeToJson ( brokerRequest ) ,
251
+ ( ) => "[Android broker] CreateBrokerAccountBundle: " + JsonHelper . SerializeToJson ( brokerRequest ) ,
262
252
( ) => "Enable PII to see the broker account bundle request. " ) ;
263
253
Bundle bundle = new Bundle ( ) ;
264
254
@@ -387,7 +377,7 @@ private AuthenticatorDescription GetInstalledAuthenticator()
387
377
if ( authenticator . Type . Equals ( BrokerConstants . BrokerAccountType , StringComparison . OrdinalIgnoreCase )
388
378
&& VerifySignature ( authenticator . PackageName ) )
389
379
{
390
- _logger . Verbose ( ( ) => "[Android broker] Found the Authenticator on the device. " ) ;
380
+ _logger . Verbose ( ( ) => "[Android broker] Found the Authenticator on the device. " ) ;
391
381
return authenticator ;
392
382
}
393
383
}
0 commit comments