fix: minor HTTP bugs

This commit is contained in:
braginini 2021-08-25 14:16:17 +02:00
parent 0fa15e6920
commit 49800a6d03
4 changed files with 32 additions and 4 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
dist/ dist/
.env .env
conf.json conf.json
http-cmds.sh

View File

@ -58,6 +58,7 @@ func (h *Peers) deletePeer(accountId string, peer *server.Peer, w http.ResponseW
http.Redirect(w, r, "/", http.StatusInternalServerError) http.Redirect(w, r, "/", http.StatusInternalServerError)
return return
} }
writeJSONObject(w, "")
} }
func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) { func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {

View File

@ -27,6 +27,7 @@ type SetupKeyResponse struct {
Revoked bool Revoked bool
UsedTimes int UsedTimes int
LastUsed time.Time LastUsed time.Time
State string
} }
// SetupKeyRequest is a request sent by client. This object contains fields that can be modified // 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 { 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{ return &SetupKeyResponse{
Id: key.Id, Id: key.Id,
Key: key.Key, Key: key.Key,
@ -193,5 +204,6 @@ func toResponseBody(key *server.SetupKey) *SetupKeyResponse {
Revoked: key.Revoked, Revoked: key.Revoked,
UsedTimes: key.UsedTimes, UsedTimes: key.UsedTimes,
LastUsed: key.LastUsed, LastUsed: key.LastUsed,
State: state,
} }
} }

View File

@ -50,6 +50,7 @@ func (key *SetupKey) Copy() *SetupKey {
ExpiresAt: key.ExpiresAt, ExpiresAt: key.ExpiresAt,
Revoked: key.Revoked, Revoked: key.Revoked,
UsedTimes: key.UsedTimes, 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 // 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 { func (key *SetupKey) IsValid() bool {
expired := time.Now().After(key.ExpiresAt) return !key.IsRevoked() && !key.IsExpired() && !key.IsOverUsed()
overUsed := key.Type == SetupKeyOneOff && key.UsedTimes >= 1 }
return !key.Revoked && !expired && !overUsed
// 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 // GenerateSetupKey generates a new setup key