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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
7using Waher.Script;
9
11{
16 {
21 : base("Account/GetSessionToken",
22 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
23 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
24 {
25 }
26
27 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetSessionToken).Namespace + ".JSON.GetSessionToken.req");
28 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetSessionToken).Namespace + ".XML.GetSessionToken.req");
29
35 {
36 base.GetAuthenticationSchemes(Request); // Initialize factory property.
37 return null;
38 }
39
43 public override bool UserSessions => true;
44
53 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
54 {
56 !(v.ValueElement.AssociatedObjectValue is SessionTokenInfo Info))
57 {
58 throw new NotFoundException("Session token information not found.");
59 }
60
61 if (Info.Expires < DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds)
62 throw new BadRequestException("Token has expired.");
63
64 await Response.Return(new NamedDictionary<string, object>("SessionToken", AgentNamespace)
65 {
66 { "AccountCreated", new Dictionary<string,object>()
67 {
68 { "created", Info.Account.Created },
69 { "enabled", Info.Account.Enabled },
70 { "canRelay", Info.Account.CanRelayMessages },
71 { "jwt", Info.Token },
72 { "expires", Info.Expires },
73 }
74 },
75 { "seconds", Info.Seconds }
76 });
77 }
78 }
79}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static readonly DateTime UnixEpoch
Unix Date and Time epoch, starting at 1970-01-01T00:00:00Z
Definition: JSON.cs:18
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
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:18
Variables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:164
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Class managing a script expression.
Definition: Expression.cs:39
Contains information about a variable.
Definition: Variable.cs:10
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
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:17
Abstract base class for agent resources supporting the POST method.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd