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

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

Refactored console commands for A21 caching

File size: 3.6 KB
RevLine 
[391]1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using JetBrains.Annotations;
[404]4using Webserver.Permissions;
[391]5
6namespace Webserver.Commands {
7 [UsedImplicitly]
8 public class WebTokens : ConsoleCmdAbstract {
9 private static readonly Regex validNameTokenMatcher = new Regex (@"^\w+$");
10
[405]11 protected override string[] getCommands () {
[391]12 return new[] {"webtokens"};
13 }
14
[405]15 protected override string getDescription () {
[391]16 return "Manage web tokens";
17 }
18
[405]19 protected override string getHelp () {
[391]20 return "Set/get webtoken permission levels. A level of 0 is maximum permission.\n" +
21 "Usage:\n" +
[404]22 " webtokens add <tokenname> <tokensecret> <level>\n" +
23 " webtokens remove <tokenname>\n" +
[391]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 {
[402]36 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
[391]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) {
[402]45 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}.");
[391]46 return;
47 }
48
49 if (string.IsNullOrEmpty (_params [1])) {
[404]50 SdtdConsole.Instance.Output ("Argument 'tokenname' is empty.");
[391]51 return;
52 }
53
54 if (!validNameTokenMatcher.IsMatch (_params [1])) {
55 SdtdConsole.Instance.Output (
[404]56 "Argument 'tokenname' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
[391]57 return;
58 }
59
60 if (string.IsNullOrEmpty (_params [2])) {
[404]61 SdtdConsole.Instance.Output ("Argument 'tokensecret' is empty.");
[391]62 return;
63 }
64
65 if (!validNameTokenMatcher.IsMatch (_params [2])) {
66 SdtdConsole.Instance.Output (
[404]67 "Argument 'tokensecret' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
[391]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
[404]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}.");
[391]78 }
79
80 private void ExecuteRemove (List<string> _params) {
81 if (_params.Count != 2) {
[402]82 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
[391]83 return;
84 }
85
86 if (string.IsNullOrEmpty (_params [1])) {
[404]87 SdtdConsole.Instance.Output ("Argument 'tokenname' is empty.");
[391]88 return;
89 }
90
91 if (!validNameTokenMatcher.IsMatch (_params [1])) {
92 SdtdConsole.Instance.Output (
[404]93 "Argument 'tokenname' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
[391]94 return;
95 }
96
[404]97 AdminApiTokens.Instance.RemoveToken (_params [1]);
98 SdtdConsole.Instance.Output ($"{_params [1]} removed from web API token permissions list.");
[391]99 }
100
101 private void ExecuteList () {
[404]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}");
[391]106 }
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.