Browse Source

Add option to prevent multiple tokens per player

master
NGnius 4 years ago
parent
commit
5ce120c319
2 changed files with 12 additions and 3 deletions
  1. +1
    -0
      config.go
  2. +11
    -3
      handlers.go

+ 1
- 0
config.go View File

@@ -28,6 +28,7 @@ func initArgs() {
flag.BoolVar(&randomizeTokens, "random-tokens", false, "Generate tokens with some random bytes")
flag.StringVar(&corsHeader, "cors-header", defaultCorsHeader, "Access-Control-Allow-Origin HTTP request header")
flag.StringVar(&password, "entry-pwd", defaultPassword, "Password provided in POST JSON to authenticate")
flag.BoolVar(&reuseTokens, "reuse-tokens", false, "Generate a single token for every player")
}

func parseArgs() {


+ 11
- 3
handlers.go View File

@@ -18,8 +18,9 @@ const (
)

var (
corsHeader string
password string
corsHeader string
password string
reuseTokens bool
)

func boardHandler(w http.ResponseWriter, r *http.Request) {
@@ -214,7 +215,14 @@ func newKeyHandler(w http.ResponseWriter, r *http.Request) {
errorResponse(400, "Unable to convert request to JSON: "+jsonErr.Error(), w, r)
return
}
key, sqlErr := newKeySql(newKey.PlayerID)
var key *Key
var sqlErr error
if reuseTokens {
key, sqlErr = keyByPlayer(newKey.PlayerID)
}
if (sqlErr != nil && reuseTokens) || !reuseTokens {
key, sqlErr = newKeySql(newKey.PlayerID)
}
if sqlErr != nil {
fmt.Println(sqlErr)
errorResponse(500, "Key could not be created: "+sqlErr.Error(), w, r)