/
MaskinportenSchemaController.cs
237 lines (221 loc) · 11.6 KB
/
MaskinportenSchemaController.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
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.AccessManagement.UI.Core.Helpers;
using Altinn.AccessManagement.UI.Core.Models;
using Altinn.AccessManagement.UI.Core.Models.Delegation.Frontend;
using Altinn.AccessManagement.UI.Core.Models.SingleRight;
using Altinn.AccessManagement.UI.Core.Services.Interfaces;
using Altinn.AccessManagement.UI.Filters;
using Altinn.Platform.Profile.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Altinn.AccessManagement.UI.Controllers
{
/// <summary>
/// Controller responsible for all operations for managing maskinportenscehma delegations
/// </summary>
[ApiController]
[AutoValidateAntiforgeryTokenIfAuthCookie]
[Route("accessmanagement/api/v1")]
public class MaskinportenSchemaController : ControllerBase
{
private readonly IMaskinportenSchemaService _maskinportenService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<MaskinportenSchemaController> _logger;
private readonly IProfileService _profileService;
private readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
/// <summary>
/// Initializes a new instance of the <see cref="MaskinportenSchemaController" /> class.
/// </summary>
/// <param name="logger">the handler for logger service</param>
/// <param name="maskinportenServicesService">The service implementation for handling maskinporten schema delegations</param>
/// <param name="profileService">The service implementation for user profile operations</param>
/// <param name="httpContextAccessor">Accessor for httpcontext</param>
public MaskinportenSchemaController(
ILogger<MaskinportenSchemaController> logger,
IMaskinportenSchemaService maskinportenServicesService,
IProfileService profileService,
IHttpContextAccessor httpContextAccessor)
{
_logger = logger;
_maskinportenService = maskinportenServicesService;
_profileService = profileService;
_serializerOptions.Converters.Add(new JsonStringEnumConverter());
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// Endpoint for retrieving received Maskinporten resource delegation to the reportee party from others
/// </summary>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[Authorize]
[Route("{party}/maskinportenschema/received")]
public async Task<ActionResult<List<MaskinportenSchemaDelegationFE>>> GetReceivedMaskinportenSchemaDelegations([FromRoute] string party)
{
try
{
int userId = AuthenticationHelper.GetUserId(_httpContextAccessor.HttpContext);
UserProfile userProfile = await _profileService.GetUserProfile(userId);
string languageCode = ProfileHelper.GetLanguageCodeForUserAltinnStandard(userProfile, HttpContext);
return await _maskinportenService.GetReceivedMaskinportenSchemaDelegations(party, languageCode);
}
catch (Exception ex)
{
if (ex is ValidationException || ex is ArgumentException)
{
ModelState.AddModelError("Validation Error", ex.Message);
return new ObjectResult(ProblemDetailsFactory.CreateValidationProblemDetails(HttpContext, ModelState));
}
_logger.LogError(ex, "Unexpected exception occurred during retrieval of received maskinportenschema");
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
/// <summary>
/// Endpoint for retrieving delegated MaskinportenSchema resources offered by the reportee party to others
/// </summary>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[Authorize]
[Route("{party}/maskinportenschema/offered")]
public async Task<ActionResult<List<MaskinportenSchemaDelegationFE>>> GetOfferedMaskinportenSchemaDelegations([FromRoute] string party)
{
try
{
int userId = AuthenticationHelper.GetUserId(_httpContextAccessor.HttpContext);
UserProfile userProfile = await _profileService.GetUserProfile(userId);
string languageCode = ProfileHelper.GetLanguageCodeForUserAltinnStandard(userProfile, HttpContext);
return await _maskinportenService.GetOfferedMaskinportenSchemaDelegations(party, languageCode);
}
catch (Exception ex)
{
if (ex is ValidationException || ex is ArgumentException)
{
ModelState.AddModelError("Validation Error", ex.Message);
return new ObjectResult(ProblemDetailsFactory.CreateValidationProblemDetails(HttpContext, ModelState));
}
_logger.LogError(ex, "Unexpected exception occurred during retrieval of offered maskinportenschema");
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
/// <summary>
/// Endpoint for revoking a maskinporten scope delegation on behalf of the party having received the delegation
/// </summary>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpPost]
[Authorize]
[Route("{party}/maskinportenschema/received/revoke")]
public async Task<ActionResult> RevokeReceivedMaskinportenScopeDelegation([FromRoute] string party, [FromBody] RevokeReceivedDelegation delegation)
{
try
{
HttpResponseMessage response = await _maskinportenService.RevokeReceivedMaskinportenScopeDelegation(party, delegation);
if (response.StatusCode == HttpStatusCode.NoContent)
{
return NoContent();
}
string responseContent = await response.Content.ReadAsStringAsync();
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext, (int?)response.StatusCode, "Unexpected HttpStatus response", detail: responseContent));
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected exception occurred during revoke of received maskinportenschema");
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
/// <summary>
/// Endpoint for revoking a maskinporten scope delegation on behalf of the party having offered the delegation
/// </summary>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpPost]
[Authorize]
[Route("{party}/maskinportenschema/offered/revoke")]
public async Task<ActionResult> RevokeOfferedMaskinportenScopeDelegation([FromRoute] string party, [FromBody] RevokeOfferedDelegation delegation)
{
try
{
HttpResponseMessage response = await _maskinportenService.RevokeOfferedMaskinportenScopeDelegation(party, delegation);
if (response.StatusCode == HttpStatusCode.NoContent)
{
return NoContent();
}
if (response.StatusCode == HttpStatusCode.BadRequest)
{
string responseContent = await response.Content.ReadAsStringAsync();
ValidationProblemDetails problemDetails = JsonSerializer.Deserialize<ValidationProblemDetails>(responseContent, _serializerOptions);
return new ObjectResult(problemDetails);
}
else
{
string responseContent = await response.Content.ReadAsStringAsync();
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext, (int?)response.StatusCode, "Unexpected HttpStatus response", detail: responseContent));
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected exception occurred during revoke of offered maskinportenschema");
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
/// <summary>
/// Endpoint for delegating a maskinporten schema resource from the reportee party to a third party organization
/// </summary>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpPost]
[Authorize]
[Route("{party}/maskinportenschema/offered")]
public async Task<ActionResult<DelegationOutput>> CreateMaskinportenDelegation([FromRoute] string party, [FromBody] DelegationInput delegation)
{
try
{
HttpResponseMessage response = await _maskinportenService.CreateMaskinportenScopeDelegation(party, delegation);
if (response.StatusCode == HttpStatusCode.Created)
{
string responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<DelegationOutput>(responseContent, _serializerOptions);
}
if (response.StatusCode == HttpStatusCode.BadRequest)
{
string responseContent = await response.Content.ReadAsStringAsync();
ValidationProblemDetails problemDetails = JsonSerializer.Deserialize<ValidationProblemDetails>(responseContent, _serializerOptions);
return new ObjectResult(problemDetails);
}
else
{
string responseContent = await response.Content.ReadAsStringAsync();
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext, (int?)response.StatusCode, "Unexpected HttpStatus response", detail: responseContent));
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected exception occurred during delegation of maskinportenschema");
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
/// <summary>
/// Endpoint for performing a check if the user can delegate a maskinporten schema service to a specified reportee.
/// </summary>
/// <param name="partyId">The reportee's party id</param>
/// <param name="request">Necessary info about the right that's going to be checked</param>
[HttpPost]
[Authorize]
[Route("{partyId}/maskinportenschema/delegationcheck")]
public async Task<ActionResult<List<DelegationResponseData>>> DelegationCheck([FromRoute] string partyId, [FromBody] Right request)
{
try
{
return await _maskinportenService.DelegationCheck(partyId, request);
}
catch (Exception)
{
return new ObjectResult(ProblemDetailsFactory.CreateProblemDetails(HttpContext));
}
}
}
}