Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SignData.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
12using Waher.Script;
14using Waher.Security;
19
21{
26 {
30 public SignData()
31 : base("Legal/SignData",
32 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
33 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
34 {
35 }
36
37 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SignData).Namespace + ".JSON.SignData.req");
38 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SignData).Namespace + ".XML.SignData.req");
39
44 public override bool Synchronous => false;
45
54 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
55 {
57
58 string KeyId = (string)Parameters["PKeyId"].AssociatedObjectValue;
59 CaseInsensitiveString LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
60 string DataBase64 = (string)Parameters["PDataBase64"].AssociatedObjectValue;
61 string KeySignature = (string)Parameters["PKeySignature"].AssociatedObjectValue;
62 string RequestSignature = (string)Parameters["PRequestSignature"].AssociatedObjectValue;
63 byte[] Data;
64
65 if (string.IsNullOrEmpty(KeyId))
66 throw new BadRequestException("Key ID cannot be empty.");
67
68 try
69 {
70 Data = Convert.FromBase64String(DataBase64);
71 }
72 catch (Exception)
73 {
74 throw new BadRequestException("Invalid base64-encoded data.");
75 }
76
77 LegalIdentity Identity = await LegalComponent.GetLocalLegalIdentity(LegalId)
78 ?? throw new NotFoundException("Legal identity not found.");
79
80 if (Identity.Account != User.UserName)
81 throw new ForbiddenException(Request, "Only allowed to add attachments to your own legal identities.");
82
83 if (Identity.State != IoTBroker.Legal.Identity.IdentityState.Approved)
84 throw new ForbiddenException(Request, "Legal Identity not approved.");
85
86 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
87 new FilterFieldEqualTo("Account", User.UserName),
88 new FilterFieldEqualTo("Id", KeyId)))
89 ?? throw new NotFoundException("Key not found.");
90
91 StringBuilder sb = new StringBuilder();
92
93 sb.Append(User.UserName);
94 sb.Append(':');
95 sb.Append(Request.Header.Host.Value);
96 sb.Append(':');
97 sb.Append(AgentKey.LocalName);
98 sb.Append(':');
99 sb.Append(AgentKey.Namespace);
100 sb.Append(':');
101 sb.Append(KeyId);
102
103 //string s1 = sb.ToString();
104
105 sb.Append(':');
106 sb.Append(KeySignature);
107
108 string s2 = sb.ToString();
109
110 sb.Append(':');
111 sb.Append(DataBase64);
112 sb.Append(':');
113 sb.Append(LegalId);
114
115 string s3 = sb.ToString();
116
117 string s = Convert.ToBase64String(
119 Encoding.UTF8.GetBytes(User.Account.Password),
120 Encoding.UTF8.GetBytes(s3)));
121
122 if (s != RequestSignature)
123 {
124 string Msg = "Request Signature invalid.";
125 throw new ForbiddenException(Request, Msg);
126 }
127
128 EllipticCurveEndpoint KeyEndpoint = ApplyId.GetEndpoint(Request, AgentKey, s2);
129 byte[] Signature = KeyEndpoint.Sign(Data);
130
131 await Response.Return(new NamedDictionary<string, object>("SignatureResponse", AgentNamespace)
132 {
133 { "Signature", Convert.ToBase64String(Signature) }
134 });
135 }
136
137 }
138}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
HttpFieldHost Host
Host HTTP Field header. (RFC 2616, §14.23)
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Abstract base class for Elliptic Curve endpoints.
override byte[] Sign(byte[] Data)
Signs binary data using the local private key.
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeHMACSHA256Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:735
string Password
Password of account
Definition: Account.cs:151
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Contains an encrypted key for an agent.
Definition: AgentKey.cs:13