mirror of
https://github.com/netbirdio/netbird.git
synced 2025-02-02 11:29:46 +01:00
comments for codacy
This commit is contained in:
parent
1343a3f00e
commit
454240ca05
@ -1123,6 +1123,7 @@ func (am *DefaultAccountManager) redeemInvite(account *Account, userID string) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarkPATUsed marks a personal access token as used
|
||||||
func (am *DefaultAccountManager) MarkPATUsed(tokenID string) error {
|
func (am *DefaultAccountManager) MarkPATUsed(tokenID string) error {
|
||||||
unlock := am.Store.AcquireGlobalLock()
|
unlock := am.Store.AcquireGlobalLock()
|
||||||
defer unlock()
|
defer unlock()
|
||||||
|
@ -17,8 +17,13 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/status"
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAccountFromPATFunc function
|
||||||
type GetAccountFromPATFunc func(token string) (*server.Account, *server.User, *server.PersonalAccessToken, error)
|
type GetAccountFromPATFunc func(token string) (*server.Account, *server.User, *server.PersonalAccessToken, error)
|
||||||
|
|
||||||
|
// ValidateAndParseTokenFunc function
|
||||||
type ValidateAndParseTokenFunc func(token string) (*jwt.Token, error)
|
type ValidateAndParseTokenFunc func(token string) (*jwt.Token, error)
|
||||||
|
|
||||||
|
// MarkPATUsedFunc function
|
||||||
type MarkPATUsedFunc func(token string) error
|
type MarkPATUsedFunc func(token string) error
|
||||||
|
|
||||||
// AuthMiddleware middleware to verify personal access tokens (PAT) and JWT tokens
|
// AuthMiddleware middleware to verify personal access tokens (PAT) and JWT tokens
|
||||||
@ -29,8 +34,10 @@ type AuthMiddleware struct {
|
|||||||
audience string
|
audience string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type key string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
userProperty = "user"
|
userProperty key = "user"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewAuthMiddleware instance constructor
|
// NewAuthMiddleware instance constructor
|
||||||
@ -44,13 +51,13 @@ func NewAuthMiddleware(getAccountFromPAT GetAccountFromPATFunc, validateAndParse
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handler method of the middleware which authenticates a user either by JWT claims or by PAT
|
// Handler method of the middleware which authenticates a user either by JWT claims or by PAT
|
||||||
func (a *AuthMiddleware) Handler(h http.Handler) http.Handler {
|
func (m *AuthMiddleware) Handler(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
auth := strings.Split(r.Header.Get("Authorization"), " ")
|
auth := strings.Split(r.Header.Get("Authorization"), " ")
|
||||||
authType := auth[0]
|
authType := auth[0]
|
||||||
switch strings.ToLower(authType) {
|
switch strings.ToLower(authType) {
|
||||||
case "bearer":
|
case "bearer":
|
||||||
err := a.CheckJWTFromRequest(w, r)
|
err := m.CheckJWTFromRequest(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Error when validating JWT claims: %s", err.Error())
|
log.Debugf("Error when validating JWT claims: %s", err.Error())
|
||||||
util.WriteError(status.Errorf(status.Unauthorized, "Token invalid"), w)
|
util.WriteError(status.Errorf(status.Unauthorized, "Token invalid"), w)
|
||||||
@ -58,7 +65,7 @@ func (a *AuthMiddleware) Handler(h http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
case "token":
|
case "token":
|
||||||
err := a.CheckPATFromRequest(w, r)
|
err := m.CheckPATFromRequest(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Error when validating PAT claims: %s", err.Error())
|
log.Debugf("Error when validating PAT claims: %s", err.Error())
|
||||||
util.WriteError(status.Errorf(status.Unauthorized, "Token invalid"), w)
|
util.WriteError(status.Errorf(status.Unauthorized, "Token invalid"), w)
|
||||||
@ -93,7 +100,7 @@ func (m *AuthMiddleware) CheckJWTFromRequest(w http.ResponseWriter, r *http.Requ
|
|||||||
|
|
||||||
// If we get here, everything worked and we can set the
|
// If we get here, everything worked and we can set the
|
||||||
// user property in context.
|
// user property in context.
|
||||||
newRequest := r.WithContext(context.WithValue(r.Context(), userProperty, validatedToken)) // nolint
|
newRequest := r.WithContext(context.WithValue(r.Context(), string(userProperty), validatedToken)) // nolint
|
||||||
// Update the current request with the new context information.
|
// Update the current request with the new context information.
|
||||||
*r = *newRequest
|
*r = *newRequest
|
||||||
return nil
|
return nil
|
||||||
|
@ -9,10 +9,15 @@ import (
|
|||||||
type key string
|
type key string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// TokenUserProperty key for the user property in the request context
|
||||||
TokenUserProperty key = "user"
|
TokenUserProperty key = "user"
|
||||||
|
// AccountIDSuffix suffix for the account id claim
|
||||||
AccountIDSuffix key = "wt_account_id"
|
AccountIDSuffix key = "wt_account_id"
|
||||||
|
// DomainIDSuffix suffix for the domain id claim
|
||||||
DomainIDSuffix key = "wt_account_domain"
|
DomainIDSuffix key = "wt_account_domain"
|
||||||
|
// DomainCategorySuffix suffix for the domain category claim
|
||||||
DomainCategorySuffix key = "wt_account_domain_category"
|
DomainCategorySuffix key = "wt_account_domain_category"
|
||||||
|
// UserIDClaim claim for the user id
|
||||||
UserIDClaim key = "sub"
|
UserIDClaim key = "sub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -58,10 +58,12 @@ type JSONWebKey struct {
|
|||||||
X5c []string `json:"x5c"`
|
X5c []string `json:"x5c"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JWTValidator struct to handle token validation and parsing
|
||||||
type JWTValidator struct {
|
type JWTValidator struct {
|
||||||
options Options
|
options Options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewJWTValidator constructor
|
||||||
func NewJWTValidator(issuer string, audience string, keysLocation string) (*JWTValidator, error) {
|
func NewJWTValidator(issuer string, audience string, keysLocation string) (*JWTValidator, error) {
|
||||||
keys, err := getPemKeys(keysLocation)
|
keys, err := getPemKeys(keysLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -102,6 +104,7 @@ func NewJWTValidator(issuer string, audience string, keysLocation string) (*JWTV
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateAndParse validates the token and returns the parsed token
|
||||||
func (m *JWTValidator) ValidateAndParse(token string) (*jwt.Token, error) {
|
func (m *JWTValidator) ValidateAndParse(token string) (*jwt.Token, error) {
|
||||||
// If the token is empty...
|
// If the token is empty...
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user