remove dependency to external base62 package and create own methods in utils

This commit is contained in:
Pascal Fischer
2023-05-16 12:44:26 +02:00
parent a3ee45b79e
commit 2fef52b856
7 changed files with 101 additions and 9 deletions
+2 -2
View File
@@ -15,7 +15,6 @@ import (
"sync"
"time"
"codeberg.org/ac/base62"
"github.com/eko/gocache/v3/cache"
cacheStore "github.com/eko/gocache/v3/store"
gocache "github.com/patrickmn/go-cache"
@@ -28,6 +27,7 @@ import (
"github.com/netbirdio/netbird/management/server/jwtclaims"
"github.com/netbirdio/netbird/management/server/status"
"github.com/netbirdio/netbird/route"
"github.com/netbirdio/netbird/util"
)
const (
@@ -1174,7 +1174,7 @@ func (am *DefaultAccountManager) GetAccountFromPAT(token string) (*Account, *Use
secret := token[len(PATPrefix) : len(PATPrefix)+PATSecretLength]
encodedChecksum := token[len(PATPrefix)+PATSecretLength : len(PATPrefix)+PATSecretLength+PATChecksumLength]
verificationChecksum, err := base62.Decode(encodedChecksum)
verificationChecksum, err := util.DecodeBase62(encodedChecksum)
if err != nil {
return nil, nil, nil, fmt.Errorf("token checksum decoding failed: %w", err)
}
+3 -2
View File
@@ -7,9 +7,10 @@ import (
"hash/crc32"
"time"
"codeberg.org/ac/base62"
b "github.com/hashicorp/go-secure-stdlib/base62"
"github.com/rs/xid"
"github.com/netbirdio/netbird/util"
)
const (
@@ -71,7 +72,7 @@ func generateNewToken() (string, string, error) {
}
checksum := crc32.ChecksumIEEE([]byte(secret))
encodedChecksum := base62.Encode(checksum)
encodedChecksum := util.EncodeBase62(checksum)
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
plainToken := PATPrefix + secret + paddedChecksum
hashedToken := sha256.Sum256([]byte(plainToken))
@@ -4,11 +4,13 @@ import (
"crypto/sha256"
b64 "encoding/base64"
"hash/crc32"
"math/big"
"strings"
"testing"
"codeberg.org/ac/base62"
"github.com/stretchr/testify/assert"
"github.com/netbirdio/netbird/util"
)
func TestPAT_GenerateToken_Hashing(t *testing.T) {
@@ -33,8 +35,10 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) {
secret := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
var i big.Int
i.SetString(secret, 62)
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
actualChecksum, err := base62.Decode(tokenCheckSum)
actualChecksum, err := util.DecodeBase62(tokenCheckSum)
if err != nil {
t.Fatal(err)
}