Skip to content

Commit cee33a1

Browse files
committed
/$sign2 with JWS
1 parent df97464 commit cee33a1

1 file changed

Lines changed: 132 additions & 22 deletions

File tree

src/IO.Swagger.Lib.V3/Controllers/SubmodelRepositoryAPIApi.cs

Lines changed: 132 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,32 @@ namespace IO.Swagger.Controllers;
2222
* Generated by: https://github.com/swagger-api/swagger-codegen.git
2323
*/
2424

25+
using System;
26+
using System.Collections.Generic;
27+
using System.ComponentModel.DataAnnotations;
28+
using System.IO;
29+
using System.Linq;
30+
using System.Net.Mime;
31+
using System.Runtime.ConstrainedExecution;
32+
using System.Security.Claims;
33+
using System.Security.Cryptography;
34+
using System.Security.Cryptography.X509Certificates;
35+
using System.Security.Cryptography.Xml;
36+
using System.Text;
37+
using System.Text.Json;
38+
using System.Text.Json.Nodes;
39+
using System.Text.Json.Serialization;
40+
using System.Threading.Tasks;
2541
using AasxServer;
2642
using AasxServerStandardBib.Interfaces;
2743
using AasxServerStandardBib.Logging;
44+
using AdminShellNS.Exceptions;
2845
using AdminShellNS.Lib.V3.Models;
46+
using Contracts;
47+
using Contracts.DbRequests;
48+
using Contracts.Exceptions;
49+
using Contracts.Pagination;
50+
using Contracts.Security;
2951
using DataTransferObjects.MetadataDTOs;
3052
using DataTransferObjects.ValueDTOs;
3153
using IO.Swagger.Attributes;
@@ -34,35 +56,16 @@ namespace IO.Swagger.Controllers;
3456
using IO.Swagger.Lib.V3.SerializationModifiers.Mappers;
3557
using IO.Swagger.Lib.V3.Services;
3658
using IO.Swagger.Models;
59+
using Jose;
3760
using Microsoft.AspNetCore.Authorization;
3861
using Microsoft.AspNetCore.Http;
3962
using Microsoft.AspNetCore.Mvc;
40-
using Swashbuckle.AspNetCore.Annotations;
41-
using System;
42-
using System.Collections.Generic;
43-
using System.ComponentModel.DataAnnotations;
44-
using System.IO;
45-
using System.Linq;
46-
using System.Net.Mime;
47-
using System.Security.Claims;
48-
using System.Threading.Tasks;
49-
using AdminShellNS.Exceptions;
5063
using Microsoft.IdentityModel.Tokens;
5164
using Namotion.Reflection;
5265
using Newtonsoft.Json;
66+
using Swashbuckle.AspNetCore.Annotations;
5367
using TimeStamp;
54-
using Contracts;
55-
using Contracts.DbRequests;
56-
using Contracts.Exceptions;
57-
using Contracts.Pagination;
58-
using Contracts.Security;
59-
using System.Text.Json.Serialization;
60-
using System.Text.Json;
61-
using System.Security.Cryptography.X509Certificates;
62-
using System.Security.Cryptography.Xml;
63-
using System.Security.Cryptography;
64-
using System.Text;
65-
using System.Text.Json.Nodes;
68+
using static QRCoder.PayloadGenerator;
6669

