Skip to content

Commit

Permalink
Move static initialization outside ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Feb 10, 2020
1 parent aa4ae5d commit 0ebd4a8
Showing 1 changed file with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Android.Accounts;
using Android.App;
using Android.Content;
using AndroidNative = Android;
using Microsoft.Identity.Client.Core;
using Microsoft.Identity.Client.Internal.Broker;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.UI;
using Microsoft.Identity.Client.Utils;
using System.Globalization;
using Microsoft.Identity.Json.Linq;
using Microsoft.Identity.Json;
using Android.OS;

namespace Microsoft.Identity.Client.Platforms.Android
{
Expand All @@ -29,14 +22,14 @@ internal class AndroidBroker : IBroker
// When the broker responds, we cannot correlate back to a started task.
// So we make a simplifying assumption - only one broker open session can exist at a time
// This semaphore is static to enforce this
private static SemaphoreSlim _readyForResponse;
private static SemaphoreSlim s_readyForResponse = new SemaphoreSlim(0);

private static MsalTokenResponse _androidBrokerTokenResponse = null;
private static MsalTokenResponse s_androidBrokerTokenResponse = null;
//Since the correlation ID is not returned from the broker response, it must be stored at the beginning of the authentication call and reinjected into the response at the end.
private static string _correlationId;
private static string s_correlationId;
private readonly AndroidBrokerHelper _brokerHelper;
private readonly ICoreLogger _logger;
private Activity _activity;
private readonly Activity _activity;

public AndroidBroker(CoreUIParent uiParent, ICoreLogger logger)
{
Expand All @@ -55,8 +48,8 @@ public bool CanInvokeBroker()

public async Task<MsalTokenResponse> AcquireTokenUsingBrokerAsync(Dictionary<string, string> brokerPayload)
{
_androidBrokerTokenResponse = null;
_correlationId = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.CorrelationId);
s_androidBrokerTokenResponse = null;
s_correlationId = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.CorrelationId);

try
{
Expand All @@ -73,19 +66,16 @@ public async Task<MsalTokenResponse> AcquireTokenUsingBrokerAsync(Dictionary<str
throw new MsalClientException(MsalError.AndroidBrokerOperationFailed, ex.Message, ex);
}

return _androidBrokerTokenResponse;
return s_androidBrokerTokenResponse;
}

private async Task AcquireTokenInternalAsync(IDictionary<string, string> brokerPayload)
{
try
{
_readyForResponse = new SemaphoreSlim(0);

await _brokerHelper.InitiateBrokerHandshakeAsync(_activity).ConfigureAwait(false);

Context mContext = Application.Context;

brokerPayload[BrokerParameter.BrokerAccountName] = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.LoginHint);

// Don't send silent background request if account information is not provided
Expand All @@ -94,8 +84,8 @@ private async Task AcquireTokenInternalAsync(IDictionary<string, string> brokerP
{
_logger.Verbose("User is specified for silent token request. Starting silent broker request");
string silentResult = await _brokerHelper.GetBrokerAuthTokenSilentlyAsync(brokerPayload, _activity).ConfigureAwait(false);
_androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(silentResult);
_readyForResponse?.Release();
s_androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(silentResult);
s_readyForResponse?.Release();
return;
}
else
Expand Down Expand Up @@ -135,11 +125,11 @@ private async Task AcquireTokenInternalAsync(IDictionary<string, string> brokerP
_logger.ErrorPiiWithPrefix(ex, "Broker invocation failed.");


_readyForResponse.Release();
s_readyForResponse.Release();
throw;
}

await _readyForResponse.WaitAsync().ConfigureAwait(false);
await s_readyForResponse.WaitAsync().ConfigureAwait(false);
}

internal static void SetBrokerResult(Intent data, int resultCode)
Expand All @@ -153,20 +143,20 @@ internal static void SetBrokerResult(Intent data, int resultCode)

if (resultCode != (int)BrokerResponseCode.ResponseReceived)
{
_androidBrokerTokenResponse = new MsalTokenResponse
s_androidBrokerTokenResponse = new MsalTokenResponse
{
Error = MsalError.BrokerResponseReturnedError,
ErrorDescription = data.GetStringExtra(BrokerConstants.BrokerResultV2),
};
}
else
{
_androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(data.GetStringExtra(BrokerConstants.BrokerResultV2));
s_androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(data.GetStringExtra(BrokerConstants.BrokerResultV2));
}
}
finally
{
_readyForResponse.Release();
s_readyForResponse.Release();
}
}

Expand All @@ -182,7 +172,7 @@ private static MsalTokenResponse CreateMsalTokenResponseFromResult(string broker
response.Add(BrokerResponseConst.Authority, authResult[BrokerResponseConst.Authority].ToString());
response.Add(BrokerResponseConst.AccessToken, authResult[BrokerResponseConst.AccessToken].ToString());
response.Add(BrokerResponseConst.IdToken, authResult[BrokerResponseConst.IdToken].ToString());
response.Add(BrokerResponseConst.CorrelationId, _correlationId);
response.Add(BrokerResponseConst.CorrelationId, s_correlationId);
response.Add(BrokerResponseConst.Scope, authResult[BrokerResponseConst.AndroidScopes].ToString());
response.Add(BrokerResponseConst.ExpiresOn, authResult[BrokerResponseConst.ExpiresOn].ToString());
response.Add(BrokerResponseConst.ClientInfo, authResult[BrokerResponseConst.ClientInfo].ToString());
Expand Down

0 comments on commit 0ebd4a8

Please sign in to comment.