-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCallbackService.cs
More file actions
76 lines (69 loc) · 3.83 KB
/
CallbackService.cs
File metadata and controls
76 lines (69 loc) · 3.83 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
/********************************************************************************
* Copyright (c) 2024 BMW Group AG
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
using Dim.Clients.Extensions;
using DimProcess.Library.Callback.DependencyInjection;
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Framework.HttpClientExtensions;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Token;
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
namespace DimProcess.Library.Callback;
public class CallbackService(ITokenService tokenService, IOptions<CallbackSettings> options)
: ICallbackService
{
private readonly CallbackSettings _settings = options.Value;
public async Task SendCallback(string bpn, AuthenticationDetail authenticationDetail, JsonDocument didDocument, string did, CancellationToken cancellationToken)
{
var httpClient = await tokenService.GetAuthorizedClient<CallbackService>(_settings, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
var data = new CallbackDataModel(
did,
didDocument,
authenticationDetail
);
ValueTask<(bool, string?)> CustomErrorHandling(HttpResponseMessage errorResponse) => new(
(errorResponse.StatusCode == HttpStatusCode.Conflict,
null));
await httpClient.PostAsJsonAsync($"/api/administration/registration/dim/{bpn}", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("send-callback", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE, CustomErrorHandling)
.ConfigureAwait(false);
}
public async Task SendTechnicalUserCallback(Guid externalId, string tokenAddress, string clientId, string clientSecret, CancellationToken cancellationToken)
{
var httpClient = await tokenService.GetAuthorizedClient<CallbackService>(_settings, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
var data = new AuthenticationDetail(
tokenAddress,
clientId,
clientSecret);
await httpClient.PostAsJsonAsync($"/api/administration/serviceAccount/callback/{externalId}", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("send-technical-user-callback", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE)
.ConfigureAwait(false);
}
public async Task SendTechnicalUserDeletionCallback(Guid externalId, CancellationToken cancellationToken)
{
var httpClient = await tokenService.GetAuthorizedClient<CallbackService>(_settings, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
await httpClient.PostAsync($"/api/administration/serviceAccount/callback/{externalId}/delete", null, cancellationToken)
.CatchingIntoServiceExceptionFor("send-technical-user-deletion-callback", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE)
.ConfigureAwait(false);
}
}