Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetSessionToken.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
8using Waher.Script;
10
12{
17 {
22 : base("Account/GetSessionToken",
23 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
24 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
25 {
26 }
27
28 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetSessionToken).Namespace + ".JSON.GetSessionToken.req");
29 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetSessionToken).Namespace + ".XML.GetSessionToken.req");
30
39 {
40 base.GetAuthenticationSchemes(Request); // Initialize factory property.
41 return null;
42 }
43
47 public override bool UserSessions => true;
48
57 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
58 {
60 !(v.ValueElement.AssociatedObjectValue is SessionTokenInfo Info))
61 {
62 throw new NotFoundException("Session token information not found.");
63 }
64
65 if (Info.Expires < DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds)
66 throw new BadRequestException("Token has expired.");
67
68 await Response.Return(new NamedDictionary<string, object>("SessionToken", AgentNamespace)
69 {
70 { "AccountCreated", new Dictionary<string,object>()
71 {
72 { "created", Info.Account.Created },
73 { "updated", Info.Account.Updated },
74 { "enabled", Info.Account.Enabled },
75 { "canRelay", Info.Account.CanRelayMessages },
76 { "jwt", Info.Token },
77 { "expires", Info.Expires },
78 }
79 },
80 { "seconds", Info.Seconds }
81 });
82 }
83 }
84}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static readonly DateTime UnixEpoch
Unix Date and Time epoch, starting at 1970-01-01T00:00:00Z
Definition: JSON.cs:20
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
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
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...
override bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
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
const string SessionTokenInfoVariableName
Name of session variable where token information is stored.
Gets information about a newly created token stored in a session variable.
GetSessionToken()
Gets information about a newly created token stored in a session variable.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
override bool UserSessions
If the resource uses user sessions.
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
Gets information about the currently logged in account.
Definition: Info.cs:18
Abstract base class for agent resources supporting the POST method.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd