Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WwwLogin.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 WwwLogin()
24 : base("Account/WwwLogin",
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(WwwLogin).Namespace + ".JSON.WwwLogin.req");
31 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(WwwLogin).Namespace + ".XML.WwwLogin.req");
32
41 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
42 {
43 // WWW-Authenticate mechanism already checks Login Auditor for blocks.
44
46 double Seconds = (double)Parameters["PSeconds"].AssociatedObjectValue;
47
48 int IssuedAt = (int)Math.Round(DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds);
49 int Expires = IssuedAt + (int)Seconds;
50
51 string Token = Factory.Create(
52 new KeyValuePair<string, object>(JwtClaims.JwtId, Convert.ToBase64String(Gateway.NextBytes(32))),
53 new KeyValuePair<string, object>(JwtClaims.Issuer, Gateway.Domain?.Value ?? string.Empty),
54 new KeyValuePair<string, object>(JwtClaims.Subject, User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty)),
55 new KeyValuePair<string, object>(JwtClaims.IssueTime, IssuedAt),
56 new KeyValuePair<string, object>(JwtClaims.ExpirationTime, Expires));
57
58 await Response.Return(new NamedDictionary<string, object>("LoggedIn", AgentNamespace)
59 {
60 { "jwt", Token },
61 { "expires", Expires }
62 });
63 }
64 }
65}
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 login, using the WWW-Authenticate header in HTTP.
Definition: WwwLogin.cs:19
WwwLogin()
Allows the client to login, using the WWW-Authenticate header in HTTP.
Definition: WwwLogin.cs:23
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: WwwLogin.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