Skip to content

Added securityDefinition to Swagger schema#47

Open
frnknglrt wants to merge 2 commits intonielsfalk:masterfrom
frnknglrt:swaggerWithSecurityDefinition
Open

Added securityDefinition to Swagger schema#47
frnknglrt wants to merge 2 commits intonielsfalk:masterfrom
frnknglrt:swaggerWithSecurityDefinition

Conversation

@frnknglrt
Copy link
Copy Markdown

Reason of change

The current implementation of swagger v2 schemas did not support security defintions to specify the authentication methods for different endpoints. However, the security annotation for each operation was already present. This pull request now allows it to specify the security defintion for swagger v2 schemas.

Example

Setup

install(SwaggerSupport) {
    forwardRoot = true
    swagger = Swagger().apply {
        securityDefinitions.put("basic", BasicAuthSecurityDefinition())
    }
}

Usage

post<pets, PetModel>(
    "create"
        .description("Save a pet in our wonderful database!")
        .responds(
            created<PetModel>()
        ).security(mapOf("basic" to listOf()))
) { _, entity ->
    call.respond(Created, entity.copy(id = newId()).apply {
        data.pets.add(this)
    })
}

Limitations

  1. I did not change the security defintion of generated openapi schemas. There, it was already possible to specify a security definition (using maps). Reworking this part may break existing implementations which rely on setting the securityDefinition via map.

  2. Specifying the authentication does not hook with the ktor authentication. It only provides an information for generating the correct schema. In order to actually enforce the authentication, ktors authenticate(){/*...*/} may be used.

@JLLeitschuh
Copy link
Copy Markdown
Collaborator

I'm more than happy to merge this. However, I do need tests to verify that it works before doing so.

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants