Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated mock service endpoint in MockProvider #141

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions CommunityToolkit.Authentication/MockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Authentication.Extensions;

Expand All @@ -15,7 +16,12 @@ namespace CommunityToolkit.Authentication
[Obsolete("MockProvider is meant for prototyping and demonstration purposes only. Not for use in production applications.")]
public class MockProvider : BaseProvider
{
private const string GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url=";
private const string GRAPH_PROXY_URL_REQUEST_ENDPOINT = "https://cdn.graph.office.net/en-us/graph/api/proxy/endpoint";
private const string FALLBACK_GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url=";

private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);

private string _baseUrl = null;

/// <summary>
/// Initializes a new instance of the <see cref="MockProvider"/> class.
Expand All @@ -30,19 +36,19 @@ public MockProvider(bool signedIn = true)
public override string CurrentAccountId => State == ProviderState.SignedIn ? "mock-account-id" : null;

/// <inheritdoc/>
public override Task AuthenticateRequestAsync(HttpRequestMessage request)
public override async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
// Append the SDK version header
AddSdkVersion(request);

// Append the token auth header
request.AddMockProviderToken();

var baseUrl = await GetBaseUrlAsync();

// Prepend Proxy Service URI
var requestUri = request.RequestUri.ToString();
request.RequestUri = new Uri(GRAPH_PROXY_URL + Uri.EscapeDataString(requestUri));

return Task.FromResult(0);
request.RequestUri = new Uri(baseUrl + Uri.EscapeDataString(requestUri));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -88,5 +94,36 @@ public override async Task<bool> TrySilentSignInAsync()
await SignInAsync();
return true;
}

private async Task<string> GetBaseUrlAsync()
{
await _semaphore.WaitAsync();

if (_baseUrl == null)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(GRAPH_PROXY_URL_REQUEST_ENDPOINT);

if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();

if (responseContent.StartsWith("\"") && responseContent.EndsWith("\""))
{
responseContent = responseContent.Substring(1, responseContent.Length - 2);
}

_baseUrl = $"{responseContent}?url=";
}
else
{
_baseUrl = FALLBACK_GRAPH_PROXY_URL;
}
}

_semaphore.Release();

return _baseUrl;
}
}
}