Added endpoint /token/destroy/{token}
This commit is contained in:
parent
17683eaeaa
commit
5192c07d6d
1 changed files with 21 additions and 4 deletions
|
|
@ -17,12 +17,31 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
Tokens = new Dictionary<string, object>();
|
Tokens = new Dictionary<string, object>();
|
||||||
Register(new RestCommand("/token/new/{username}/{password}", newtoken) { RequiesToken = false });
|
Register(new RestCommand("/token/new/{username}/{password}", newtoken) { RequiesToken = false });
|
||||||
|
Register(new RestCommand("/token/destroy/{token}", DestroyToken) {RequiesToken = true});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object DestroyToken(RestVerbs verbs, IParameterCollection parameters)
|
||||||
|
{
|
||||||
|
var token = verbs["token"];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Tokens.Remove(token);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified token queued for destruction failed to be deleted." } };
|
||||||
|
}
|
||||||
|
return new Dictionary<string, string> { { "status", "200" }, { "response", "Requested token was successfully destroyed." } };
|
||||||
|
}
|
||||||
|
|
||||||
object newtoken(RestVerbs verbs, IParameterCollection parameters)
|
object newtoken(RestVerbs verbs, IParameterCollection parameters)
|
||||||
{
|
{
|
||||||
var user = verbs["username"];
|
var user = verbs["username"];
|
||||||
var pass = verbs["password"];
|
var pass = verbs["password"];
|
||||||
|
|
||||||
|
if (Verify != null && !Verify(user, pass))
|
||||||
|
return new Dictionary<string, string> { { "status", "401" } , { "error", "Invalid username/password combination provided. Please re-submit your query with a correct pair." } };
|
||||||
|
|
||||||
var userAccount = TShock.Users.GetUserByName(user);
|
var userAccount = TShock.Users.GetUserByName(user);
|
||||||
if (userAccount == null)
|
if (userAccount == null)
|
||||||
{
|
{
|
||||||
|
|
@ -36,12 +55,9 @@ namespace TShockAPI
|
||||||
|
|
||||||
if (!Tools.GetGroup(userAccount.Group).HasPermission("api") && userAccount.Group != "superadmin")
|
if (!Tools.GetGroup(userAccount.Group).HasPermission("api") && userAccount.Group != "superadmin")
|
||||||
{
|
{
|
||||||
return new Dictionary<string, string> { { "status", "403" }, { "error", "Although your account was successfully found and identified, your account lacks the permission required to use the API. (api)"} };
|
return new Dictionary<string, string> { { "status", "403" }, { "error", "Although your account was successfully found and identified, your account lacks the permission required to use the API. (api)" } };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Verify != null && !Verify(user, pass))
|
|
||||||
return new Dictionary<string, string> { { "status", "401" } , { "error", "Invalid username/password combination provided. Please re-submit your query with a correct pair." } };
|
|
||||||
|
|
||||||
string hash = string.Empty;
|
string hash = string.Empty;
|
||||||
var rand = new Random();
|
var rand = new Random();
|
||||||
var randbytes = new byte[20];
|
var randbytes = new byte[20];
|
||||||
|
|
@ -55,6 +71,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
return new Dictionary<string, string> { { "status", "200" } , { "token", hash } }; ;
|
return new Dictionary<string, string> { { "status", "200" } , { "token", hash } }; ;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms)
|
protected override object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms)
|
||||||
{
|
{
|
||||||
if (cmd.RequiesToken)
|
if (cmd.RequiesToken)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue