mirror of
https://github.com/netbirdio/netbird.git
synced 2025-05-31 07:07:42 +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 {
|
if prefix != PATPrefix {
|
||||||
return nil, nil, fmt.Errorf("token invalid")
|
return nil, nil, fmt.Errorf("token invalid")
|
||||||
}
|
}
|
||||||
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 := base62.Decode(encodedChecksum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -488,7 +488,10 @@ func TestAccountManager_GetAccountFromPAT(t *testing.T) {
|
|||||||
AutoGroups: nil,
|
AutoGroups: nil,
|
||||||
PATs: []PersonalAccessToken{pat},
|
PATs: []PersonalAccessToken{pat},
|
||||||
}
|
}
|
||||||
store.SaveAccount(account)
|
err := store.SaveAccount(account)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error when saving account: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
am := DefaultAccountManager{
|
am := DefaultAccountManager{
|
||||||
Store: store,
|
Store: store,
|
||||||
|
@ -7,9 +7,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/util"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type accounts struct {
|
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
|
hashedToken := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].HashedToken
|
||||||
tokenID, err := store.GetTokenIDByHashedToken(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
|
expectedTokenID := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
||||||
assert.Equal(t, expectedTokenID, tokenID)
|
assert.Equal(t, expectedTokenID, tokenID)
|
||||||
@ -433,8 +437,8 @@ func TestFileStore_GetUserByTokenID(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenId := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
tokenID := accounts.Accounts["bf1c8084-ba50-4ce7-9439-34653001fc3b"].Users["f4f6d672-63fb-11ec-90d6-0242ac120003"].PATs[0].ID
|
||||||
user, err := store.GetUserByTokenID(tokenId)
|
user, err := store.GetUserByTokenID(tokenID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 (
|
const (
|
||||||
// PATPrefix is the globally used, 4 char prefix for personal access tokens
|
// PATPrefix is the globally used, 4 char prefix for personal access tokens
|
||||||
PATPrefix = "nbp_"
|
PATPrefix = "nbp_"
|
||||||
PATsecretLength = 30
|
// PATSecretLength number of characters used for the secret inside the token
|
||||||
PATLength = 40
|
PATSecretLength = 30
|
||||||
|
// PATChecksumLength number of characters used for the encoded checksum of the secret inside the token
|
||||||
PATChecksumLength = 6
|
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
|
// 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) {
|
func generateNewToken() (string, string, error) {
|
||||||
secret, err := b.Random(PATsecretLength)
|
secret, err := b.Random(PATSecretLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
@ -206,11 +206,8 @@ func (am *DefaultAccountManager) AddPATToUser(accountID string, userID string, p
|
|||||||
|
|
||||||
user.PATs = append(user.PATs, pat)
|
user.PATs = append(user.PATs, pat)
|
||||||
|
|
||||||
if err = am.Store.SaveAccount(account); err != nil {
|
err = am.Store.SaveAccount(account)
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveUser saves updates a given user. If the user doesn't exit it will throw status.NotFound error.
|
// 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",
|
Name: "peer name",
|
||||||
Status: &PeerStatus{Connected: true, LastSeen: time.Now()},
|
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{
|
am := DefaultAccountManager{
|
||||||
Store: store,
|
Store: store,
|
||||||
@ -39,7 +42,7 @@ func TestUser_AddPATToUser(t *testing.T) {
|
|||||||
|
|
||||||
token := "someToken"
|
token := "someToken"
|
||||||
pat := PersonalAccessToken{
|
pat := PersonalAccessToken{
|
||||||
ID: "tokenId",
|
ID: "tokenID",
|
||||||
Description: "some Description",
|
Description: "some Description",
|
||||||
HashedToken: token,
|
HashedToken: token,
|
||||||
ExpirationDate: time.Time{},
|
ExpirationDate: time.Time{},
|
||||||
@ -48,20 +51,23 @@ func TestUser_AddPATToUser(t *testing.T) {
|
|||||||
LastUsed: time.Time{},
|
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)
|
fileStore := am.Store.(*FileStore)
|
||||||
tokenId := fileStore.HashedPAT2TokenID[string(token[:])]
|
tokenID := fileStore.HashedPAT2TokenID[token[:]]
|
||||||
|
|
||||||
if tokenId == "" {
|
if tokenID == "" {
|
||||||
t.Fatal("GetTokenIDByHashedToken failed after adding PAT")
|
t.Fatal("GetTokenIDByHashedToken failed after adding PAT")
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, "tokenId", tokenId)
|
assert.Equal(t, "tokenID", tokenID)
|
||||||
|
|
||||||
userId := fileStore.TokenID2UserID[tokenId]
|
userID := fileStore.TokenID2UserID[tokenID]
|
||||||
if userId == "" {
|
if userID == "" {
|
||||||
t.Fatal("GetUserByTokenId failed after adding PAT")
|
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