From 49800a6d03d9bd632544a131162dcb6fa0d5a2b8 Mon Sep 17 00:00:00 2001 From: braginini Date: Wed, 25 Aug 2021 14:16:17 +0200 Subject: [PATCH] fix: minor HTTP bugs --- .gitignore | 3 ++- management/server/http/handler/peers.go | 1 + management/server/http/handler/setupkeys.go | 12 ++++++++++++ management/server/setupkey.go | 20 +++++++++++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 164675261..085048599 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.iml dist/ .env -conf.json \ No newline at end of file +conf.json +http-cmds.sh \ No newline at end of file diff --git a/management/server/http/handler/peers.go b/management/server/http/handler/peers.go index b16d6db2e..11b2b7c93 100644 --- a/management/server/http/handler/peers.go +++ b/management/server/http/handler/peers.go @@ -58,6 +58,7 @@ func (h *Peers) deletePeer(accountId string, peer *server.Peer, w http.ResponseW http.Redirect(w, r, "/", http.StatusInternalServerError) return } + writeJSONObject(w, "") } func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) { diff --git a/management/server/http/handler/setupkeys.go b/management/server/http/handler/setupkeys.go index bbe07e8cc..ba1a44c9f 100644 --- a/management/server/http/handler/setupkeys.go +++ b/management/server/http/handler/setupkeys.go @@ -27,6 +27,7 @@ type SetupKeyResponse struct { Revoked bool UsedTimes int LastUsed time.Time + State string } // SetupKeyRequest is a request sent by client. This object contains fields that can be modified @@ -183,6 +184,16 @@ func writeSuccess(w http.ResponseWriter, key *server.SetupKey) { } func toResponseBody(key *server.SetupKey) *SetupKeyResponse { + var state string + if key.IsExpired() { + state = "expired" + } else if key.IsRevoked() { + state = "revoked" + } else if key.IsOverUsed() { + state = "overused" + } else { + state = "valid" + } return &SetupKeyResponse{ Id: key.Id, Key: key.Key, @@ -193,5 +204,6 @@ func toResponseBody(key *server.SetupKey) *SetupKeyResponse { Revoked: key.Revoked, UsedTimes: key.UsedTimes, LastUsed: key.LastUsed, + State: state, } } diff --git a/management/server/setupkey.go b/management/server/setupkey.go index a17346a2d..1dad9fe43 100644 --- a/management/server/setupkey.go +++ b/management/server/setupkey.go @@ -50,6 +50,7 @@ func (key *SetupKey) Copy() *SetupKey { ExpiresAt: key.ExpiresAt, Revoked: key.Revoked, UsedTimes: key.UsedTimes, + LastUsed: key.LastUsed, } } @@ -63,9 +64,22 @@ func (key *SetupKey) IncrementUsage() *SetupKey { // IsValid is true if the key was not revoked, is not expired and used not more than it was supposed to func (key *SetupKey) IsValid() bool { - expired := time.Now().After(key.ExpiresAt) - overUsed := key.Type == SetupKeyOneOff && key.UsedTimes >= 1 - return !key.Revoked && !expired && !overUsed + return !key.IsRevoked() && !key.IsExpired() && !key.IsOverUsed() +} + +// IsRevoked if key was revoked +func (key *SetupKey) IsRevoked() bool { + return key.Revoked +} + +// IsExpired if key was expired +func (key *SetupKey) IsExpired() bool { + return time.Now().After(key.ExpiresAt) +} + +// IsOverUsed if key was used too many times +func (key *SetupKey) IsOverUsed() bool { + return key.Type == SetupKeyOneOff && key.UsedTimes >= 1 } // GenerateSetupKey generates a new setup key