Skip to content

Commit 28e919f

Browse files
Updated mock service endpoint in MockProvider (#141)
* Updated mock service endpoint in MockProvider
1 parent 90dc3f6 commit 28e919f

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

CommunityToolkit.Authentication/MockProvider.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Net.Http;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using CommunityToolkit.Authentication.Extensions;
910

@@ -15,7 +16,12 @@ namespace CommunityToolkit.Authentication
1516
[Obsolete("MockProvider is meant for prototyping and demonstration purposes only. Not for use in production applications.")]
1617
public class MockProvider : BaseProvider
1718
{
18-
private const string GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url=";
19+
private const string GRAPH_PROXY_URL_REQUEST_ENDPOINT = "https://cdn.graph.office.net/en-us/graph/api/proxy/endpoint";
20+
private const string FALLBACK_GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url=";
21+
22+
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
23+
24+
private string _baseUrl = null;
1925

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

3238
/// <inheritdoc/>
33-
public override Task AuthenticateRequestAsync(HttpRequestMessage request)
39+
public override async Task AuthenticateRequestAsync(HttpRequestMessage request)
3440
{
3541
// Append the SDK version header
3642
AddSdkVersion(request);
3743

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

47+
var baseUrl = await GetBaseUrlAsync();
48+
4149
// Prepend Proxy Service URI
4250
var requestUri = request.RequestUri.ToString();
43-
request.RequestUri = new Uri(GRAPH_PROXY_URL + Uri.EscapeDataString(requestUri));
44-
45-
return Task.FromResult(0);
51+
request.RequestUri = new Uri(baseUrl + Uri.EscapeDataString(requestUri));
4652
}
4753

4854
/// <inheritdoc/>
@@ -88,5 +94,36 @@ public override async Task<bool> TrySilentSignInAsync()
8894
await SignInAsync();
8995
return true;
9096
}
97+
98+
private async Task<string> GetBaseUrlAsync()
99+
{
100+
await _semaphore.WaitAsync();
101+
102+
if (_baseUrl == null)
103+
{
104+
var httpClient = new HttpClient();
105+
var response = await httpClient.GetAsync(GRAPH_PROXY_URL_REQUEST_ENDPOINT);
106+
107+
if (response.IsSuccessStatusCode)
108+
{
109+
var responseContent = await response.Content.ReadAsStringAsync();
110+
111+
if (responseContent.StartsWith("\"") && responseContent.EndsWith("\""))
112+
{
113+
responseContent = responseContent.Substring(1, responseContent.Length - 2);
114+
}
115+
116+
_baseUrl = $"{responseContent}?url=";
117+
}
118+
else
119+
{
120+
_baseUrl = FALLBACK_GRAPH_PROXY_URL;
121+
}
122+
}
123+
124+
_semaphore.Release();
125+
126+
return _baseUrl;
127+
}
91128
}
92129
}

0 commit comments

Comments
 (0)