Consider the prefix when testing token length

This commit is contained in:
eikendev 2022-02-13 11:55:43 +01:00
parent 500a8cd4b0
commit be99411b1b
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
2 changed files with 9 additions and 9 deletions

View file

@ -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:

View file

@ -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) {