Skip to content

Commit

Permalink
better log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard87 committed Oct 14, 2024
1 parent f36dbc4 commit daab67b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
16 changes: 9 additions & 7 deletions api/utils/token/chained_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package token
import (
"context"
"errors"
"fmt"
)

type ChainedValidator struct{ validators []ValidatorInterface }
Expand All @@ -14,15 +15,16 @@ func NewChainedValidator(validators ...ValidatorInterface) *ChainedValidator {
return &ChainedValidator{validators}
}

func (v *ChainedValidator) ValidateToken(ctx context.Context, token string) (principal TokenPrincipal, err error) {
for index, validator := range v.validators {
principal, err = validator.ValidateToken(ctx, token)
if err == nil {
func (v *ChainedValidator) ValidateToken(ctx context.Context, token string) (TokenPrincipal, error) {
var errs []error

for _, validator := range v.validators {
principal, err := validator.ValidateToken(ctx, token)
if principal != nil {
return principal, nil
} else if index == len(v.validators)-1 {
return nil, err
}
errs = append(errs, err)
}

return nil, errNoIssuersFound
return nil, fmt.Errorf("%w: %v", errNoIssuersFound, errors.Join(errs...))
}
1 change: 1 addition & 0 deletions api/utils/token/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TokenPrincipal interface {
}

type ValidatorInterface interface {
// ValidateToken will return a TokenPrincipal object if token payload and signature is validated agains issuer. It will return nil principal and a error if it fails.
ValidateToken(context.Context, string) (TokenPrincipal, error)
}

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func initializeServer(c config.Config) *http.Server {
func initializeTokenValidator(c config.Config) token.ValidatorInterface {
azureValidator, err := token.NewValidator(c.AzureOidc.Issuer, c.AzureOidc.Audience)
if err != nil {
log.Fatal().Err(err).Msg("Error creating JWT validator")
log.Fatal().Err(err).Msg("Error creating JWT Azure OIDC validator")
}

kubernetesValidator, err := token.NewValidator(c.KubernetesOidc.Issuer, c.KubernetesOidc.Audience)
if err != nil {
log.Fatal().Err(err).Msg("Error creating JWT validator")
log.Fatal().Err(err).Msg("Error creating JWT Kubernetes OIDC validator")
}

chainedValidator := token.NewChainedValidator(azureValidator, kubernetesValidator)
Expand Down

0 comments on commit daab67b

Please sign in to comment.