mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-07 08:44:07 +01:00
remove dependency to external base62 package and create own methods in utils
This commit is contained in:
parent
a3ee45b79e
commit
2fef52b856
1
go.mod
1
go.mod
@ -28,7 +28,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
codeberg.org/ac/base62 v0.0.0-20210305150220-e793b546833a
|
|
||||||
fyne.io/fyne/v2 v2.1.4
|
fyne.io/fyne/v2 v2.1.4
|
||||||
github.com/c-robinson/iplib v1.0.3
|
github.com/c-robinson/iplib v1.0.3
|
||||||
github.com/coreos/go-iptables v0.6.0
|
github.com/coreos/go-iptables v0.6.0
|
||||||
|
2
go.sum
2
go.sum
@ -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.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.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
|
||||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
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=
|
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 h1:bt1+28++kAzRzPB0GM2EuSV4cnl8rXNX4cjfd8G06Rc=
|
||||||
fyne.io/fyne/v2 v2.1.4/go.mod h1:p+E/Dh+wPW8JwR2DVcsZ9iXgR9ZKde80+Y+40Is54AQ=
|
fyne.io/fyne/v2 v2.1.4/go.mod h1:p+E/Dh+wPW8JwR2DVcsZ9iXgR9ZKde80+Y+40Is54AQ=
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/ac/base62"
|
|
||||||
"github.com/eko/gocache/v3/cache"
|
"github.com/eko/gocache/v3/cache"
|
||||||
cacheStore "github.com/eko/gocache/v3/store"
|
cacheStore "github.com/eko/gocache/v3/store"
|
||||||
gocache "github.com/patrickmn/go-cache"
|
gocache "github.com/patrickmn/go-cache"
|
||||||
@ -28,6 +27,7 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
||||||
"github.com/netbirdio/netbird/management/server/status"
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -1174,7 +1174,7 @@ func (am *DefaultAccountManager) GetAccountFromPAT(token string) (*Account, *Use
|
|||||||
secret := token[len(PATPrefix) : len(PATPrefix)+PATSecretLength]
|
secret := token[len(PATPrefix) : len(PATPrefix)+PATSecretLength]
|
||||||
encodedChecksum := token[len(PATPrefix)+PATSecretLength : len(PATPrefix)+PATSecretLength+PATChecksumLength]
|
encodedChecksum := token[len(PATPrefix)+PATSecretLength : len(PATPrefix)+PATSecretLength+PATChecksumLength]
|
||||||
|
|
||||||
verificationChecksum, err := base62.Decode(encodedChecksum)
|
verificationChecksum, err := util.DecodeBase62(encodedChecksum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, fmt.Errorf("token checksum decoding failed: %w", err)
|
return nil, nil, nil, fmt.Errorf("token checksum decoding failed: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ import (
|
|||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/ac/base62"
|
|
||||||
b "github.com/hashicorp/go-secure-stdlib/base62"
|
b "github.com/hashicorp/go-secure-stdlib/base62"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -71,7 +72,7 @@ func generateNewToken() (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
checksum := crc32.ChecksumIEEE([]byte(secret))
|
checksum := crc32.ChecksumIEEE([]byte(secret))
|
||||||
encodedChecksum := base62.Encode(checksum)
|
encodedChecksum := util.EncodeBase62(checksum)
|
||||||
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
|
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
|
||||||
plainToken := PATPrefix + secret + paddedChecksum
|
plainToken := PATPrefix + secret + paddedChecksum
|
||||||
hashedToken := sha256.Sum256([]byte(plainToken))
|
hashedToken := sha256.Sum256([]byte(plainToken))
|
||||||
|
@ -4,11 +4,13 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
b64 "encoding/base64"
|
b64 "encoding/base64"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"codeberg.org/ac/base62"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPAT_GenerateToken_Hashing(t *testing.T) {
|
func TestPAT_GenerateToken_Hashing(t *testing.T) {
|
||||||
@ -33,8 +35,10 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) {
|
|||||||
secret := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
|
secret := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
|
||||||
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
|
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
|
||||||
|
|
||||||
|
var i big.Int
|
||||||
|
i.SetString(secret, 62)
|
||||||
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
|
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
|
||||||
actualChecksum, err := base62.Decode(tokenCheckSum)
|
actualChecksum, err := util.DecodeBase62(tokenCheckSum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
59
util/base62.go
Normal file
59
util/base62.go
Normal file
@ -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
util/base62_test.go
Normal file
31
util/base62_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user