Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Refresh.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
8using Waher.Script;
12
14{
19 {
23 public Refresh()
24 : base("Account/Refresh",
25 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
26 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
27 {
28 }
29
30 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(Refresh).Namespace + ".JSON.Refresh.req");
31 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(Refresh).Namespace + ".XML.Refresh.req");
32
41 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
42 {
43 AccountUser User = AssertUserAuthenticated(Request, false);
44 double Seconds = (double)Parameters["PSeconds"].AssociatedObjectValue;
45
46 int IssuedAt = (int)Math.Round(DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds);
47 int Expires = IssuedAt + (int)Seconds;
48
49 string Token = Factory.Create(
50 new KeyValuePair<string, object>(JwtClaims.JwtId, Convert.ToBase64String(Gateway.NextBytes(32))),
51 new KeyValuePair<string, object>(JwtClaims.Issuer, Gateway.Domain?.Value ?? string.Empty),
52 new KeyValuePair<string, object>(JwtClaims.Subject, User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty)),
53 new KeyValuePair<string, object>(JwtClaims.IssueTime, IssuedAt),
54 new KeyValuePair<string, object>(JwtClaims.ExpirationTime, Expires));
55
56 await Response.Return(new NamedDictionary<string, object>("LoggedIn", AgentNamespace)
57 {
58 { "jwt", Token },
59 { "expires", Expires }
60 });
61 }
62 }
63}
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
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
Represents an HTTP request.
Definition: HttpRequest.cs:18
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,...
Class managing a script expression.
Definition: Expression.cs:39
Static class containing predefined JWT claim names.
Definition: JwtClaims.cs:10
const string Issuer
Issuer of the JWT
Definition: JwtClaims.cs:14
const string IssueTime
Time at which the JWT was issued; can be used to determine age of the JWT
Definition: JwtClaims.cs:39
const string JwtId
Unique identifier; can be used to prevent the JWT from being replayed (allows a token to be used only...
Definition: JwtClaims.cs:44
const string Subject
Subject of the JWT (the user)
Definition: JwtClaims.cs:19
const string ExpirationTime
Time after which the JWT expires
Definition: JwtClaims.cs:29
string Create(params KeyValuePair< string, object >[] Claims)
Creates a new JWT token.
Definition: JwtFactory.cs:248
Allows the client to refresh the session token and maintain the login.
Definition: Refresh.cs:19
Refresh()
Allows the client to refresh the session token and maintain the login.
Definition: Refresh.cs:23
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: Refresh.cs:41
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd