1212using Android . Content . PM ;
1313using Android . Util ;
1414using Java . Security ;
15- using Java . Util . Concurrent ;
1615using Signature = Android . Content . PM . Signature ;
1716using Microsoft . Identity . Client . Core ;
1817using Microsoft . Identity . Client . Internal . Broker ;
1918using 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 ;
2719using Microsoft . Identity . Client . OAuth2 ;
28- using Microsoft . Identity . Client . Http ;
2920using AndroidNative = Android ;
30- using System . Linq ;
21+ using System . Text . Json ;
3122
3223namespace Microsoft . Identity . Client . Platforms . Android . Broker
3324{
@@ -48,7 +39,7 @@ public AndroidBrokerHelper(Context androidContext, ILoggerAdapter logger)
4839 _androidContext = androidContext ?? throw new ArgumentNullException ( nameof ( androidContext ) ) ;
4940 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
5041
51- _logger . Verbose ( ( ) => "[Android broker] Getting the Android context for broker request. " ) ;
42+ _logger . Verbose ( ( ) => "[Android broker] Getting the Android context for broker request. " ) ;
5243 AndroidAccountManager = AccountManager . Get ( _androidContext ) ;
5344 }
5445
@@ -57,7 +48,7 @@ public bool IsBrokerInstalledAndInvokable(AuthorityType authorityType)
5748 using ( _logger . LogMethodDuration ( ) )
5849 {
5950 bool canInvoke = CanSwitchToBroker ( ) ;
60- _logger . Verbose ( ( ) => "[Android broker] Can invoke broker? " + canInvoke ) ;
51+ _logger . Verbose ( ( ) => "[Android broker] Can invoke broker? " + canInvoke ) ;
6152
6253 return canInvoke ;
6354 }
@@ -74,7 +65,7 @@ private bool CanSwitchToBroker()
7465
7566 //Force this to return true for broker test app
7667 var authenticator = GetInstalledAuthenticator ( ) ;
77- return authenticator != null
68+ return authenticator != null
7869 && ! packageName . Equals ( BrokerConstants . PackageName , StringComparison . OrdinalIgnoreCase )
7970 && ! packageName
8071 . Equals ( BrokerConstants . AzureAuthenticatorAppPackageName , StringComparison . OrdinalIgnoreCase ) ;
@@ -120,30 +111,29 @@ public BrokerRequest UpdateBrokerRequestWithAccountData(string accountData, Brok
120111 string homeAccountId = brokerRequest . HomeAccountId ;
121112 string localAccountId = brokerRequest . LocalAccountId ;
122113
123- dynamic AccountDataList = JArray . Parse ( accountData ) ;
114+ var accountDataList = JsonHelper . DeserializeFromJson < List < AccountData > > ( accountData ) ;
124115
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 ;
130121
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+ }
139129
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 ;
146135 }
136+ }
147137
148138 _logger . Info ( "[Android broker] The requested account does not exist in the Android account manager. " ) ;
149139 throw new MsalUiRequiredException ( MsalError . NoAndroidBrokerAccountFound , MsalErrorMessage . NoAndroidBrokerAccountFound ) ;
@@ -158,24 +148,24 @@ public IReadOnlyList<IAccount> ExtractBrokerAccountsFromAccountData(string accou
158148
159149 if ( ! string . IsNullOrEmpty ( accountData ) )
160150 {
161- dynamic authResult = JArray . Parse ( accountData ) ;
151+ var accountDataList = JsonHelper . DeserializeFromJson < List < AccountData > > ( accountData ) ;
162152
163- foreach ( JObject account in authResult )
153+ foreach ( AccountData account in accountDataList )
164154 {
165- if ( account . ContainsKey ( BrokerResponseConst . Account ) )
155+ var accountInfo = account . Account ;
156+
157+ if ( accountInfo != null && accountInfo . HomeAccountId != null )
166158 {
167- var accountInfo = account [ BrokerResponseConst . Account ] ;
168159 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 ) ;
172163 brokerAccounts . Add ( iAccount ) ;
173164 }
174165 }
175166 }
176167
177168 _logger . Info ( ( ) => "[Android broker] Found " + brokerAccounts . Count + " accounts in the account manager. " ) ;
178-
179169 return brokerAccounts ;
180170 }
181171
@@ -258,7 +248,7 @@ public Bundle CreateSilentBrokerBundle(BrokerRequest brokerRequest)
258248 public Bundle CreateBrokerAccountBundle ( BrokerRequest brokerRequest )
259249 {
260250 _logger . InfoPii (
261- ( ) => "[Android broker] CreateBrokerAccountBundle: " + JsonHelper . SerializeToJson ( brokerRequest ) ,
251+ ( ) => "[Android broker] CreateBrokerAccountBundle: " + JsonHelper . SerializeToJson ( brokerRequest ) ,
262252 ( ) => "Enable PII to see the broker account bundle request. " ) ;
263253 Bundle bundle = new Bundle ( ) ;
264254
@@ -387,7 +377,7 @@ private AuthenticatorDescription GetInstalledAuthenticator()
387377 if ( authenticator . Type . Equals ( BrokerConstants . BrokerAccountType , StringComparison . OrdinalIgnoreCase )
388378 && VerifySignature ( authenticator . PackageName ) )
389379 {
390- _logger . Verbose ( ( ) => "[Android broker] Found the Authenticator on the device. " ) ;
380+ _logger . Verbose ( ( ) => "[Android broker] Found the Authenticator on the device. " ) ;
391381 return authenticator ;
392382 }
393383 }
0 commit comments