Skip to content

Commit becc82b

Browse files
committed
Update Android and iOS
1 parent 1c350b1 commit becc82b

11 files changed

+167
-255
lines changed

build/platform_and_feature_flags.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project>
2-
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetCore)' or '$(TargetFramework)' == '$(TargetFrameworkNet)'">
3-
<DefineConstants>$(DefineConstants);SUPPORTS_SYSTEM_TEXT_JSON;NET_CORE;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_BROKER;SUPPORTS_WIN32;</DefineConstants>
2+
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)'">
3+
<DefineConstants>$(DefineConstants)SUPPORTS_MTLS;NET_CORE;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_BROKER;SUPPORTS_WIN32;</DefineConstants>
44
</PropertyGroup>
55
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop462)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)' or '$(TargetFramework)' == '$(TargetFrameworkNetStandard)'">
66
<DefineConstants>$(DefineConstants);SUPPORTS_OTEL;</DefineConstants>
77
</PropertyGroup>
8-
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)'">
8+
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)'">
99
<DefineConstants>$(DefineConstants);SUPPORTS_MTLS;</DefineConstants>
1010
</PropertyGroup>
1111
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetAndroid)'">
@@ -15,7 +15,7 @@
1515
<DefineConstants>$(DefineConstants);SUPPORTS_BROKER;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_WIN32</DefineConstants>
1616
</PropertyGroup>
1717
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetIos)'">
18-
<DefineConstants>$(DefineConstants);iOS;SUPPORTS_BROKER</DefineConstants>
18+
<DefineConstants>$(DefineConstants);iOS;SUPPORTS_BROKER</DefineConstants>
1919
</PropertyGroup>
2020
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetStandard)'">
2121
<DefineConstants>$(DefineConstants);NETSTANDARD;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_BROKER;SUPPORTS_CUSTOM_CACHE;SUPPORTS_WIN32;</DefineConstants>

src/client/Microsoft.Identity.Client/Platforms/Android/Broker/AndroidAccountManagerBroker.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Microsoft.Identity.Client.Internal.Broker;
1313
using Microsoft.Identity.Client.OAuth2;
1414
using Microsoft.Identity.Client.UI;
15-
using Microsoft.Identity.Json.Linq;
1615
using Microsoft.Identity.Client.Internal.Requests;
1716
using Microsoft.Identity.Client.ApiConfig.Parameters;
1817
using Microsoft.Identity.Client.Http;
@@ -350,14 +349,16 @@ public async Task InitiateBrokerHandshakeAsync()
350349
return;
351350
}
352351

353-
dynamic errorResult = JObject.Parse(helloRequestResult.GetString(BrokerConstants.BrokerResultV2));
352+
string errorResponse = helloRequestResult.GetString(BrokerConstants.BrokerResultV2);
354353
string errorCode = null;
355354
string errorDescription = null;
356355

357-
if (!string.IsNullOrEmpty(errorResult))
356+
if (!string.IsNullOrEmpty(errorResponse))
358357
{
359-
errorCode = errorResult[BrokerResponseConst.BrokerErrorCode]?.ToString();
360-
string errorMessage = errorResult[BrokerResponseConst.BrokerErrorMessage]?.ToString();
358+
// serialize the error response to get the error code and error message withouth dynamic
359+
var errorResult = JsonHelper.DeserializeFromJson<BrokerErrorResponse>(errorResponse);
360+
errorCode = errorResult.BrokerErrorCode;
361+
string errorMessage = errorResult.BrokerErrorMessage;
361362
errorDescription = $"[Android broker] An error occurred during hand shake with the broker. Error: {errorCode} Error Message: {errorMessage}";
362363
}
363364
else

src/client/Microsoft.Identity.Client/Platforms/Android/Broker/AndroidBrokerHelper.cs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@
1212
using Android.Content.PM;
1313
using Android.Util;
1414
using Java.Security;
15-
using Java.Util.Concurrent;
1615
using Signature = Android.Content.PM.Signature;
1716
using Microsoft.Identity.Client.Core;
1817
using Microsoft.Identity.Client.Internal.Broker;
1918
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;
2719
using Microsoft.Identity.Client.OAuth2;
28-
using Microsoft.Identity.Client.Http;
2920
using AndroidNative = Android;
30-
using System.Linq;
21+
using System.Text.Json;
3122

3223
namespace 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
}

src/client/Microsoft.Identity.Client/Platforms/Android/Broker/AndroidBrokerInteractiveResponseHelper.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
4+
using System.Text.Json;
85
using System.Threading;
9-
using System.Threading.Tasks;
106
using Android.Content;
117
using Microsoft.Identity.Client.Core;
128
using Microsoft.Identity.Client.Http;
139
using Microsoft.Identity.Client.Internal.Broker;
1410
using Microsoft.Identity.Client.OAuth2;
15-
using Microsoft.Identity.Json.Linq;
11+
using Microsoft.Identity.Client.Utils;
1612

