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

Last change on this file since 425 was 405, checked in by alloc, 21 months ago

Refactored console commands for A21 caching

File size: 3.6 KB
Line 
1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using JetBrains.Annotations;
4using Webserver.Permissions;
5
6namespace Webserver.Commands {
7 [UsedImplicitly]
8 public class WebTokens : ConsoleCmdAbstract {
9 private static readonly Regex validNameTokenMatcher = new Regex (@"^\w+$");
10
11 protected override string[] getCommands () {
12 return new[] {"webtokens"};
13 }
14
15 protected override string getDescription () {
16 return "Manage web tokens";
17 }
18
19 protected override string getHelp () {
20 return "Set/get webtoken permission levels. A level of 0 is maximum permission.\n" +
21 "Usage:\n" +
22 " webtokens add <tokenname> <tokensecret> <level>\n" +
23 " webtokens remove <tokenname>\n" +
24 " webtokens list";
25 }
26
27 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
28 if (_params.Count >= 1) {
29 if (_params [0].EqualsCaseInsensitive ("add")) {
30 ExecuteAdd (_params);
31 } else if (_params [0].EqualsCaseInsensitive ("remove")) {
32 ExecuteRemove (_params);
33 } else if (_params [0].EqualsCaseInsensitive ("list")) {
34 ExecuteList ();
35 } else {
36 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
37 }
38 } else {
39 SdtdConsole.Instance.Output ("No sub command given.");
40 }
41 }
42
43 private void ExecuteAdd (List<string> _params) {
44 if (_params.Count != 4) {
45 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}.");
46 return;
47 }
48
49 if (string.IsNullOrEmpty (_params [1])) {
50 SdtdConsole.Instance.Output ("Argument 'tokenname' is empty.");
51 return;
52 }
53
54 if (!validNameTokenMatcher.IsMatch (_params [1])) {
55 SdtdConsole.Instance.Output (
56 "Argument 'tokenname' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
57 return;
58 }
59
60 if (string.IsNullOrEmpty (_params [2])) {
61 SdtdConsole.Instance.Output ("Argument 'tokensecret' is empty.");
62 return;
63 }
64
65 if (!validNameTokenMatcher.IsMatch (_params [2])) {
66 SdtdConsole.Instance.Output (
67 "Argument 'tokensecret' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
68 return;
69 }
70
71 if (!int.TryParse (_params [3], out int level)) {
72 SdtdConsole.Instance.Output ("Argument 'level' is not a valid integer.");
73 return;
74 }
75
76 AdminApiTokens.Instance.AddToken (_params [1], _params [2], level);
77 SdtdConsole.Instance.Output ($"Web API token with name={_params [1]} and secret={_params [2]} added with permission level of {level}.");
78 }
79
80 private void ExecuteRemove (List<string> _params) {
81 if (_params.Count != 2) {
82 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
83 return;
84 }
85
86 if (string.IsNullOrEmpty (_params [1])) {
87 SdtdConsole.Instance.Output ("Argument 'tokenname' is empty.");
88 return;
89 }
90
91 if (!validNameTokenMatcher.IsMatch (_params [1])) {
92 SdtdConsole.Instance.Output (
93 "Argument 'tokenname' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
94 return;
95 }
96
97 AdminApiTokens.Instance.RemoveToken (_params [1]);
98 SdtdConsole.Instance.Output ($"{_params [1]} removed from web API token permissions list.");
99 }
100
101 private void ExecuteList () {
102 SdtdConsole.Instance.Output ("Defined web API token permissions:");
103 SdtdConsole.Instance.Output (" Level: Name / Secret");
104 foreach ((string _, AdminApiTokens.ApiToken apiToken) in AdminApiTokens.Instance.GetTokens ()) {
105 SdtdConsole.Instance.Output ($" {apiToken.PermissionLevel,5}: {apiToken.Name} / {apiToken.Secret}");
106 }
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.