-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceService.cs
More file actions
99 lines (80 loc) · 3.64 KB
/
ResourceService.cs
File metadata and controls
99 lines (80 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
namespace Cook;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
public class ResourceService
{
private readonly IConfiguration configuration;
private readonly HttpClient httpClient;
private OAuth2TokenResponse? cachedToken;
public ResourceService(IConfiguration configuration, HttpClient httpClient)
{
this.configuration = configuration;
this.httpClient = httpClient;
}
public async Task<string> GetContentAsync(string name)
{
try
{
var configServerUri = this.configuration["spring:cloud:config:uri"];
var appName = this.configuration["spring:application:name"] ?? "application";
var profile = this.configuration["spring:cloud:config:env"] ?? "default";
var label = this.configuration["spring:cloud:config:label"] ?? "main";
var accessToken = await this.GetOrRefreshAccessTokenAsync();
var request = new HttpRequestMessage(HttpMethod.Get, $"{configServerUri}/{appName}/{profile}/{label}/{name}");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await this.httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new HttpRequestException($"Failed to fetch resource: {response.StatusCode} - {response.ReasonPhrase}");
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Error fetching resource: {ex.Message}", ex);
}
}
private async Task<string> GetOrRefreshAccessTokenAsync()
{
if (this.cachedToken != null && DateTime.UtcNow < this.cachedToken.ExpirationTime)
{
return this.cachedToken.AccessToken;
}
var clientId = this.configuration["spring:cloud:config:clientId"];
var clientSecret = this.configuration["spring:cloud:config:clientSecret"];
var accessTokenUri = this.configuration["spring:cloud:config:accessTokenUri"];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret) || string.IsNullOrEmpty(accessTokenUri))
{
throw new InvalidOperationException("OAuth2 credentials not configured");
}
var tokenRequest = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
});
var tokenResponse = await this.httpClient.PostAsync(accessTokenUri, tokenRequest);
if (!tokenResponse.IsSuccessStatusCode)
{
throw new HttpRequestException($"Failed to get access token: {tokenResponse.StatusCode} - {tokenResponse.ReasonPhrase}");
}
this.cachedToken = this.ExtractTokenData(await tokenResponse.Content.ReadAsStringAsync());
return this.cachedToken.AccessToken;
}
private OAuth2TokenResponse ExtractTokenData(string tokenResponse)
{
return JsonSerializer.Deserialize<OAuth2TokenResponse>(tokenResponse) ?? new OAuth2TokenResponse();
}
}
public class OAuth2TokenResponse
{
[JsonPropertyName("access_token")]
public string AccessToken { get; set; } = string.Empty;
[JsonPropertyName("expires_in")]
public int ExpiresIn { get; set; } = 3600;
public DateTime ExpirationTime => DateTime.UtcNow.AddSeconds(this.ExpiresIn - 10); // Subtract 10 seconds for safety
}