-Added REST endpoint "/v2/token/destroy/all"
-Added command "/rest destroytokens", "/rest listusers"
This commit is contained in:
parent
d9ff9a6ac5
commit
4d95b5594e
3 changed files with 66 additions and 2 deletions
|
|
@ -246,6 +246,7 @@ namespace TShockAPI
|
||||||
add(Permissions.xmas, ForceXmas, "forcexmas");
|
add(Permissions.xmas, ForceXmas, "forcexmas");
|
||||||
add(Permissions.settempgroup, TempGroup, "tempgroup");
|
add(Permissions.settempgroup, TempGroup, "tempgroup");
|
||||||
add(null, Aliases, "aliases");
|
add(null, Aliases, "aliases");
|
||||||
|
add(Rests.RestPermissions.restmanage, ManageRest, "rest");
|
||||||
//add(null, TestCallbackCommand, "test");
|
//add(null, TestCallbackCommand, "test");
|
||||||
|
|
||||||
TShockCommands = new ReadOnlyCollection<Command>(tshockCommands);
|
TShockCommands = new ReadOnlyCollection<Command>(tshockCommands);
|
||||||
|
|
@ -1345,6 +1346,57 @@ namespace TShockAPI
|
||||||
new PluginUpdaterThread(args.Player);
|
new PluginUpdaterThread(args.Player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ManageRest(CommandArgs args)
|
||||||
|
{
|
||||||
|
string subCommand = "help";
|
||||||
|
if (args.Parameters.Count > 0)
|
||||||
|
subCommand = args.Parameters[0];
|
||||||
|
|
||||||
|
switch(subCommand.ToLower())
|
||||||
|
{
|
||||||
|
case "listusers":
|
||||||
|
{
|
||||||
|
int pageNumber;
|
||||||
|
if (!PaginationTools.TryParsePageNumber(args.Parameters, 1, args.Player, out pageNumber))
|
||||||
|
return;
|
||||||
|
|
||||||
|
Dictionary<string,int> restUsersTokens = new Dictionary<string,int>();
|
||||||
|
foreach (Rests.SecureRest.TokenData tokenData in TShock.RestApi.Tokens.Values)
|
||||||
|
{
|
||||||
|
if (restUsersTokens.ContainsKey(tokenData.Username))
|
||||||
|
restUsersTokens[tokenData.Username]++;
|
||||||
|
else
|
||||||
|
restUsersTokens.Add(tokenData.Username, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<string> restUsers = new List<string>(
|
||||||
|
restUsersTokens.Select(ut => string.Format("{0} ({1} tokens)", ut.Key, ut.Value)));
|
||||||
|
|
||||||
|
PaginationTools.SendPage(
|
||||||
|
args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(restUsers), new PaginationTools.Settings {
|
||||||
|
NothingToDisplayString = "There are currently no active REST users.",
|
||||||
|
HeaderFormat = "Active REST Users ({0}/{1}):",
|
||||||
|
FooterFormat = "Type /rest listusers {0} for more."
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "destroytokens":
|
||||||
|
{
|
||||||
|
TShock.RestApi.Tokens.Clear();
|
||||||
|
args.Player.SendSuccessMessage("All REST tokens have been destroyed.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
args.Player.SendInfoMessage("Available REST Sub-Commands:");
|
||||||
|
args.Player.SendMessage("destroytokens - Destroys all current REST tokens.", Color.White);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion Server Maintenence Commands
|
#endregion Server Maintenence Commands
|
||||||
|
|
||||||
#region Cause Events and Spawn Monsters Commands
|
#region Cause Events and Spawn Monsters Commands
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,9 @@ namespace Rests
|
||||||
[Description("User can create REST tokens.")]
|
[Description("User can create REST tokens.")]
|
||||||
public static readonly string restapi;
|
public static readonly string restapi;
|
||||||
|
|
||||||
|
[Description("User or REST user can destroy all REST tokens.")]
|
||||||
|
public static readonly string restmanage;
|
||||||
|
|
||||||
|
|
||||||
[Description("REST user can turn off / restart the server.")]
|
[Description("REST user can turn off / restart the server.")]
|
||||||
public static readonly string restmaintenance;
|
public static readonly string restmaintenance;
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ namespace Rests
|
||||||
Register(new RestCommand("/token/create/{username}/{password}", NewToken) { DoLog = false });
|
Register(new RestCommand("/token/create/{username}/{password}", NewToken) { DoLog = false });
|
||||||
Register(new RestCommand("/v2/token/create/{password}", NewTokenV2) { DoLog = false });
|
Register(new RestCommand("/v2/token/create/{password}", NewTokenV2) { DoLog = false });
|
||||||
Register(new SecureRestCommand("/token/destroy/{token}", DestroyToken));
|
Register(new SecureRestCommand("/token/destroy/{token}", DestroyToken));
|
||||||
|
Register(new SecureRestCommand("/v2/token/destroy/all", DestroyAllTokens, RestPermissions.restmanage));
|
||||||
|
|
||||||
foreach (KeyValuePair<string, TokenData> t in TShockAPI.TShock.RESTStartupTokens)
|
foreach (KeyValuePair<string, TokenData> t in TShockAPI.TShock.RESTStartupTokens)
|
||||||
{
|
{
|
||||||
|
|
@ -69,6 +70,14 @@ namespace Rests
|
||||||
{{"status", "200"}, {"response", "Requested token was successfully destroyed."}};
|
{{"status", "200"}, {"response", "Requested token was successfully destroyed."}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private object DestroyAllTokens(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData)
|
||||||
|
{
|
||||||
|
Tokens.Clear();
|
||||||
|
|
||||||
|
return new Dictionary<string, string>
|
||||||
|
{{"status", "200"}, {"response", "All tokens were successfully destroyed."}};
|
||||||
|
}
|
||||||
|
|
||||||
private object NewTokenV2(RestVerbs verbs, IParameterCollection parameters)
|
private object NewTokenV2(RestVerbs verbs, IParameterCollection parameters)
|
||||||
{
|
{
|
||||||
var user = parameters["username"];
|
var user = parameters["username"];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue