Skip to content

Commit

Permalink
remove dependency to external base62 package and create own methods i…
Browse files Browse the repository at this point in the history
…n utils
  • Loading branch information
pascal-fischer committed May 16, 2023
1 parent a3ee45b commit 2fef52b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
)

require (
codeberg.org/ac/base62 v0.0.0-20210305150220-e793b546833a
fyne.io/fyne/v2 v2.1.4
github.com/c-robinson/iplib v1.0.3
github.com/coreos/go-iptables v0.6.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
codeberg.org/ac/base62 v0.0.0-20210305150220-e793b546833a h1:U6cY/g6VSiy59vuvnBU6J/eSir0qVg4BeTnCDLaX+20=
codeberg.org/ac/base62 v0.0.0-20210305150220-e793b546833a/go.mod h1:ykEpkLT4JtH3I4Rb4gwkDsNLfgUg803qRDeIX88t3e8=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
fyne.io/fyne/v2 v2.1.4 h1:bt1+28++kAzRzPB0GM2EuSV4cnl8rXNX4cjfd8G06Rc=
fyne.io/fyne/v2 v2.1.4/go.mod h1:p+E/Dh+wPW8JwR2DVcsZ9iXgR9ZKde80+Y+40Is54AQ=
Expand Down
4 changes: 2 additions & 2 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions management/server/personal_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 6 additions & 2 deletions management/server/personal_access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
59 changes: 59 additions & 0 deletions util/base62.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package util

import (
"fmt"
"math"
"strings"
)

const (
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
base = uint32(len(alphabet))
)

// EncodeBase62 encodes a uint32 value to a base62 string.
func EncodeBase62(num uint32) string {
if num == 0 {
return string(alphabet[0])
}

var encoded strings.Builder
remainder := uint32(0)

for num > 0 {
remainder = num % base
encoded.WriteByte(alphabet[remainder])
num /= base
}

// Reverse the encoded string
encodedString := encoded.String()
reversed := reverse(encodedString)
return reversed
}

// DecodeBase62 decodes a base62 string to a uint32 value.
func DecodeBase62(encoded string) (uint32, error) {
var decoded uint32
strLen := len(encoded)

for i, char := range encoded {
index := strings.IndexRune(alphabet, char)
if index < 0 {
return 0, fmt.Errorf("invalid character: %c", char)
}

decoded += uint32(index) * uint32(math.Pow(float64(base), float64(strLen-i-1)))
}

return decoded, nil
}

// Reverse a string.
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
31 changes: 31 additions & 0 deletions util/base62_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"testing"
)

func TestEncodeDecode(t *testing.T) {
tests := []struct {
num uint32
}{
{0},
{1},
{42},
{12345},
{99999},
{123456789},
}

for _, tt := range tests {
encoded := EncodeBase62(tt.num)
decoded, err := DecodeBase62(encoded)

if err != nil {
t.Errorf("Decode error: %v", err)
}

if decoded != tt.num {
t.Errorf("Decode(%v) = %v, want %v", encoded, decoded, tt.num)
}
}
}

0 comments on commit 2fef52b

Please sign in to comment.