1713
namespace Microsoft.Identity.Client.Platforms.Android.Broker
1814
{
@@ -58,14 +54,15 @@ internal static void SetBrokerResult(Intent data, int resultCode, ILoggerAdapter
5854
case (int)BrokerResponseCode.BrowserCodeError:
5955
unreliableLogger?.Info("[Android broker] Response received - error. ");
6056

61-
dynamic errorResult = JObject.Parse(data.GetStringExtra(BrokerConstants.BrokerResultV2));
57+
var errorResult = JsonHelper.DeserializeFromJson<BrokerErrorResult>
58+
(data.GetStringExtra(BrokerConstants.BrokerResultV2));
6259

6360
string error;
6461
string errorDescription;
6562
if (errorResult != null)
6663
{
67-
error = errorResult[BrokerResponseConst.BrokerErrorCode]?.ToString();
68-
errorDescription = errorResult[BrokerResponseConst.BrokerErrorMessage]?.ToString();
64+
error = errorResult.BrokerErrorCode;
65+
errorDescription = errorResult.BrokerErrorMessage;
6966

7067
unreliableLogger?.Error($"[Android broker] error: {error} errorDescription {errorDescription}. ");
7168
}
@@ -76,21 +73,22 @@ internal static void SetBrokerResult(Intent data, int resultCode, ILoggerAdapter
7673
unreliableLogger?.Error("[Android broker] Error response received, but not error could be extracted. ");
7774
}
7875

79-
var httpResponse = new HttpResponse();
80-
//TODO: figure out how to get status code properly deserialized from JObject
81-
httpResponse.Body = errorResult[BrokerResponseConst.BrokerHttpBody]?.ToString();
76+
var httpResponse = new HttpResponse
77+
{
78+
Body = errorResult?.BrokerHttpBody
79+
};
8280

8381
InteractiveBrokerTokenResponse = new MsalTokenResponse
8482
{
8583
Error = error,
8684
ErrorDescription = errorDescription,
87-
SubError = errorResult[BrokerResponseConst.BrokerSubError],
85+
SubError = errorResult?.BrokerSubError,
8886
HttpResponse = httpResponse,
8987
CorrelationId = InteractiveRequestCorrelationId,
90-
TenantId = errorResult[BrokerResponseConst.TenantId]?.ToString(),
91-
Upn = errorResult[BrokerResponseConst.UserName]?.ToString(),
92-
AccountUserId = errorResult[BrokerResponseConst.LocalAccountId]?.ToString(),
93-
AuthorityUrl = errorResult[BrokerResponseConst.Authority]?.ToString(),
88+
TenantId = errorResult?.TenantId,
89+
Upn = errorResult?.UserName,
90+
AccountUserId = errorResult?.LocalAccountId,
91+
AuthorityUrl = errorResult?.Authority,
9492
};
9593
break;
9694
default:

src/client/Microsoft.Identity.Client/Platforms/Android/Broker/AndroidContentProviderBroker.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.Identity.Client.OAuth2;
1818
using Microsoft.Identity.Client.UI;
1919
using Microsoft.Identity.Client.Utils;
20-
using Microsoft.Identity.Json.Linq;
2120
using AndroidNative = Android;
2221
using AndroidUri = Android.Net.Uri;
2322

@@ -73,16 +72,18 @@ public string GetProtocolKeyFromHandShakeResult(Bundle bundleResult)
7372
return negotiatedBrokerProtocalKey;
7473
}
7574

76-
dynamic errorResult = JObject.Parse(bundleResult?.GetString(BrokerConstants.BrokerResultV2));
75+
string errorResponse = bundleResult.GetString(BrokerConstants.BrokerResultV2);
7776
string errorCode = null;
7877
string errorDescription = null;
7978

80-
if (!string.IsNullOrEmpty(errorResult))
79+
if (!string.IsNullOrEmpty(errorResponse))
8180
{
82-
errorCode = errorResult[BrokerResponseConst.BrokerErrorCode]?.ToString();
83-
string errorMessage = errorResult[BrokerResponseConst.BrokerErrorMessage]?.ToString();
81+
// serialize the error response to get the error code and error message withouth dynamic
82+
var errorResult = JsonHelper.DeserializeFromJson<BrokerErrorResponse>(errorResponse);
83+
errorCode = errorResult.BrokerErrorCode;
84+
string errorMessage = errorResult.BrokerErrorMessage;
8485
errorDescription = $"[Android broker] An error occurred during hand shake with the broker. Error: {errorCode} Error Message: {errorMessage}";
85-
}
86+
}
8687
else
8788
{
8889
errorCode = BrokerConstants.BrokerUnknownErrorCode;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Text.Json.Serialization;
5+
using Microsoft.Identity.Client.Internal.Broker;
6+
7+
namespace Microsoft.Identity.Client.Platforms.Android.Broker
8+
{
9+
[Preserve(AllMembers = true)]
10+
internal class BrokerErrorResponse
11+
{
12+
[JsonPropertyName(BrokerResponseConst.BrokerErrorCode)]
13+
public string BrokerErrorCode { get; set; }
14+
15+
[JsonPropertyName(BrokerResponseConst.BrokerErrorMessage)]
16+
public string BrokerErrorMessage { get; set; }
17+
}
18+
}

0 commit comments

Comments
 (0)