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;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
16
18{
23 {
24 private JwtAuthentication jwtAuthentication;
25
29 public GetPublicKey()
30 : base("Crypto/GetPublicKey",
31 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
32 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
33 {
34 }
35
36 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetPublicKey).Namespace + ".JSON.GetPublicKey.req");
37 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetPublicKey).Namespace + ".XML.GetPublicKey.req");
38
47 {
48 foreach (HttpAuthenticationScheme Scheme in base.GetAuthenticationSchemes(Request))
49 {
50 if (Scheme is JwtAuthentication JwtAuth)
51 {
52 this.jwtAuthentication = JwtAuth;
53 break;
54 }
55 }
56
57 return null;
58 }
59
68 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
69 {
70 EllipticCurveEndpoint Endpoint;
71 string KeyId = null;
72 string Namespace;
73 byte[] PublicKey;
74
75 if (Parameters.TryGetValue("PKeyId", out IElement E) && !(E is null) && E.AssociatedObjectValue is string s)
76 KeyId = s;
77
78 if (string.IsNullOrEmpty(KeyId))
79 {
81 throw new ServiceUnavailableException("Public key not configured.");
82
84 Namespace = Endpoint.Namespace;
85 }
86 else
87 {
88 AccountUser User = (this.jwtAuthentication is null ? null : await this.jwtAuthentication.IsAuthenticated(Request) as AccountUser)
89 ?? throw new ForbiddenException(Request, "User not authenticated.");
90
91 if (!User.Account.Enabled)
92 throw new ForbiddenException(Request, "Account not enabled.");
93
94 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
95 new FilterFieldEqualTo("Account", User.UserName),
96 new FilterFieldEqualTo("Id", KeyId)))
97 ?? throw new NotFoundException("Key not found.");
98
100 throw new ServiceUnavailableException("Key algorithm no longer supported.");
101
102 PublicKey = AgentKey.PublicKey ?? Array.Empty<byte>();
103 Namespace = AgentKey.Namespace;
104 }
105
106 await Response.Return(new NamedDictionary<string, object>("PublicKey", AgentNamespace)
107 {
108 { "key", Convert.ToBase64String(PublicKey) },
109 { "Algorithm", new Dictionary<string, object>()
110 {
111 { "localName", Endpoint.LocalName },
112 { "namespace", Namespace },
113 { "securityStrength", Endpoint.SecurityStrength },
114 { "safe", Endpoint.Safe },
115 { "slow", Endpoint.Slow },
116 { "pqc", Endpoint.PostQuantumCryptography },
117 { "score", Endpoint.Score }
118 }
119 }
120 });
121 }
122
123 }
124}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Represents an HTTP request.
Definition: HttpRequest.cs:22
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...
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: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
Use JWT tokens for authentication. The Bearer scheme defined in RFC 6750 is used: https://tools....
override async Task< IUser > IsAuthenticated(HttpRequest Request)
Checks if the request is authorized.
bool Enabled
If account is enabled
Definition: Account.cs:354
Abstract base class for agent resources supporting the POST method.
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:68
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
Definition: GetPublicKey.cs:46
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:21