swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Apache Authentication and Authorization Conf Gen

Open stkrwork opened this issue 9 years ago • 14 comments

Description

With the information in the Swagger AuthMethods component, a Apache configuration could be generated.

It might also be useful to extend the AuthMethods Class to include a groups Array, to create the group handling in Apache for HTTP Basic/Digest Authentication/LDAP.

Through this one can generate the server stub and reroute the calls to the server stub via apache. Apache then takes care of the authentication and authorization.

stkrwork avatar Jun 02 '17 08:06 stkrwork

Yes, I think that will be useful. There are discussions about generating a DB schema from OpenAPI/Swagger 2.0 spec so there are unlimited possibilities with Swagger Codegen to generate "things" other than API client, server stub, documentation.

wing328 avatar Jun 05 '17 07:06 wing328

I did make an implementation for it, but it misuses the scope parameters for groups for apache authorization instead as swagger codegen does not support groups for authorization for http basic requests.

This implementation generates the paths in form of <Location {{fullPath}}></Location> with limits set within for each http method available in the spec. It is currently written to force a valid user for all method requests that are in the spec.

stkrwork avatar Jun 06 '17 08:06 stkrwork

as swagger codegen does not support groups for authorization for http basic requests.

What about using vendor extensions to document that in the spec?

wing328 avatar Jun 06 '17 09:06 wing328

So the user would already include a vendor extension section in the input spec containing the groups?

Something like this:

"/pet": {
      "put": {
        "tags": [
          "pet"
        ],
        "summary": "Update an existing pet",
        "description": "",
        "operationId": "updatePet",
        "consumes": [
          "application/json",
          "application/xml"
        ],
        "produces": [
          "application/xml",
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Pet"
            }
          }
        ],
        "responses": {
          "400": {
            "description": "Invalid ID supplied"
          },
          "404": {
            "description": "Pet not found"
          },
          "405": {
            "description": "Validation exception"
          }
        },
        "security": [
          {
            "petstore_auth": [
              "write:pets",
              "read:pets"
            ]
          }
        ],
        "vendorExtensions": [
          {
            "x-apache-groups": [
              "test",
              "test2"
            ]
          }
        ]
      }
    },

So if Auth Method is Basic, then check for the x-apache-groups section in the vendor extensions. Is that what you meant?

stkrwork avatar Jun 06 '17 09:06 stkrwork

Yup, that's one way to do it if we want to classify the endpoint into 1 or more groups.

Is the Apache group similar to the security definition (e.g. petstore_auth)?

wing328 avatar Jun 06 '17 09:06 wing328

If the logged in user is not part of the group on the apache server, the apache server returns unauthorized.

Through this you could define a group test, and on all POST PUT and DELETE methods add a require group test in <limit> tags. Every user that is not part of test, will get a unauthorized Header returned. If you want every logged in user to use a resource, you would define require valid-user, as no group is required then. If everybody can access is, just leave away the require statement.

That is why i used the scopes param in the Swagger Spec instead, as they serve a similar purpose in OAuth

stkrwork avatar Jun 06 '17 09:06 stkrwork

I would suggest you to submit a PR for the generator so that we can look at the output and see how we can improve it together.

wing328 avatar Jun 07 '17 13:06 wing328

#5800 is the pr but it is still not ready yet. but it shows the basic idea.

stkrwork avatar Jun 08 '17 07:06 stkrwork

@stkrwork I think it's a very good starting point for a configuration generator 👍

I would suggest you to list out a list of tasks left to be done to make the generator ready for an alpha/beta release so that the community can help out.

wing328 avatar Jun 18 '17 15:06 wing328

Well in terms of using it, the generated config can be copied into an apache webserver config right now.

If one would want to extend it, it would be possible to add more cli options for generating in cgi handling, enable logging, change the path of the log files, or change the authentication from basic to having a form to handle login.

Other things that can be done to extend it is setting the server name at the top and encapsulate the currently generated code in VirtualHost tags, to change the generated config into a more complete config stub.

stkrwork avatar Jun 18 '17 19:06 stkrwork

@stkrwork I would suggest we merge it into master so as to gather feedback from the community. Then we can create a list of tasks for enhancements (similar to what you've done for c++ RestBed recently)

wing328 avatar Jun 27 '17 16:06 wing328

Yes, I agree. I'll put up the todo list as soon as its merged in.

stkrwork avatar Jun 27 '17 17:06 stkrwork

#5936 for the todo list

stkrwork avatar Jun 27 '17 18:06 stkrwork

Closed via #5800

We'll continue the discussion for enhancements in #5936

wing328 avatar Jun 28 '17 07:06 wing328