netbird/management/server/personal_access_token_test.go

41 lines
1.0 KiB
Go
Raw Normal View History

package server
import (
"crypto/sha256"
"hash/crc32"
"strings"
"testing"
"codeberg.org/ac/base62"
"github.com/stretchr/testify/assert"
)
func TestPAT_GenerateToken_Hashing(t *testing.T) {
2023-03-08 11:36:03 +01:00
hashedToken, plainToken, _ := generateNewToken()
2023-03-08 11:30:09 +01:00
expectedToken := sha256.Sum256([]byte(plainToken))
assert.Equal(t, hashedToken, string(expectedToken[:]))
}
func TestPAT_GenerateToken_Prefix(t *testing.T) {
2023-03-08 11:36:03 +01:00
_, plainToken, _ := generateNewToken()
2023-03-06 13:49:07 +01:00
fourCharPrefix := plainToken[:4]
2023-03-08 11:54:10 +01:00
assert.Equal(t, PATPrefix, fourCharPrefix)
}
func TestPAT_GenerateToken_Checksum(t *testing.T) {
2023-03-08 11:36:03 +01:00
_, plainToken, _ := generateNewToken()
tokenWithoutPrefix := strings.Split(plainToken, "_")[1]
if len(tokenWithoutPrefix) != 36 {
t.Fatal("Token has wrong length")
}
2023-03-06 13:49:07 +01:00
secret := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
2023-03-06 13:49:07 +01:00
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
actualChecksum, err := base62.Decode(tokenCheckSum)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expectedChecksum, actualChecksum)
}