mirror of
https://github.com/pushbits/server.git
synced 2025-05-18 11:26:36 +02:00
Add tests for token generation
This commit is contained in:
parent
8b90541a8f
commit
9c22816495
2 changed files with 56 additions and 19 deletions
|
@ -2,15 +2,14 @@ package authentication
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"log"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tokenCharacters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
tokenCharacters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||||
standardTokenLength = 64 // This length includes the prefix (one character).
|
regularTokenLength = 64 // This length includes the prefix (one character).
|
||||||
compatTokenLength = 15 // This length includes the prefix (one character).
|
compatTokenLength = 15 // This length includes the prefix (one character).
|
||||||
applicationPrefix = "A"
|
applicationTokenPrefix = "A"
|
||||||
)
|
)
|
||||||
|
|
||||||
func randIntn(n int) int {
|
func randIntn(n int) int {
|
||||||
|
@ -46,23 +45,13 @@ func generateRandomString(length int) string {
|
||||||
return string(res)
|
return string(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateRandomToken(prefix string, compat bool) string {
|
// GenerateApplicationToken generates a token for an application.
|
||||||
tokenLength := standardTokenLength
|
func GenerateApplicationToken(compat bool) string {
|
||||||
|
tokenLength := regularTokenLength
|
||||||
|
|
||||||
if compat {
|
if compat {
|
||||||
tokenLength = compatTokenLength
|
tokenLength = compatTokenLength
|
||||||
}
|
}
|
||||||
|
|
||||||
// Although constant at the time of writing, this check should prevent future changes from generating insecure tokens.
|
return applicationTokenPrefix + generateRandomString(tokenLength)
|
||||||
randomLength := tokenLength - len(prefix)
|
|
||||||
if randomLength < 14 {
|
|
||||||
log.Fatalf("Tokens should have more than %d random characters", randomLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
return prefix + generateRandomString(randomLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateApplicationToken generates a token for an application.
|
|
||||||
func GenerateApplicationToken(compat bool) string {
|
|
||||||
return generateRandomToken(applicationPrefix, compat)
|
|
||||||
}
|
}
|
||||||
|
|
48
internal/authentication/token_test.go
Normal file
48
internal/authentication/token_test.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
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):]
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
if compat {
|
||||||
|
assert.Equal(len(token), compatTokenLength, "Unexpected compatibility token length")
|
||||||
|
} else {
|
||||||
|
assert.Equal(len(token), regularTokenLength, "Unexpected regular token length")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(prefix, applicationTokenPrefix, "Invalid token prefix")
|
||||||
|
|
||||||
|
for _, c := range []byte(token) {
|
||||||
|
assert.Contains(tokenCharacters, c, "Unexpected character in token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthentication_GenerateApplicationToken(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
for i := 0; i < 64; i++ {
|
||||||
|
token := GenerateApplicationToken(false)
|
||||||
|
|
||||||
|
isGoodToken(assert, require, token, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 64; i++ {
|
||||||
|
token := GenerateApplicationToken(true)
|
||||||
|
|
||||||
|
isGoodToken(assert, require, token, true)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue