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;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
10using Waher.Things;
11
13{
18 {
19 private readonly Account account;
20
26 {
27 this.account = Account;
28 }
29
33 public Account Account => this.account;
34
38 public string UserName => this.account.UserName;
39
43 public string FederatedUserName => this.account.UserName + "@" + Gateway.Domain;
44
48 public string FriendlyName
49 {
50 get
51 {
52 string s = this.account.FullName;
53 return string.IsNullOrEmpty(s) ? this.UserName : s;
54 }
55 }
56
60 public string PasswordHash => this.account.Password;
61
65 public string PasswordHashType => string.Empty;
66
72 public bool HasPrivilege(string Privilege)
73 {
74 return this.account.HasPrivilege(Privilege);
75 }
76
80 public Task<RequestOrigin> GetOrigin()
81 {
82 return Task.FromResult(new RequestOrigin(this.UserName + "@" + Gateway.Domain, null, null, null, this));
83 }
84
90 public async Task<IEnumerable<KeyValuePair<string, object>>> CreateClaims(bool Encrypted)
91 {
92 if (!this.account.Enabled)
93 return null;
94
95 int IssuedAt = (int)Math.Round(DateTime.UtcNow.Subtract(JSON.UnixEpoch).TotalSeconds);
96 int Expires = IssuedAt + 3600;
97
98 List<KeyValuePair<string, object>> Claims = new List<KeyValuePair<string, object>>()
99 {
100 new KeyValuePair<string, object>(JwtClaims.JwtId, Convert.ToBase64String(Gateway.NextBytes(32))),
101 new KeyValuePair<string, object>(JwtClaims.Subject, this.account.UserName + "@" + Gateway.Domain),
102 new KeyValuePair<string, object>(JwtClaims.IssueTime, IssuedAt),
103 new KeyValuePair<string, object>(JwtClaims.ExpirationTime, Expires),
104 new KeyValuePair<string, object>(JwtClaims.Issuer, Gateway.Domain)
105 };
106
107 if (Encrypted)
108 {
109 if (!string.IsNullOrWhiteSpace(this.account.EMail))
110 Claims.Add(new KeyValuePair<string, object>(JwtClaims.EMail, this.account.EMail));
111
112 if (!string.IsNullOrWhiteSpace(this.account.PhoneNr))
113 Claims.Add(new KeyValuePair<string, object>(JwtClaims.PhoneNumber, this.account.PhoneNr));
114
115 if (this.account.CanRelayMessages)
116 Claims.Add(new KeyValuePair<string, object>(JwtClaims.Entitlements, "+" + SmtpServer.SmtpRelayPrivilegeID.Replace(".", "\\.")));
117
118 if ((this.account.RoleIds?.Length ?? 0) > 0)
119 {
120 StringBuilder sb = null;
121
122 foreach (string RoleId in this.account.RoleIds)
123 User.AppendValue(ref sb, "\"" + RoleId + "\"", ", ");
124
125 Claims.Add(new KeyValuePair<string, object>(JwtClaims.Roles, sb?.ToString()));
126
127 sb = null;
128
129 foreach (Role Role in await this.account.LoadRoles())
130 {
132 {
133 if (Privilege.Include)
134 User.AppendValue(ref sb, "+" + Privilege.Expression, Environment.NewLine);
135 else
136 User.AppendValue(ref sb, "-" + Privilege.Expression, Environment.NewLine);
137 }
138 }
139
140 Claims.Add(new KeyValuePair<string, object>(JwtClaims.Entitlements, sb?.ToString()));
141 }
142 }
143
144 return Claims;
145 }
146
154 public async Task<string> CreateToken(JwtFactory Factory, bool Encrypted,
155 params KeyValuePair<string, object>[] AdditionalClaims)
156 {
157 IEnumerable<KeyValuePair<string, object>> Claims = await this.CreateClaims(Encrypted);
158 if (Claims is null)
159 return null;
160
161 Claims = JwtFactory.JoinClaims(Claims, AdditionalClaims);
162 return Factory.Create(Claims);
163 }
164 }
165}
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
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:4335
Implements a simple SMTP Server, as defined in:
Definition: SmtpServer.cs:45
const string SmtpRelayPrivilegeID
SmtpRelay
Definition: SmtpServer.cs:69
Static class containing predefined JWT claim names.
Definition: JwtClaims.cs:10
const string Issuer
Issuer of the JWT
Definition: JwtClaims.cs:14
const string Roles
Roles
Definition: JwtClaims.cs:159
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:66
static IEnumerable< KeyValuePair< string, object > > JoinClaims(IEnumerable< KeyValuePair< string, object > > Claims1, params KeyValuePair< string, object >[] Claims2)
Joins two sets of claims into one.
Definition: JwtFactory.cs:528
string Create(params KeyValuePair< string, object >[] Claims)
Creates a new JWT token.
Definition: JwtFactory.cs:379
Corresponds to a privilege in the system.
Definition: Privilege.cs:16
Contains a reference to a privilege
Corresponds to a role in the system.
Definition: Role.cs:15
PrivilegePattern[] Privileges
Privileges
Definition: Role.cs:62
Corresponds to a user in the system.
Definition: User.cs:24
static void AppendValue(ref StringBuilder Output, object Value, string Delimiter)
Appends a value to a StringBuilder.
Definition: User.cs:304
Contains information about a broker account.
Definition: Account.cs:41
AccountUser(Account Account)
Account user object.
Definition: AccountUser.cs:25
string FriendlyName
Friendly name of the user, for display purposes.
Definition: AccountUser.cs:49
string PasswordHashType
Type of password hash. The empty stream means a clear-text password.
Definition: AccountUser.cs:65
Task< RequestOrigin > GetOrigin()
Origin of request.
Definition: AccountUser.cs:80
string FederatedUserName
Full Federated User Name.
Definition: AccountUser.cs:43
async Task< IEnumerable< KeyValuePair< string, object > > > CreateClaims(bool Encrypted)
Creates a set of claims identifying the user.
Definition: AccountUser.cs:90
async Task< string > CreateToken(JwtFactory Factory, bool Encrypted, params KeyValuePair< string, object >[] AdditionalClaims)
Creates a JWT Token referencing the user object.
Definition: AccountUser.cs:154
bool HasPrivilege(string Privilege)
If the user has a given privilege.
Definition: AccountUser.cs:72
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.