papsign / Ktor-OpenAPI-Generator

Ktor OpenAPI/Swagger 3 Generator
Apache License 2.0
241 stars 42 forks source link

Slight workaround needed for JWT auth to work and validate #98

Closed sigmanil closed 2 years ago

sigmanil commented 3 years ago

JWT authorization with the bearer scheme needs a slight workaround for the generated openapi document to be valid and working. Here's what we did, in the route setup for the openapi.json document:

get("/openapi.json") {
            val jackson = jacksonObjectMapper()

            //TODO: These are hacks to fix things I've not yet found out how to do with openapigen (might be bugs)
            val json = jackson.writeValueAsString(application.openAPIGen.api.serialize()).replace(
                oldValue = """"securitySchemes":{"jwtAuth":{"bearerFormat":"JWT","name":"jwtAuth","scheme":"bearer","type":"http"}}""",
                newValue = """"securitySchemes":{"jwtAuth":{"bearerFormat":"JWT","scheme":"bearer","type":"http"}}""",
                ignoreCase = false
            ).replace(
                """"security":[{}]""",
                """"security":[{"jwtAuth":[]}]""",
                false
            )

            call.respond(json)
        }

I'm not 100% sure we haven't mis-configured something somewhere, but I think it's a bug in the generation

Wicpar commented 2 years ago

Is this still relevant ?

kkalisz commented 2 years ago

If I'm correct this was fixed with PR https://github.com/papsign/Ktor-OpenAPI-Generator/pull/88/ (For sure it fixes additional name field in security schema)

I have just tested my existing project hat uses com.github.papsign:Ktor-OpenAPI-Generator:0.2-beta.20 Both parts looks good.

    "securitySchemes": {
      "firebase": {
        "bearerFormat": "JWT",
        "scheme": "bearer",
        "type": "http"
      },
      "jwtAuth": {
        "bearerFormat": "JWT",
        "scheme": "bearer",
        "type": "http"
      }
        "security": [
          {
            "firebase": [

            ]
          },
          {
            "jwtAuth": [

            ]
          }
        ]
sigmanil commented 2 years ago

This has indeed been fixed :-)