-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config to chained validator #692
Conversation
api/utils/token/chained_validator.go
Outdated
for index, validator := range v.validators { | ||
principal, err = validator.ValidateToken(ctx, token) | ||
if err == nil { | ||
return principal, nil | ||
} else if index == len(v.validators)-1 { | ||
return nil, err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional suggestion - to make it easier to read
for index, validator := range v.validators { | |
principal, err = validator.ValidateToken(ctx, token) | |
if err == nil { | |
return principal, nil | |
} else if index == len(v.validators)-1 { | |
return nil, err | |
} | |
} | |
if principal, err = validator.ValidateToken(ctx, token); err == nil { | |
return principal, nil | |
} else if index == len(v.validators)-1 { | |
return nil, err | |
} |
or
for index, validator := range v.validators { | |
principal, err = validator.ValidateToken(ctx, token) | |
if err == nil { | |
return principal, nil | |
} else if index == len(v.validators)-1 { | |
return nil, err | |
} | |
} | |
principal, err = validator.ValidateToken(ctx, token) | |
if err == nil { | |
return principal, nil | |
} | |
if index == len(v.validators)-1 { | |
return nil, err | |
} |
or
for index, validator := range v.validators { | |
principal, err = validator.ValidateToken(ctx, token) | |
if err == nil { | |
return principal, nil | |
} else if index == len(v.validators)-1 { | |
return nil, err | |
} | |
} | |
var principal TokenPrincipal | |
var err error | |
for _, validator := range v.validators { | |
if principal, err = validator.ValidateToken(ctx, token); err == nil { | |
return principal, nil | |
} | |
} | |
if err != nil { | |
return nil, err | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed with:
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
}
errs = append(errs, err)
}
return nil, fmt.Errorf("%w: %v", errNoIssuersFound, errors.Join(errs...))
}
api/utils/token/chained_validator.go
Outdated
return &ChainedValidator{validators} | ||
} | ||
|
||
func (v *ChainedValidator) ValidateToken(ctx context.Context, token string) (principal TokenPrincipal, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for names in returned values - method returns them explicitly, like return principal, nil
or return nil, err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, returning a list of errors if not found :)
Release: Chained validator (#692)
No description provided.