Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AccountUser.cs
1using System.Collections.Generic;
2using System;
3using System.Threading.Tasks;
6using Waher.Things;
7using Waher.Content;
9
11{
16 {
17 private readonly Account account;
18
24 {
25 this.account = Account;
26 }
27
31 public Account Account => this.account;
32
36 public string UserName => this.account.UserName;
37
41 public string PasswordHash => this.account.Password;
42
46 public string PasswordHashType => string.Empty;
47
53 public bool HasPrivilege(string Privilege)
54 {
55 return false;
56 }
57
61 public Task<RequestOrigin> GetOrigin()
62 {
63 return Task.FromResult(new RequestOrigin(this.UserName + "@" + Gateway.Domain, null, null, null));
64 }
65
71 public Task<IEnumerable<KeyValuePair<string, object>>> CreateClaims(bool Encrypted)
72 {
73 if (!this.account.Enabled)
74 return Task.FromResult<IEnumerable<KeyValuePair<string, object>>>(null);
75
76 int IssuedAt = (int)Math.Round(DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds);
77 int Expires = IssuedAt + 3600;
78
79 List<KeyValuePair<string, object>> Claims = new List<KeyValuePair<string, object>>()
80 {
81 new KeyValuePair<string, object>(JwtClaims.JwtId, Convert.ToBase64String(Gateway.NextBytes(32))),
82 new KeyValuePair<string, object>(JwtClaims.Subject, this.account.UserName + "@" + Gateway.Domain),
83 new KeyValuePair<string, object>(JwtClaims.IssueTime, IssuedAt),
84 new KeyValuePair<string, object>(JwtClaims.ExpirationTime, Expires),
85 new KeyValuePair<string, object>(JwtClaims.Issuer, Gateway.Domain)
86 };
87
88 if (Encrypted)
89 {
90 if (!string.IsNullOrWhiteSpace(this.account.EMail))
91 Claims.Add(new KeyValuePair<string, object>(JwtClaims.EMail, this.account.EMail));
92
93 if (!string.IsNullOrWhiteSpace(this.account.PhoneNr))
94 Claims.Add(new KeyValuePair<string, object>(JwtClaims.PhoneNumber, this.account.PhoneNr));
95
96 if (this.account.CanRelayMessages)
97 Claims.Add(new KeyValuePair<string, object>(JwtClaims.Entitlements, "+" + SmtpServer.SmtpRelayPrivilegeID.Replace(".", "\\.")));
98 }
99
100 return Task.FromResult<IEnumerable<KeyValuePair<string, object>>>(Claims);
101 }
102
109 public async Task<string> CreateToken(JwtFactory Factory, bool Encrypted)
110 {
111 IEnumerable<KeyValuePair<string, object>> Claims = await this.CreateClaims(Encrypted);
112 if (Claims is null)
113 return null;
114
115 return Factory.Create(Claims);
116 }
117 }
118}
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
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
Implements a simple SMTP Server, as defined in:
Definition: SmtpServer.cs:44
const string SmtpRelayPrivilegeID
SmtpRelay
Definition: SmtpServer.cs:68
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 EMail
Preferred e-mail address
Definition: JwtClaims.cs:94
const string PhoneNumber
Preferred telephone number
Definition: JwtClaims.cs:124
const string Subject
Subject of the JWT (the user)
Definition: JwtClaims.cs:19
const string Entitlements
Entitlements
Definition: JwtClaims.cs:169
const string ExpirationTime
Time after which the JWT expires
Definition: JwtClaims.cs:29
A factory that can create and validate JWT tokens.
Definition: JwtFactory.cs:53
string Create(params KeyValuePair< string, object >[] Claims)
Creates a new JWT token.
Definition: JwtFactory.cs:248
Contains information about a broker account.
Definition: Account.cs:28
AccountUser(Account Account)
Account user object.
Definition: AccountUser.cs:23
string PasswordHashType
Type of password hash. The empty stream means a clear-text password.
Definition: AccountUser.cs:46
Task< RequestOrigin > GetOrigin()
Origin of request.
Definition: AccountUser.cs:61
Task< IEnumerable< KeyValuePair< string, object > > > CreateClaims(bool Encrypted)
Creates a set of claims identifying the user.
Definition: AccountUser.cs:71
bool HasPrivilege(string Privilege)
If the user has a given privilege.
Definition: AccountUser.cs:53
async Task< string > CreateToken(JwtFactory Factory, bool Encrypted)
Creates a JWT Token referencing the user object.
Definition: AccountUser.cs:109
Tokens available in request.
Definition: RequestOrigin.cs:9
A User that can participate in distributed operations, where the user is identified using a JWT token...
Interface for requestors that can act as an origin for distributed requests.