From ce32c1230ea2a721fa7b856d120971236fcbec54 Mon Sep 17 00:00:00 2001 From: lvermeire Date: Tue, 18 Jul 2023 16:18:14 +0200 Subject: [PATCH] allow nil values for consumer and credential --- client/client.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/client/client.go b/client/client.go index 6c50f3f..2872446 100644 --- a/client/client.go +++ b/client/client.go @@ -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 }