Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
KillMachine.cs
2using System.Runtime.Serialization;
3using System.Threading.Tasks;
4using Waher.Content;
12using Waher.Security;
16
18{
20 {
21 public KillMachine()
22 : base("/KillMachine")
23 {
24 }
25
26 public override bool HandlesSubPaths => false;
27 public override bool UserSessions => true;
28 public bool AllowsPOST => true;
29
30 public async Task POST(HttpRequest Request, HttpResponse Response)
31 {
32 IUser User = Gateway.AssertUserAuthenticated(Request, "Admin.Notarius.NeuroFeatures");
33
34 if (!Request.HasData)
35 {
36 await Response.SendResponse(new BadRequestException());
37 return;
38 }
39
40 ContentResponse Content = await Request.DecodeDataAsync();
41 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Obj) ||
42 !Obj.TryGetValue("tokenId", out object Obj2) || !(Obj2 is string TokenId) ||
43 !Obj.TryGetValue("tabId", out Obj2) || !(Obj2 is string TabId))
44 {
45 await Response.SendResponse(new BadRequestException());
46 return;
47 }
48
49 Token Token = await Database.FindFirstIgnoreRest<Token>(
50 new FilterFieldEqualTo("TokenId", TokenId));
51
52 if (Token is null)
53 {
54 await Response.SendResponse(new NotFoundException());
55 return;
56 }
57
58 Response.StatusCode = 200;
59 Response.StatusMessage = "OK";
60
61 string LegalId = null;
62 int? Result = null;
63
64 if (User is Security.Users.User AdminUser)
65 {
66 if (string.IsNullOrEmpty(AdminUser.LegalId))
67 Result = -2;
68 else
69 LegalId = AdminUser.LegalId;
70 }
71 else if (User is AccountUser AccountUser)
72 {
74 Result = -2;
75 else if (!AccountUser.Account.LatestIdentityState.HasValue)
76 Result = -3;
77 else if (AccountUser.Account.LatestIdentityState.Value != Legal.Identity.IdentityState.Approved)
78 Result = -4;
79 else
81 }
82 else if (User.GetType().FullName == typeof(Login).FullName + "+InternalUser")
84 else
85 Result = -1;
86
87 if (!Result.HasValue)
88 {
89 if (string.IsNullOrEmpty(LegalId))
90 Result = -2;
91 else
92 {
93 Result = await StateMachineProcessor.KillMachine(Token, User,
94 LegalId, Request.RemoteEndPoint,
95 (_, e) =>
96 {
97 return ClientEvents.PushEvent(new string[] { TabId },
98 e.Response ? "RequestAccepted" : "RequestRejected", null);
99 }, null);
100 }
101 }
102
103 await Response.Return(new Dictionary<string, object>()
104 {
105 { "result", Result.Value }
106 });
107 }
108 }
109}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
Provides a resource that allows the caller to login to the gateway through a POST method call.
Definition: Login.cs:19
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:243
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Task Return(Exception ex)
Returns an error to the client.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Represents a case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty 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 have a named field equal to a given value.
IdentityState? LatestIdentityState
State of Last Legal Identity approved for account.
Definition: Account.cs:206
CaseInsensitiveString LatestIdentity
Last Legal Identity approved for account. Note: Identity object might not be approved any longer.
Definition: Account.cs:196
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: KillMachine.cs:30
bool AllowsPOST
If the POST method is allowed.
Definition: KillMachine.cs:28
POST Interface for HTTP resources.
Basic interface for a user.
Definition: IUser.cs:7
Definition: ImplTypes.g.cs:58