mirror of
https://github.com/netbirdio/netbird.git
synced 2025-04-10 10:48:55 +02:00
move into separate package
This commit is contained in:
parent
2fef52b856
commit
873abc43bf
@ -1,4 +1,4 @@
|
|||||||
package util
|
package base62
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -11,8 +11,8 @@ const (
|
|||||||
base = uint32(len(alphabet))
|
base = uint32(len(alphabet))
|
||||||
)
|
)
|
||||||
|
|
||||||
// EncodeBase62 encodes a uint32 value to a base62 string.
|
// Encode encodes a uint32 value to a base62 string.
|
||||||
func EncodeBase62(num uint32) string {
|
func Encode(num uint32) string {
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
return string(alphabet[0])
|
return string(alphabet[0])
|
||||||
}
|
}
|
||||||
@ -32,8 +32,8 @@ func EncodeBase62(num uint32) string {
|
|||||||
return reversed
|
return reversed
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBase62 decodes a base62 string to a uint32 value.
|
// Decode decodes a base62 string to a uint32 value.
|
||||||
func DecodeBase62(encoded string) (uint32, error) {
|
func Decode(encoded string) (uint32, error) {
|
||||||
var decoded uint32
|
var decoded uint32
|
||||||
strLen := len(encoded)
|
strLen := len(encoded)
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package util
|
package base62
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -17,8 +17,8 @@ func TestEncodeDecode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
encoded := EncodeBase62(tt.num)
|
encoded := Encode(tt.num)
|
||||||
decoded, err := DecodeBase62(encoded)
|
decoded, err := Decode(encoded)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Decode error: %v", err)
|
t.Errorf("Decode error: %v", err)
|
@ -21,13 +21,13 @@ import (
|
|||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/base62"
|
||||||
nbdns "github.com/netbirdio/netbird/dns"
|
nbdns "github.com/netbirdio/netbird/dns"
|
||||||
"github.com/netbirdio/netbird/management/server/activity"
|
"github.com/netbirdio/netbird/management/server/activity"
|
||||||
"github.com/netbirdio/netbird/management/server/idp"
|
"github.com/netbirdio/netbird/management/server/idp"
|
||||||
"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 := util.DecodeBase62(encodedChecksum)
|
verificationChecksum, err := base62.Decode(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)
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
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"
|
"github.com/netbirdio/netbird/base62"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -72,7 +72,7 @@ func generateNewToken() (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
checksum := crc32.ChecksumIEEE([]byte(secret))
|
checksum := crc32.ChecksumIEEE([]byte(secret))
|
||||||
encodedChecksum := util.EncodeBase62(checksum)
|
encodedChecksum := base62.Encode(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))
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/util"
|
"github.com/netbirdio/netbird/base62"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPAT_GenerateToken_Hashing(t *testing.T) {
|
func TestPAT_GenerateToken_Hashing(t *testing.T) {
|
||||||
@ -38,7 +38,7 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) {
|
|||||||
var i big.Int
|
var i big.Int
|
||||||
i.SetString(secret, 62)
|
i.SetString(secret, 62)
|
||||||
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
|
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
|
||||||
actualChecksum, err := util.DecodeBase62(tokenCheckSum)
|
actualChecksum, err := base62.Decode(tokenCheckSum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user