source: binary-improvements2/WebServer/src/Commands/WebTokens.cs@ 402

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 3.5 KB
RevLine 
[391]1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using JetBrains.Annotations;
4
5namespace Webserver.Commands {
6 [UsedImplicitly]
7 public class WebTokens : ConsoleCmdAbstract {
8 private static readonly Regex validNameTokenMatcher = new Regex (@"^\w+$");
9
10 public override string[] GetCommands () {
11 return new[] {"webtokens"};
12 }
13
14 public override string GetDescription () {
15 return "Manage web tokens";
16 }
17
18 public override string GetHelp () {
19 return "Set/get webtoken permission levels. A level of 0 is maximum permission.\n" +
20 "Usage:\n" +
21 " webtokens add <username> <usertoken> <level>\n" +
22 " webtokens remove <username>\n" +
23 " webtokens list";
24 }
25
26 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
27 if (_params.Count >= 1) {
28 if (_params [0].EqualsCaseInsensitive ("add")) {
29 ExecuteAdd (_params);
30 } else if (_params [0].EqualsCaseInsensitive ("remove")) {
31 ExecuteRemove (_params);
32 } else if (_params [0].EqualsCaseInsensitive ("list")) {
33 ExecuteList ();
34 } else {
[402]35 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
[391]36 }
37 } else {
38 SdtdConsole.Instance.Output ("No sub command given.");
39 }
40 }
41
42 private void ExecuteAdd (List<string> _params) {
43 if (_params.Count != 4) {
[402]44 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}.");
[391]45 return;
46 }
47
48 if (string.IsNullOrEmpty (_params [1])) {
49 SdtdConsole.Instance.Output ("Argument 'username' is empty.");
50 return;
51 }
52
53 if (!validNameTokenMatcher.IsMatch (_params [1])) {
54 SdtdConsole.Instance.Output (
55 "Argument 'username' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
56 return;
57 }
58
59 if (string.IsNullOrEmpty (_params [2])) {
60 SdtdConsole.Instance.Output ("Argument 'usertoken' is empty.");
61 return;
62 }
63
64 if (!validNameTokenMatcher.IsMatch (_params [2])) {
65 SdtdConsole.Instance.Output (
66 "Argument 'usertoken' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
67 return;
68 }
69
70 if (!int.TryParse (_params [3], out int level)) {
71 SdtdConsole.Instance.Output ("Argument 'level' is not a valid integer.");
72 return;
73 }
74
75 WebPermissions.Instance.AddAdmin (_params [1], _params [2], level);
76 SdtdConsole.Instance.Output ($"Web user with name={_params [1]} and password={_params [2]} added with permission level of {level}.");
77 }
78
79 private void ExecuteRemove (List<string> _params) {
80 if (_params.Count != 2) {
[402]81 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
[391]82 return;
83 }
84
85 if (string.IsNullOrEmpty (_params [1])) {
86 SdtdConsole.Instance.Output ("Argument 'username' is empty.");
87 return;
88 }
89
90 if (!validNameTokenMatcher.IsMatch (_params [1])) {
91 SdtdConsole.Instance.Output (
92 "Argument 'username' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
93 return;
94 }
95
96 WebPermissions.Instance.RemoveAdmin (_params [1]);
97 SdtdConsole.Instance.Output ($"{_params [1]} removed from web user permissions list.");
98 }
99
100 private void ExecuteList () {
101 SdtdConsole.Instance.Output ("Defined webuser permissions:");
102 SdtdConsole.Instance.Output (" Level: Name / Token");
103 foreach (WebPermissions.AdminToken at in WebPermissions.Instance.GetAdmins ()) {
104 SdtdConsole.Instance.Output ($" {at.permissionLevel,5}: {at.name} / {at.token}");
105 }
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.