-
Notifications
You must be signed in to change notification settings - Fork 4
/
FccAdminApiProvider.cs
308 lines (273 loc) · 14.4 KB
/
FccAdminApiProvider.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using fiskaltrust.Middleware.SCU.DE.DeutscheFiskal.Communication.Helpers;
using fiskaltrust.Middleware.SCU.DE.DeutscheFiskal.Exceptions;
using fiskaltrust.Middleware.SCU.DE.DeutscheFiskal.Models;
using fiskaltrust.Middleware.SCU.DE.Helpers.TLVLogParser.Tar;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace fiskaltrust.Middleware.SCU.DE.DeutscheFiskal.Communication
{
public class FccAdminApiProvider
{
private readonly DeutscheFiskalSCUConfiguration _configuration;
private readonly ILogger<FccAdminApiProvider> _logger;
private readonly Uri _baseAddress;
private readonly ConcurrentDictionary<Guid, List<(DateTime, DateTime)>> _splitExports = new ConcurrentDictionary<Guid, List<(DateTime, DateTime)>>();
public FccAdminApiProvider(DeutscheFiskalSCUConfiguration configuration, ILogger<FccAdminApiProvider> logger)
{
_configuration = configuration;
_logger = logger;
_baseAddress = FccUriHelper.GetFccUri(configuration);
}
public async Task<List<ClientResponseDto>> GetClientsAsync()
{
using var client = GetBasicAuthAdminClient();
var response = await client.GetAsync("clients").ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<List<ClientResponseDto>>(responseContent);
}
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while getting registered clients (GET /clients). Response: {responseContent}",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "GET /clients");
}
public async Task CreateClientAsync(string clientId)
{
var request = new CreateClientRequestDto
{
ClientId = clientId,
ErsIdentifier = clientId,
RegistrationToken = _configuration.ActivationToken,
BriefDescription = clientId,
TypeOfSystem = _configuration.TypeOfSystem
};
using var client = GetBasicAuthAdminClient();
var requestContent = JsonConvert.SerializeObject(request);
var response = await client.PostAsync("registration", new StringContent(requestContent, Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while registering client (POST /registration). Request: '{requestContent}', Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "POST /registration");
}
}
public async Task DeregisterClientAsync(string clientId)
{
var request = new DeregisterClientRequestDto
{
ClientId = clientId
};
using var client = GetBasicAuthAdminClient();
var requestContent = JsonConvert.SerializeObject(request);
var response = await client.PostAsync("deregistration", new StringContent(requestContent, Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while deregistering client (POST /deregistration). Request: '{requestContent}', Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "POST /deregistration");
}
}
public async Task<FccInfoResponseDto> GetFccInfoAsync()
{
using var client = GetBasicAuthAdminClient();
var response = await client.GetAsync("info");
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<FccInfoResponseDto>(responseContent);
}
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while requesting FCC info (GET /info). Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "GET /info");
}
public async Task<SelfCheckResponseDto> GetSelfCheckResultAsync()
{
using var client = GetOAuthAdminClient();
var response = await client.GetAsync("selfcheck");
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<SelfCheckResponseDto>(responseContent);
}
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while getting self check result (GET /selfcheck). Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "GET /selfcheck");
}
public async Task<TssDetailsResponseDto> GetTssDetailsAsync()
{
using var client = GetOAuthAdminClient();
var response = await client.GetAsync("tssdetails");
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<TssDetailsResponseDto>(responseContent);
}
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while getting TSS details (GET /tssdetails). Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), "GET /tssdetails");
}
public async Task<byte[]> ExportSingleTransactionAsync(ulong transactionNumber)
{
using var client = GetOAuthAdminClient();
var response = await client.GetAsync($"export/transactions/{transactionNumber}");
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return Convert.FromBase64String(responseContent);
}
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while exporting single transaction (GET /export/transactions/{transactionNumber}). Response: '{responseContent}'",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), $"GET /export/transactions/{transactionNumber}");
}
public async Task RequestExportAsync(Guid exportId, string targetFile, DateTime startDate, DateTime endDate, string clientId = null, bool isSplit = false)
{
var url = $"export/transactions/time?startDate={startDate:yyyy-MM-dd'T'HH:mm:ss'Z'}&endDate={endDate:yyyy-MM-dd'T'HH:mm:ss'Z'}";
if (clientId != null)
{
url += $"&clientId={clientId}";
}
var response = await GetOAuthAdminClient().GetAsync(url);
if (response.StatusCode == HttpStatusCode.NotFound)
{
if (!isSplit)
{
File.WriteAllBytes(targetFile, Array.Empty<byte>());
}
return;
}
if (response.IsSuccessStatusCode)
{
// Deserialize base64 response without loading it into memory. Bold move of Microsoft to call this "CryptoStream", but who am I to judge.
using var contentStream = new CryptoStream(await response.Content.ReadAsStreamAsync(), new FromBase64Transform(), CryptoStreamMode.Read);
if (isSplit)
{
TarFileHelper.AppendTarStreamToTarFile(targetFile, contentStream);
AddSplitAcknowledgment(exportId, startDate, endDate);
}
else
{
using var fileStream = File.Create(targetFile);
contentStream.CopyTo(fileStream);
}
}
else
{
if (response.StatusCode.Equals(HttpStatusCode.RequestEntityTooLarge))
{
_logger.LogDebug("Requested export was too large to be processed by the FCC, splitting into multiple export requests.");
await RequestSplitExportAsync(exportId, targetFile, startDate, endDate, clientId).ConfigureAwait(false);
}
else
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new FiskalCloudException($"Communication error ({response.StatusCode}) while requesting export (GET {url}). Response: '{responseContent}",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), $"GET /{url}");
}
}
// Finalize merged TAR file after last batch was processed
if (IsSplitExport(exportId) && !isSplit)
{
TarFileHelper.FinalizeTarFile(targetFile);
_logger.LogDebug("Finalized merged TAR file {fileName}.", targetFile);
}
}
public bool IsSplitExport(Guid exportId)
{
return _splitExports.ContainsKey(exportId);
}
private void AddSplitAcknowledgment(Guid exportId, DateTime startDate, DateTime endDate)
{
_splitExports.AddOrUpdate(exportId, new List<(DateTime, DateTime)>() { (startDate, endDate) }, (k, v) => { v.Add((startDate, endDate)); return v; });
}
private async Task RequestSplitExportAsync(Guid exportId, string targetFile, DateTime startDate, DateTime endDate, string clientId)
{
var difference = endDate - startDate;
if (difference.TotalSeconds > 0)
{
var half = difference.TotalSeconds / 2;
var newStartDate = startDate.AddSeconds(half);
var newEndDate = startDate.AddSeconds(half - 1);
_logger.LogDebug("Split large export into two parts: 1) {firstStartDate}-{firstEndDate}, 2) {firstStartDate}-{secondEndDate}", newStartDate, endDate, startDate, newEndDate);
await RequestExportAsync(exportId, targetFile, newStartDate, endDate, clientId, true).ConfigureAwait(false);
await RequestExportAsync(exportId, targetFile, startDate, newEndDate, clientId, true).ConfigureAwait(false);
}
}
public async Task AcknowledgeSplitTransactionsAsync(Guid exportId, string clientId = null)
{
if (_splitExports.TryGetValue(exportId, out var _splitExportDatetimes))
{
foreach ((var startDateTime, var endDateTime) in _splitExportDatetimes)
{
await AcknowledgeAllTransactionsAsync(startDateTime, endDateTime, clientId).ConfigureAwait(false);
}
_splitExports.TryRemove(exportId, out _);
}
}
public void RemoveSplitExportIfExists(string exportId)
{
try
{
var gExportID = Guid.Parse(exportId);
if (_splitExports.ContainsKey(gExportID))
{
_splitExports.TryRemove(gExportID, out _);
}
}catch{}
}
public async Task AcknowledgeAllTransactionsAsync(DateTime startDate, DateTime endDate, string clientId = null)
{
var url = $"export/transactions/time?startDate={startDate:yyyy-MM-dd'T'HH:mm:ss'Z'}&endDate={endDate:yyyy-MM-dd'T'HH:mm:ss'Z'}";
if (clientId != null)
{
url += $"&clientId={clientId}";
}
var response = await GetOAuthAdminClient().PostAsync(url, new StringContent("ACK", Encoding.UTF8, "text/plain"));
var responseContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NotFound)
{
throw new FiskalCloudException($"Communication error ({response.StatusCode}): {responseContent}",
(int) response.StatusCode, ErrorHelper.GetErrorType(responseContent), $"POST /{url}");
}
}
public HttpClient GetBasicAuthAdminClient()
{
var client = new HttpClient { BaseAddress = _baseAddress, Timeout = TimeSpan.FromSeconds(_configuration.FCCTimeoutSec) };
var credentials = Encoding.ASCII.GetBytes($"admin:{_configuration.ErsCode}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
return client;
}
public HttpClient GetBasicAuthActuatorClient()
{
var client = new HttpClient { BaseAddress = _baseAddress, Timeout = TimeSpan.FromSeconds(_configuration.FCCTimeoutSec) };
var credentials = Encoding.ASCII.GetBytes($"actuator:{_configuration.ErsCode}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
return client;
}
public HttpClient GetOAuthAdminClient()
{
var clientConfig = new ClientConfiguration
{
BaseAddress = _baseAddress,
UserName = "admin-auth-client-id",
Password = "admin-auth-client-secret",
GrantType = "password",
AdditionalProperties = new Dictionary<string, string>
{
{ "username", "admin" },
{ "password", _configuration.ErsCode },
},
Timeout = _configuration.FCCTimeoutSec
};
return new HttpClient(new AuthenticatedHttpClientHandler(clientConfig))
{
BaseAddress = _baseAddress
};
}
}
}