Skip to content

Commit

Permalink
allow nil values for consumer and credential
Browse files Browse the repository at this point in the history
  • Loading branch information
lvermeire committed Jul 18, 2023
1 parent 4deec45 commit ce32c12
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,27 @@ func (c Client) GetConsumer() (consumer entities.Consumer, err error) {
// for the current request. While both consumer and credential can be nil,
// it is required that at least one of them exists. Otherwise this function will throw an error.
func (c Client) Authenticate(consumer *entities.Consumer, credential *AuthenticatedCredential) error {
if consumer == nil {
return fmt.Errorf("Invalid consumer")
if consumer == nil && credential == nil {
return fmt.Errorf("either credential or consumer must be provided")
}
if credential == nil {
return fmt.Errorf("Invalid credential")
}
arg := &kong_plugin_protocol.AuthenticateArgs{
Consumer: &kong_plugin_protocol.Consumer{

arg := &kong_plugin_protocol.AuthenticateArgs{}
if consumer != nil {
arg.Consumer = &kong_plugin_protocol.Consumer{
Id: consumer.Id,
CreatedAt: int64(consumer.CreatedAt),
Username: consumer.Username,
CustomId: consumer.CustomId,
Tags: consumer.Tags,
},
Credential: &kong_plugin_protocol.AuthenticatedCredential{
}
}
if credential != nil {
arg.Credential = &kong_plugin_protocol.AuthenticatedCredential{
Id: credential.Id,
ConsumerId: credential.ConsumerId,
},
}
}

err := c.Ask(`kong.client.authenticate`, arg, nil)
return err
}
Expand Down

0 comments on commit ce32c12

Please sign in to comment.