throttle requests on failed token

This commit is contained in:
Roland Osborne 2022-09-10 02:20:32 -07:00
parent 0f00fc1c4a
commit 22e58c9c0e
4 changed files with 7 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"databag/internal/store"
"encoding/hex"
"errors"
"time"
"gorm.io/gorm"
"net/http"
"os"
@ -19,6 +20,7 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
if r.FormValue("token") != "" {
token, _, res = AccessToken(r)
if res != nil || token.TokenType != APPTokenCreate {
time.Sleep(APPUsernameWait * time.Millisecond);
ErrResponse(w, http.StatusUnauthorized, res)
return
}

View File

@ -15,11 +15,10 @@ type accountUsername struct {
//GetAccountUsername determines if username is assignable
func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
time.Sleep(APPUsernameWait * time.Millisecond);
if r.FormValue("token") != "" {
token, _, res := AccessToken(r)
if res != nil || token.TokenType != APPTokenCreate {
time.Sleep(APPUsernameWait * time.Second);
ErrResponse(w, http.StatusUnauthorized, res)
return
}

View File

@ -4,6 +4,7 @@ import (
"databag/internal/store"
"encoding/hex"
"errors"
"time"
"github.com/theckman/go-securerandom"
"gorm.io/gorm"
"net/http"
@ -14,6 +15,7 @@ func SetAccountAccess(w http.ResponseWriter, r *http.Request) {
token, _, res := AccessToken(r)
if res != nil || token.TokenType != APPTokenReset {
time.Sleep(APPUsernameWait * time.Millisecond);
ErrResponse(w, http.StatusUnauthorized, res)
return
}

View File

@ -96,8 +96,8 @@ const APPTokenReset = "reset"
//APPNotifyBuffer config for size of channel reciving notifications
const APPNotifyBuffer = 4096
//APPUsernameWait milliseconds to delay response
const APPUsernameWait = 333
//APPUsernameWait seconds to delay response
const APPUsernameWait = 1
//APPTopicUnconfirmed config for status name for unconfirmed
const APPTopicUnconfirmed = "unconfirmed"