Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ResetPassword.cs
1using System;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Events;
9
11{
16 {
21 : base(@"^reset\s+password\s+(?'Name'[^\s]+)$")
22 {
23 }
24
28 public override string Name => "Reset";
29
38 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
39 ResponseCallbackHandler ResponseCallback)
40 {
41 string Name = Details.Groups["Name"].Value;
42
43 IUser User0 = await Waher.Security.Users.Users.GetUser(Name, false)
44 ?? throw new Exception("User not found.");
45
46 User User = User0 as User
47 ?? throw new Exception("User not editable.");
48
49 string Password = Base64Url.Encode(Gateway.NextBytes(32));
50 await ResponseCallback("Generating password: `" + Password + "`", string.Empty);
51
52 User.PasswordHash = Convert.ToBase64String(Waher.Security.Users.Users.ComputeHash(Name, Password));
53
54 await Persistence.Database.Update(User);
55
57
58 Log.Informational("Password changed via Chat-Admin.", User.UserName, State.To);
59
60 await ResponseCallback("User updated: `" + Name + "`", string.Empty);
61 }
62
67 public override HelpItem[] GetHelp()
68 {
69 return new HelpItem[]
70 {
71 new HelpItem("reset password NAME", "Resets the password of an existing user with user name `NAME`.")
72 };
73 }
74 }
75}
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
Definition: Base64Url.cs:11
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Definition: Base64Url.cs:48
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:334
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
Corresponds to a user in the system.
Definition: User.cs:21
string UserName
User Name
Definition: User.cs:53
Maintains the collection of all users in the system.
Definition: Users.cs:24
static byte[] ComputeHash(string UserName, string Password)
Computes a hash of a password.
Definition: Users.cs:157
static async Task< User > GetUser(string UserName, bool CreateIfNew)
Gets the User object corresponding to a User Name.
Definition: Users.cs:65
static void ClearCache()
Clears internal caches.
Definition: Users.cs:202
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Contains an item of information about a command.
Definition: HelpItem.cs:9
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
override HelpItem[] GetHelp()
Gets help about the command.
ResetPassword()
Resets the password of a user.
Basic interface for a user.
Definition: IUser.cs:7
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.
Definition: App.xaml.cs:4