mirror of
https://github.com/netbirdio/netbird.git
synced 2025-05-30 06:40:15 +02:00
codacy and lint hints
This commit is contained in:
parent
628a201e31
commit
b852198f67
@ -1127,8 +1127,8 @@ func (am *DefaultAccountManager) GetAccountFromPAT(token string) (*Account, *Use
|
||||
if prefix != PATPrefix {
|
||||
return nil, nil, fmt.Errorf("token invalid")
|
||||
}
|
||||
secret := token[len(PATPrefix) : len(PATPrefix)+PATsecretLength]
|
||||
encodedChecksum := token[len(PATPrefix)+PATsecretLength : len(PATPrefix)+PATsecretLength+PATChecksumLength]
|
||||
secret := token[len(PATPrefix) : len(PATPrefix)+PATSecretLength]
|
||||
encodedChecksum := token[len(PATPrefix)+PATSecretLength : len(PATPrefix)+PATSecretLength+PATChecksumLength]
|
||||
|
||||
verificationChecksum, err := base62.Decode(encodedChecksum)
|
||||
if err != nil {
|
||||
|
@ -488,7 +488,10 @@ func TestAccountManager_GetAccountFromPAT(t *testing.T) {
|
||||
AutoGroups: nil,
|
||||
PATs: []PersonalAccessToken{pat},
|
||||
}
|
||||
store.SaveAccount(account)
|
||||
err := store.SaveAccount(account)
|
||||
if err != nil {
|
||||
t.Fatalf("Error when saving account: %s", err)
|
||||
}
|
||||
|
||||
am := DefaultAccountManager{
|
||||
Store: store,
|
||||
|
@ -7,9 +7,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/netbirdio/netbird/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/netbirdio/netbird/util"
|
||||
)
|
||||
|
||||
type accounts struct {
|
||||
@ -384,6 +385,9 @@ func TestFileStore_GetTokenIDByHashedToken(t *testing.T) {
|
||||
|
||||
hashedToken := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].HashedToken
|
||||
tokenID, err := store.GetTokenIDByHashedToken(hashedToken)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedTokenID := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
||||
assert.Equal(t, expectedTokenID, tokenID)
|
||||
@ -433,8 +437,8 @@ func TestFileStore_GetUserByTokenID(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tokenId := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
||||
user, err := store.GetUserByTokenID(tokenId)
|
||||
tokenID := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
||||
user, err := store.GetUserByTokenID(tokenID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
16
management/server/http/pat_handler.go
Normal file
16
management/server/http/pat_handler.go
Normal file
@ -0,0 +1,16 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/netbirdio/netbird/management/server"
|
||||
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
||||
)
|
||||
|
||||
// PATHandler is the nameserver group handler of the account
|
||||
type PATHandler struct {
|
||||
accountManager server.AccountManager
|
||||
claimsExtractor *jwtclaims.ClaimsExtractor
|
||||
}
|
||||
|
||||
func NewPATsHandler(accountManager server.AccountManager, authCfg AuthCfg) {
|
||||
|
||||
}
|
@ -13,10 +13,13 @@ import (
|
||||
|
||||
const (
|
||||
// PATPrefix is the globally used, 4 char prefix for personal access tokens
|
||||
PATPrefix = "nbp_"
|
||||
PATsecretLength = 30
|
||||
PATLength = 40
|
||||
PATPrefix = "nbp_"
|
||||
// PATSecretLength number of characters used for the secret inside the token
|
||||
PATSecretLength = 30
|
||||
// PATChecksumLength number of characters used for the encoded checksum of the secret inside the token
|
||||
PATChecksumLength = 6
|
||||
// PATLength total number of characters used for the token
|
||||
PATLength = 40
|
||||
)
|
||||
|
||||
// PersonalAccessToken holds all information about a PAT including a hashed version of it for verification
|
||||
@ -51,7 +54,7 @@ func CreateNewPAT(description string, expirationInDays int, createdBy string) (*
|
||||
}
|
||||
|
||||
func generateNewToken() (string, string, error) {
|
||||
secret, err := b.Random(PATsecretLength)
|
||||
secret, err := b.Random(PATSecretLength)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
@ -206,11 +206,8 @@ func (am *DefaultAccountManager) AddPATToUser(accountID string, userID string, p
|
||||
|
||||
user.PATs = append(user.PATs, pat)
|
||||
|
||||
if err = am.Store.SaveAccount(account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
err = am.Store.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
// SaveUser saves updates a given user. If the user doesn't exit it will throw status.NotFound error.
|
||||
|
@ -20,7 +20,10 @@ func TestUser_AddPATToUser(t *testing.T) {
|
||||
Name: "peer name",
|
||||
Status: &PeerStatus{Connected: true, LastSeen: time.Now()},
|
||||
}
|
||||
store.SaveAccount(account)
|
||||
err := store.SaveAccount(account)
|
||||
if err != nil {
|
||||
t.Fatalf("Error when saving account: %s", err)
|
||||
}
|
||||
|
||||
am := DefaultAccountManager{
|
||||
Store: store,
|
||||
@ -39,7 +42,7 @@ func TestUser_AddPATToUser(t *testing.T) {
|
||||
|
||||
token := "someToken"
|
||||
pat := PersonalAccessToken{
|
||||
ID: "tokenId",
|
||||
ID: "tokenID",
|
||||
Description: "some Description",
|
||||
HashedToken: token,
|
||||
ExpirationDate: time.Time{},
|
||||
@ -48,20 +51,23 @@ func TestUser_AddPATToUser(t *testing.T) {
|
||||
LastUsed: time.Time{},
|
||||
}
|
||||
|
||||
am.AddPATToUser("account_id", "testuser", pat)
|
||||
err = am.AddPATToUser("account_id", "testuser", pat)
|
||||
if err != nil {
|
||||
t.Fatalf("Error when adding PAT to user: %s", err)
|
||||
}
|
||||
|
||||
fileStore := am.Store.(*FileStore)
|
||||
tokenId := fileStore.HashedPAT2TokenID[string(token[:])]
|
||||
tokenID := fileStore.HashedPAT2TokenID[token[:]]
|
||||
|
||||
if tokenId == "" {
|
||||
if tokenID == "" {
|
||||
t.Fatal("GetTokenIDByHashedToken failed after adding PAT")
|
||||
}
|
||||
|
||||
assert.Equal(t, "tokenId", tokenId)
|
||||
assert.Equal(t, "tokenID", tokenID)
|
||||
|
||||
userId := fileStore.TokenID2UserID[tokenId]
|
||||
if userId == "" {
|
||||
userID := fileStore.TokenID2UserID[tokenID]
|
||||
if userID == "" {
|
||||
t.Fatal("GetUserByTokenId failed after adding PAT")
|
||||
}
|
||||
assert.Equal(t, "testuser", userId)
|
||||
assert.Equal(t, "testuser", userID)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user