Skip to content

Commit a865453

Browse files
committed
allow nil values for consumer and credential
1 parent 4deec45 commit a865453

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

client/client.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,27 @@ func (c Client) GetConsumer() (consumer entities.Consumer, err error) {
127127
// for the current request. While both consumer and credential can be nil,
128128
// it is required that at least one of them exists. Otherwise this function will throw an error.
129129
func (c Client) Authenticate(consumer *entities.Consumer, credential *AuthenticatedCredential) error {
130-
if consumer == nil {
131-
return fmt.Errorf("Invalid consumer")
130+
if consumer == nil && credential == nil {
131+
return fmt.Errorf("either credential or consumer must be provided")
132132
}
133-
if credential == nil {
134-
return fmt.Errorf("Invalid credential")
135-
}
136-
arg := &kong_plugin_protocol.AuthenticateArgs{
137-
Consumer: &kong_plugin_protocol.Consumer{
133+
134+
arg := &kong_plugin_protocol.AuthenticateArgs{}
135+
if consumer != nil {
136+
arg.Consumer = &kong_plugin_protocol.Consumer{
138137
Id: consumer.Id,
139138
CreatedAt: int64(consumer.CreatedAt),
140139
Username: consumer.Username,
141140
CustomId: consumer.CustomId,
142141
Tags: consumer.Tags,
143-
},
144-
Credential: &kong_plugin_protocol.AuthenticatedCredential{
142+
}
143+
}
144+
if credential != nil {
145+
arg.Credential = &kong_plugin_protocol.AuthenticatedCredential{
145146
Id: credential.Id,
146147
ConsumerId: credential.ConsumerId,
147-
},
148+
}
148149
}
150+
149151
err := c.Ask(`kong.client.authenticate`, arg, nil)
150152
return err
151153
}

0 commit comments

Comments
 (0)