6770
/// <summary>
6871
///
@@ -1455,6 +1458,113 @@ public async virtual Task<IActionResult> GetSubmodelByIdSigned([FromRoute][Requi
14551458
throw new NotAllowed($"");
14561459
}
14571460

1461+
/// <summary>
1462+
/// Returns a specific Submodel signed
1463+
/// </summary>
1464+
/// <param name="submodelIdentifier">The Submodel’s unique id (UTF8-BASE64-URL-encoded)</param>
1465+
/// <param name="level">Determines the structural depth of the respective resource content</param>
1466+
/// <param name="extent">Determines to which extent the resource is being serialized</param>
1467+
/// <response code="200">Requested Submodel</response>
1468+
/// <response code="400">Bad Request, e.g. the request parameters of the format of the request body is wrong.</response>
1469+
/// <response code="401">Unauthorized, e.g. the server refused the authorization attempt.</response>
1470+
/// <response code="403">Forbidden</response>
1471+
/// <response code="404">Not Found</response>
1472+
/// <response code="500">Internal Server Error</response>
1473+
/// <response code="0">Default error handling for unmentioned status codes</response>
1474+
[HttpGet]
1475+
[Route("submodels/{submodelIdentifier}/$sign2")]
1476+
[ValidateModelState]
1477+
[SwaggerOperation("GetSubmodelByIdSigned2")]
1478+
[SwaggerResponse(statusCode: 200, type: typeof(Submodel), description: "Requested Submodel")]
1479+
[SwaggerResponse(statusCode: 400, type: typeof(Result), description: "Bad Request, e.g. the request parameters of the format of the request body is wrong.")]
1480+
[SwaggerResponse(statusCode: 401, type: typeof(Result), description: "Unauthorized, e.g. the server refused the authorization attempt.")]
1481+
[SwaggerResponse(statusCode: 403, type: typeof(Result), description: "Forbidden")]
1482+
[SwaggerResponse(statusCode: 404, type: typeof(Result), description: "Not Found")]
1483+
[SwaggerResponse(statusCode: 500, type: typeof(Result), description: "Internal Server Error")]
1484+
[SwaggerResponse(statusCode: 0, type: typeof(Result), description: "Default error handling for unmentioned status codes")]
1485+
public async virtual Task<IActionResult> GetSubmodelByIdSigned2([FromRoute][Required] string submodelIdentifier, [FromQuery] string? level, [FromQuery] string? extent)
1486+
{
1487+
//Validate level and extent
1488+
var levelEnum = _validateModifierService.ValidateLevel(level);
1489+
var extentEnum = _validateModifierService.ValidateExtent(extent);
1490+
1491+
var decodedSubmodelIdentifier = _decoderService.Decode("submodelIdentifier", submodelIdentifier);
1492+
1493+
_logger.LogInformation($"Received request to get the submodel with id {decodedSubmodelIdentifier}");
1494+
if (decodedSubmodelIdentifier == null)
1495+
{
1496+
throw new NotAllowed($"Cannot proceed as {nameof(decodedSubmodelIdentifier)} is null");
1497+
}
1498+
1499+
var securityConfig = new SecurityConfig(Program.noSecurity, this);
1500+
1501+
var submodel = await _dbRequestHandlerService.ReadSubmodelById(securityConfig, null, decodedSubmodelIdentifier);
1502+
1503+
string certFile = "Andreas_Orzelski_Chain.pfx";
1504+
string certPW = "i40";
1505+
if (System.IO.File.Exists(certFile))
1506+
{
1507+
var submodelText = Jsonization.Serialize.ToJsonObject(submodel).ToJsonString();
1508+
1509+
var mStrm = new MemoryStream(Encoding.UTF8.GetBytes(submodelText));
1510+
var node = System.Text.Json.JsonSerializer.DeserializeAsync<JsonNode>(mStrm).Result;
1511+
var s = Jsonization.Deserialize.SubmodelFrom(node);
1512+
1513+
submodel.Extensions ??= [];
1514+
using (var certificate = new X509Certificate2(certFile, certPW))
1515+
{
1516+
if (certificate == null)
1517+
{
1518+
throw new NotAllowed($"");
1519+
}
1520+
1521+
X509Certificate2Collection xc = new X509Certificate2Collection();
1522+
xc.Import(certFile, certPW, X509KeyStorageFlags.PersistKeySet);
1523+
1524+
var i = 0;
1525+
var x5c = new string[xc.Count];
1526+
for (var j = xc.Count - 1; j >= 0; j--)
1527+
{
1528+
var c = Convert.ToBase64String(xc[j].GetRawCertData());
1529+
x5c[i++] = c;
1530+
}
1531+
1532+
var headers = new Dictionary<string, object>
1533+
{
1534+
{ "alg", "RS256" },
1535+
{ "typ", "JWS" },
1536+
{ "x5c", x5c }
1537+
};
1538+
1539+
var signature = "";
1540+
try
1541+
{
1542+
using (RSA rsa = certificate.GetRSAPrivateKey())
1543+
{
1544+
if (rsa == null)
1545+
{
1546+
throw new NotAllowed($"");
1547+
}
1548+
1549+
var jws = JWT.Encode(submodelText, rsa, JwsAlgorithm.RS256, headers);
1550+
1551+
submodel.Extensions.Add(new Extension($"$sign_{Guid.NewGuid()}", value: jws));
1552+
1553+
return new ObjectResult(submodel);
1554+
}
1555+
1556+
}
1557+
// ReSharper disable EmptyGeneralCatchClause
1558+
catch
1559+
{
1560+
}
1561+
1562+
}
1563+
}
1564+
1565+
throw new NotAllowed($"");
1566+
}
1567+
14581568
/// <summary>
14591569
/// Returns the metadata attributes of a specific Submodel
14601570
/// </summary>

0 commit comments

Comments
 (0)