mirror of
https://github.com/pushbits/server.git
synced 2025-05-30 17:26:34 +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 (
|
||||
"crypto/rand"
|
||||
"log"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var (
|
||||
tokenCharacters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
standardTokenLength = 64 // This length includes the prefix (one character).
|
||||
compatTokenLength = 15 // This length includes the prefix (one character).
|
||||
applicationPrefix = "A"
|
||||
tokenCharacters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
regularTokenLength = 64 // This length includes the prefix (one character).
|
||||
compatTokenLength = 15 // This length includes the prefix (one character).
|
||||
applicationTokenPrefix = "A"
|
||||
)
|
||||
|
||||
func randIntn(n int) int {
|
||||
|
@ -46,23 +45,13 @@ func generateRandomString(length int) string {
|
|||
return string(res)
|
||||
}
|
||||
|
||||
func generateRandomToken(prefix string, compat bool) string {
|
||||
tokenLength := standardTokenLength
|
||||
// GenerateApplicationToken generates a token for an application.
|
||||
func GenerateApplicationToken(compat bool) string {
|
||||
tokenLength := regularTokenLength
|
||||
|
||||
if compat {
|
||||
tokenLength = compatTokenLength
|
||||
}
|
||||
|
||||
// Although constant at the time of writing, this check should prevent future changes from generating insecure tokens.
|
||||
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)
|
||||
return applicationTokenPrefix + generateRandomString(tokenLength)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue