Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token lengths #39

Merged
merged 3 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions internal/authentication/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ func GenerateApplicationToken(compat bool) string {
tokenLength = compatTokenLength
}

tokenLength -= len(applicationTokenPrefix)

return applicationTokenPrefix + generateRandomString(tokenLength)
}
21 changes: 11 additions & 10 deletions internal/authentication/token_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package authentication

import (
"log"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func isGoodToken(assert *assert.Assertions, require *require.Assertions, token string, compat bool) {
prefix := token[0:len(applicationTokenPrefix)]
token = token[len(applicationTokenPrefix):]
const (
minRandomChars = 14
)

// Although constant at the time of writing, this check should prevent future changes from generating insecure tokens.
if len(token) < 14 {
log.Fatalf("Tokens should have more random characters")
}
CubicrootXYZ marked this conversation as resolved.
Show resolved Hide resolved
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")
}

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) {
Expand Down