Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetPublicKey.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
14
16{
21 {
25 public GetPublicKey()
26 : base("Crypto/GetPublicKey",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetPublicKey).Namespace + ".JSON.GetPublicKey.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetPublicKey).Namespace + ".XML.GetPublicKey.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 EllipticCurveEndpoint Endpoint;
48 string KeyId = null;
49 string Namespace;
50 byte[] PublicKey;
51
52 if (Parameters.TryGetValue("PKeyId", out IElement E) && !(E is null) && E.AssociatedObjectValue is string s)
53 KeyId = s;
54
55 if (string.IsNullOrEmpty(KeyId))
56 {
58 throw new ServiceUnavailableException("Public key not configured.");
59
61 Namespace = Endpoint.Namespace;
62 }
63 else
64 {
65 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
66 new FilterFieldEqualTo("Account", User.UserName),
67 new FilterFieldEqualTo("Id", KeyId)))
68 ?? throw new NotFoundException("Key not found.");
69
71 throw new ServiceUnavailableException("Key algorithm no longer supported.");
72
73 PublicKey = AgentKey.PublicKey ?? new byte[0];
74 Namespace = AgentKey.Namespace;
75 }
76
77 await Response.Return(new NamedDictionary<string, object>("PublicKey", AgentNamespace)
78 {
79 { "key", Convert.ToBase64String(PublicKey) },
80 { "Algorithm", new Dictionary<string, object>()
81 {
82 { "localName", Endpoint.LocalName },
83 { "namespace", Namespace },
84 { "securityStrength", Endpoint.SecurityStrength },
85 { "safe", Endpoint.Safe },
86 { "slow", Endpoint.Slow },
87 { "score", Endpoint.Score }
88 }
89 }
90 });
91 }
92
93 }
94}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Abstract base class for Elliptic Curve endpoints.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
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.
Class managing a script expression.
Definition: Expression.cs:39
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
static bool TryGetAlgorithm(string LocalName, string Namespace, out EllipticCurveEndpoint Algorithm)
Tries to get an algorithm given its fully qualified name.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetPublicKey.cs:43
static LedgerConfiguration Instance
Current instance of configuration.
string SignatureAlgorithm
Name of signature algorithm.
byte[] PublicKey
Public key used to validate signatures.
Basic interface for all types of elements.
Definition: IElement.cs:20