Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RemoteQuickLogin.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
27 : base("Account/RemoteQuickLogin",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(Login).Namespace + ".JSON.RemoteQuickLogin.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(Login).Namespace + ".XML.RemoteQuickLogin.req");
35
44 {
45 base.GetAuthenticationSchemes(Request); // Initialize factory property.
46 return null;
47 }
48
52 public override bool UserSessions => true;
53
62 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
63 {
64 await this.CheckBlocks(Request);
65
66 string LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
67 string Purpose = (string)Parameters["PPurpose"].AssociatedObjectValue;
68 string TabID = (string)Parameters["PTabId"].AssociatedObjectValue;
69
70 if (string.IsNullOrEmpty(Purpose.Trim()))
71 throw new BadRequestException("Invalid purpose.");
72
73 if (Request.Session is null)
74 throw new ForbiddenException(Request, "No session established.");
75
76 if (Request.Session.TryGetVariable(IoTBroker.Legal.MFA.QuickLogin.UserVariableName, out Variable v) &&
77 v.ValueObject is QuickLoginUser QuickLoginUser &&
78 !string.IsNullOrEmpty(QuickLoginUser.LegalId.Id))
79 {
80 if (QuickLoginUser.LegalId.Id == LegalId)
81 {
82 await Response.Return(new NamedDictionary<string, object>("RemoteQuickLoginResponse", AgentNamespace)
83 {
84 { "loggedIn", true },
85 { "petitionSent", false }
86 });
87 return;
88 }
89 else
90 {
91 Request.Session.Remove(IoTBroker.Legal.MFA.QuickLogin.UserVariableName); // Logging out old user
92 Request.Session.Remove(IoTGateway.WebResources.Login.UserVariableName); // Logging out old associated admin user, if any
93 Request.Session.Remove(IoTGateway.WebResources.Login.AutoLoginVariableName); // Logging out old associated admin user, if any
94 }
95 }
96
97 string HttpSessionID = GetSessionId(Request, Request.Response);
98 if (string.IsNullOrEmpty(HttpSessionID))
99 throw new BadRequestException("No session cookie available in the request.");
100
101 Gateway.HttpServer?.GetSession(HttpSessionID, true); // Make sure session variables are created.
102
104 {
106 CaseInsensitiveString Domain = LatestApprovedIdentity[PersonalInformation.DomainTag];
107 if (!Gateway.IsDomain(Domain, true))
108 throw new InvalidOperationException("Domain in latest legal identity of gateway does not match the gateway domain.");
109
110 string ServiceId = IoTBroker.Legal.MFA.QuickLogin.GetServiceID(HttpSessionID, null);
111
112 await IoTBroker.Legal.MFA.QuickLogin.StartQuickLogin(Request, ServiceId, LegalId,
113 Purpose, null, null, TabID);
114
115 await Response.Return(new NamedDictionary<string, object>("RemoteQuickLoginResponse", AgentNamespace)
116 {
117 { "loggedIn", false },
118 { "petitionSent", true }
119 });
120 }
121 else
122 {
123 await Response.Return(new NamedDictionary<string, object>("RemoteQuickLoginResponse", AgentNamespace)
124 {
125 { "loggedIn", false },
126 { "petitionSent", false }
127 });
128 }
129 }
130 }
131}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static HttpServer HttpServer
HTTP Server
Definition: Gateway.cs:4118
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
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...
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Represents an HTTP request.
Definition: HttpRequest.cs:22
SessionVariables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:212
HttpResponse Response
HTTP Response object, if one has been assigned to the request.
Definition: HttpRequest.cs:254
static string GetSessionId(HttpRequest Request, HttpResponse Response)
Gets the session ID used for a request.
const string HttpSessionID
The Cookie Key for HTTP Session Identifiers: "HttpSessionID"
Definition: HttpResource.cs:27
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
SessionVariables GetSession(string SessionId)
Gets the set of session states corresponing to a given session ID. If no such session is known,...
Definition: HttpServer.cs:2097
override bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Contains personal information found in a legal identity.
Represents a case-insensitive string.
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 information about a variable.
Definition: Variable.cs:10
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:151
Allows the client to quick login based on a login on another federated broker.
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
override bool UserSessions
If the resource uses user sessions.
RemoteQuickLogin()
Allows the client to quick login based on a login on another federated broker.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Abstract base class for agent resources supporting the POST method.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
async Task CheckBlocks(HttpRequest Request)
Checks if the client is blocked.