Skip to content
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

feat: add multi-auth plugin #10482

Merged
merged 5 commits into from
Nov 20, 2023
Merged

Conversation

madhawa-gunasekara
Copy link
Member

@madhawa-gunasekara madhawa-gunasekara commented Nov 12, 2023

Description

Add multiple authentication for routes. This is the implementation of multi-auth plugin

{
   "plugins":{
      "multi-auth":{
         "auth_plugins":[
            {
               "basic-auth":{ }
            },
            {
               "key-auth":{
                  "query":"apikey",
                  "hide_credentials":true,
                  "header":"apikey"
               }
            },
            {
               "jwt-auth":{
                  "cookie":"jwt",
                  "query":"jwt",
                  "hide_credentials":true,
                  "header":"authorization"
               }
            }
         ]
      }
   }
}

Fixes #10196

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@madhawa-gunasekara madhawa-gunasekara changed the title Add multi-auth plugin feat: add multi-auth plugin Nov 12, 2023
}
},
{
"jwt-auth": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no test case to cover jwt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test cases to cover jwt as well.
This is kind of a wrapper plugin to enable different authentication methods using "or" relationship.
I just added these 3 plugins to show case the compatibility of any plugin with type 'auth'.

t/plugin/multi-auth.t Outdated Show resolved Hide resolved
t/plugin/multi-auth.t Outdated Show resolved Hide resolved

## Description

The `multi-auth` Plugin is used to add multiple authentication methods to a Route or a Service. Plugins with type 'auth' are supported.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For multiple different plug-ins, is there an "and" relationship or an "or" relationship between them? In other words, if there are two authentication plug-ins, do both plug-ins need to be verified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added details in here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it will go through the list of configured auth plugins, and exit after the first one is successfully authenticated. That means route can be shared between different consumers using different auth methods. If a consumer is using basic, he would be able to authenticate using basic, if another user uses jwt, he would be able to authenticate using jwt.

Maybe the doc can be clearer about the intent and use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@madhawa-gunasekara did you see this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will improve the docs

apisix/plugins/multi-auth.lua Show resolved Hide resolved
apisix/plugins/multi-auth.lua Outdated Show resolved Hide resolved
@madhawa-gunasekara
Copy link
Member Author

Hi @moonming,

I fixed the changes you mentioned, can you rereview again?

apisix/plugins/multi-auth.lua Outdated Show resolved Hide resolved
apisix/plugins/multi-auth.lua Outdated Show resolved Hide resolved
t/plugin/multi-auth.t Outdated Show resolved Hide resolved

## Enable Plugin

To enable the Plugin, you have to create a Consumer object with multiple authentication configurations:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better example and a more realistic use case is to have multiple consumers using different authentication plugins using the same route with multi-auth.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add it as an example

## Description

The `multi-auth` Plugin is used to add multiple authentication methods to a Route or a Service. It supports plugins of type 'auth'. You can combine different authentication methods using "or" relationship with `multi-auth` plugin. If you want to use multiple methods in an "and" relationship, apply specific authentication plugins directly to the route or service.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As already mentioned, docs should state that authentication plugins will be tried in the order specified in plugin config, and as soon as one one match, it will let that consumer proceed with the service call.

Copy link
Contributor

@markokocic markokocic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had only doc changes suggested. Functionality is fine.

@moonming
Copy link
Member

@madhawa-gunasekara good job 👍
@markokocic thanks for your review

@moonming moonming merged commit 5b7b1de into apache:master Nov 20, 2023
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants