From be99411b1bafdc67a4d8df4158e3861f185e6002 Mon Sep 17 00:00:00 2001 From: eikendev Date: Sun, 13 Feb 2022 11:55:43 +0100 Subject: [PATCH] Consider the prefix when testing token length --- Makefile | 6 ++---- internal/authentication/token_test.go | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 4567096..e610231 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,12 @@ build: .PHONY: test test: - stdout=$$(gofmt -l . 2>&1); \ - if [ "$$stdout" ]; then \ - exit 1; \ - fi + stdout=$$(gofmt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi go vet ./... gocyclo -over 10 $(shell find . -iname '*.go' -type f) staticcheck ./... go test -v -cover ./... + @printf '\n%s\n' "> Test successful" .PHONY: setup setup: diff --git a/internal/authentication/token_test.go b/internal/authentication/token_test.go index 4e354d1..2993e09 100644 --- a/internal/authentication/token_test.go +++ b/internal/authentication/token_test.go @@ -8,20 +8,22 @@ import ( ) const ( - minTokenLength = 14 + minRandomChars = 14 ) func isGoodToken(assert *assert.Assertions, require *require.Assertions, token string, compat bool) { + tokenLength := len(token) + if compat { - assert.Equal(len(token), compatTokenLength, "Unexpected compatibility token length") + assert.Equal(tokenLength, compatTokenLength, "Unexpected compatibility token length") } else { - assert.Equal(len(token), regularTokenLength, "Unexpected regular token length") + assert.Equal(tokenLength, regularTokenLength, "Unexpected regular token length") } - assert.GreaterOrEqual(len(token), minTokenLength, "Token is too short to give sufficient entropy") + randomChars := tokenLength - len(applicationTokenPrefix) + assert.GreaterOrEqual(randomChars, minRandomChars, "Token is too short to give sufficient entropy") prefix := token[0:len(applicationTokenPrefix)] - assert.Equal(prefix, applicationTokenPrefix, "Invalid token prefix") for _, c := range []byte(token) {