Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AgentApi.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
14
16{
20 public class AgentApi : IModule
21 {
22 private IAgentResource[] resources;
23 private IPersistentDictionary nonceValues;
24 private readonly LoginAuditor auditor;
25
29 public AgentApi()
30 : this(null, null)
31 {
32 }
33
40 {
41 AgentResource.Factory = Factory;
42 this.auditor = Auditor ?? Gateway.LoginAuditor;
43 }
44
48 public LoginAuditor Auditor => this.auditor;
49
53 public Task Start()
54 {
55 return this.Start(Gateway.HttpServer);
56 }
57
62 public async Task Start(HttpServer WebServer)
63 {
64 this.nonceValues = await Database.GetDictionary("Nonces");
65 List<IAgentResource> Resources = new List<IAgentResource>();
66
67 foreach (Type T in Types.GetTypesImplementingInterface(typeof(IAgentResource)))
68 {
69 if (T.IsAbstract)
70 continue;
71
72 try
73 {
75
76 await Resource.Register(WebServer, this);
77 }
78 catch (Exception ex)
79 {
80 Log.Exception(ex, T.FullName);
81 }
82 }
83
84 this.resources = Resources.ToArray();
85 }
86
90 public Task Stop()
91 {
92 return this.Stop(Gateway.HttpServer);
93 }
94
99 public async Task Stop(HttpServer WebServer)
100 {
101 if (!(this.resources is null))
102 {
103 foreach (IAgentResource Resource in this.resources)
104 await Resource.Unregister(WebServer);
105 }
106 }
107
113 public Task<bool> HasNonceBeenUsed(string Nonce)
114 {
115 return this.nonceValues.ContainsKeyAsync(Nonce);
116 }
117
122 public Task RegisterNonceValue(string Nonce)
123 {
124 return this.nonceValues.AddAsync(Nonce, true);
125 }
126
132 public async Task<ApiKey> GetApiKey(string ApiKey)
133 {
134 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
135 return await PersistenceLayer.GetApiKey(ApiKey);
136 }
137
142 public async Task<ApiKey> GetAgentApiApiKey()
143 {
144 return await Database.FindFirstDeleteRest<ApiKey>(
145 new FilterFieldEqualTo("Owner", "Agent API"));
146 }
147 }
148}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static HttpServer HttpServer
HTTP Server
Definition: Gateway.cs:3299
static LoginAuditor LoginAuditor
Current Login Auditor. Should be used by modules accepting user logins, to protect the system from un...
Definition: Gateway.cs:3033
Implements an HTTP server.
Definition: HttpServer.cs:36
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IPersistentDictionary > GetDictionary(string Collection)
Gets a persistent dictionary containing objects in a collection.
Definition: Database.cs:1542
This filter selects objects that have a named field equal to a given value.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1353
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
A factory that can create and validate JWT tokens.
Definition: JwtFactory.cs:53
Class that monitors login events, and help applications determine malicious intent....
Definition: LoginAuditor.cs:26
async Task< ApiKey > GetAgentApiApiKey()
Gets API Key for the Agent API.
Definition: AgentApi.cs:142
Task< bool > HasNonceBeenUsed(string Nonce)
Checks if a Nonce value has been used.
Definition: AgentApi.cs:113
AgentApi(JwtFactory Factory, LoginAuditor Auditor)
Agent API Module.
Definition: AgentApi.cs:39
async Task< ApiKey > GetApiKey(string ApiKey)
Gets an API Key
Definition: AgentApi.cs:132
Task RegisterNonceValue(string Nonce)
Registers a nonce value.
Definition: AgentApi.cs:122
async Task Start(HttpServer WebServer)
Starts the module using a specific web server.
Definition: AgentApi.cs:62
async Task Stop(HttpServer WebServer)
Stops the module using a specific web server.
Definition: AgentApi.cs:99
Persistent dictionary that can contain more entries than possible in the internal memory.
Task< bool > ContainsKeyAsync(string key)
Determines whether the System.Collections.Generic.IDictionary{string,object} contains an element with...
Task AddAsync(string key, object value)
Adds an element with the provided key and value to the System.Collections.Generic....
Interface for late-bound modules loaded at runtime.
Definition: IModule.cs:9
Task Unregister(HttpServer WebServer)
Unregisters the resource from a web server.
Task Register(HttpServer WebServer, AgentApi AgentApi)
Registers the resource on a web server.