|
| 1 | +If your OpenAPI specification file has `securitySchemes` https://spec.openapis.org/oas/v3.1.0#security-scheme-object[definitions], the inner generator |
| 2 | +will https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_provider_declaration[register ClientRequestFilter providers] for you to |
| 3 | +implement the given authentication mechanism. |
| 4 | +
|
| 5 | +To provide the credentials for your application, you can use the https://quarkus.io/guides/config[Quarkus configuration support]. The configuration key is composed using this |
| 6 | +pattern: `quarkus.openapi-generator.[filename].auth.[security_scheme_name].[auth_property_name]`. Where: |
| 7 | +
|
| 8 | +* `filename` is the sanitized name of file containing the OpenAPI spec, for example `petstore_json`. |
| 9 | +* `security_scheme_name` is the sanitized name of the https://spec.openapis.org/oas/v3.1.0#security-scheme-object[security scheme object definition] in the OpenAPI file. Given the following excerpt, we |
| 10 | +have `api_key` and `basic_auth` security schemes: |
| 11 | +
|
| 12 | +[source,json] |
| 13 | +---- |
| 14 | +{ |
| 15 | + "securitySchemes": { |
| 16 | + "api_key": { |
| 17 | + "type": "apiKey", |
| 18 | + "name": "api_key", |
| 19 | + "in": "header" |
| 20 | + }, |
| 21 | + "basic_auth": { |
| 22 | + "type": "http", |
| 23 | + "scheme": "basic" |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | +---- |
| 28 | +
|
| 29 | +WARNING: Note that the securityScheme name used to configure the specific information for each spec is sanitized using the same rules as for the file names. |
| 30 | +
|
| 31 | +* `auth_property_name` varies depending on the authentication provider. For example, for Basic Authentication we have `username` and `password`. See the following sections for more details. |
| 32 | +
|
| 33 | +> Tip: on production environments you will likely to use https://quarkiverse.github.io/quarkiverse-docs/quarkus-vault/dev/index.html[HashiCorp Vault] or https://kubernetes.io/docs/concepts/configuration/secret/[Kubernetes Secrets] to provide this information for your application. |
| 34 | +
|
| 35 | +If the OpenAPI specification file has `securitySchemes` definitions, but no https://spec.openapis.org/oas/v3.1.0#security-requirement-object[Security Requirement Object] definitions, the generator can be configured to create these by default. In this case, for all operations without a security requirement the default one will be created. Note that the property value needs to match the name of a security scheme object definition, eg. `api_key` or `basic_auth` in the `securitySchemes` list above. |
| 36 | +
|
| 37 | +
|
| 38 | +[%autowidth] |
| 39 | +|=== |
| 40 | +|Description |Property Key |Example |
| 41 | +
|
| 42 | +|Create security for the referenced security scheme |
| 43 | +|`quarkus.openapi-generator.codegen.default-security-scheme` |
| 44 | +|`quarkus.openapi-generator.codegen.default-security-scheme=api_key` |
| 45 | +|=== |
| 46 | +
|
| 47 | +See the module https://github.com/quarkiverse/quarkus-openapi-generator/tree/main/integration-tests/security[security] for an example of how to use this feature. |
| 48 | +
|
| 49 | +== Basic HTTP Authentication |
| 50 | +
|
| 51 | +For Basic HTTP Authentication, these are the supported configurations: |
| 52 | +
|
| 53 | +[%autowidth] |
| 54 | +|=== |
| 55 | +|Description |Property Key |Example |
| 56 | +
|
| 57 | +|Username credentials |
| 58 | +|`quarkus.openapi-generator.[filename].auth.[security_scheme_name].username` |
| 59 | +| `quarkus.openapi-generator.petstore_json.auth.basic_auth.username` |
| 60 | +|Password credentials |
| 61 | +|`quarkus.openapi-generator.[filename].auth.[security_scheme_name].password` |
| 62 | +|`quarkus.openapi-generator.petstore_json.auth.basic_auth-password` |
| 63 | +|=== |
| 64 | +
|
| 65 | +== Bearer Token Authentication |
| 66 | +
|
| 67 | +Authentication, these are the supported configurations: |
| 68 | +
|
| 69 | +[%autowidth] |
| 70 | +|=== |
| 71 | +|Description |Property Key |Example |
| 72 | +
|
| 73 | +|Bearer Token |
| 74 | +|`quarkus.openapi-generator.[filename].auth.[security_scheme_name].bearer-token` |
| 75 | +|`quarkus.openapi-generator.petstore_json.auth.bearer.bearer-token` |
| 76 | +|=== |
| 77 | +
|
| 78 | +== API Key Authentication |
| 79 | +
|
| 80 | +Similarly to bearer token, the API Key Authentication also has the token entry key property: |
| 81 | +
|
| 82 | +[%autowidth] |
| 83 | +|=== |
| 84 | +|Description |Property Key |Example |
| 85 | +
|
| 86 | +|API Key |
| 87 | +|`quarkus.openapi-generator.[filename].auth.[security_scheme_name].api-key` |
| 88 | +|`quarkus.openapi-generator.petstore_json.auth.api_key.api-key` |
| 89 | +|=== |
| 90 | +
|
| 91 | +The API Key scheme has an additional property that requires where to add the API key in the request token: header, cookie or query. The inner provider takes care of that for you. |
| 92 | +
|
| 93 | +== OAuth2 Authentication |
| 94 | +
|
| 95 | +The extension will generate a `ClientRequestFilter` capable to add OAuth2 authentication capabilities to the OpenAPI operations that require it. This means that you can use |
| 96 | +the https://quarkus.io/guides/security-openid-connect-client[Quarkus OIDC Extension] configuration to define your authentication flow. |
| 97 | +
|
| 98 | +The generated code creates a named `OidcClient` for each https://spec.openapis.org/oas/v3.1.0#security-scheme-object[Security Scheme] listed in the OpenAPI specification files. For example, given |
| 99 | +the following excerpt: |
| 100 | +
|
| 101 | +[source,json] |
| 102 | +---- |
| 103 | +{ |
| 104 | + "securitySchemes": { |
| 105 | + "petstore_auth": { |
| 106 | + "type": "oauth2", |
| 107 | + "flows": { |
| 108 | + "implicit": { |
| 109 | + "authorizationUrl": "https://petstore3.swagger.io/oauth/authorize", |
| 110 | + "scopes": { |
| 111 | + "write:pets": "modify pets in your account", |
| 112 | + "read:pets": "read your pets" |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +---- |
| 120 | +
|
| 121 | +You can configure this `OidcClient` as: |
| 122 | +
|
| 123 | +[source,properties] |
| 124 | +---- |
| 125 | +quarkus.oidc-client.petstore_auth.auth-server-url=https://petstore3.swagger.io/oauth/authorize |
| 126 | +quarkus.oidc-client.petstore_auth.discovery-enabled=false |
| 127 | +quarkus.oidc-client.petstore_auth.token-path=/tokens |
| 128 | +quarkus.oidc-client.petstore_auth.credentials.secret=secret |
| 129 | +quarkus.oidc-client.petstore_auth.grant.type=password |
| 130 | +quarkus.oidc-client.petstore_auth.grant-options.password.username=alice |
| 131 | +quarkus.oidc-client.petstore_auth.grant-options.password.password=alice |
| 132 | +quarkus.oidc-client.petstore_auth.client-id=petstore-app |
| 133 | +---- |
| 134 | +
|
| 135 | +The configuration suffix `quarkus.oidc-client.petstore_auth` is exclusive for the schema defined in the specification file and the `schemaName` is sanitized by applying the rules described above. |
| 136 | +
|
| 137 | +For this to work you **must** add https://quarkus.io/guides/security-openid-connect-client#oidc-client-filter[Quarkus OIDC Client Filter Extension] to your project: |
| 138 | +
|
| 139 | +RESTEasy Classic: |
| 140 | +
|
| 141 | +[source ,xml] |
| 142 | +---- |
| 143 | +<dependency> |
| 144 | + <groupId>io.quarkus</groupId> |
| 145 | + <artifactId>quarkus-oidc-client-filter</artifactId> |
| 146 | +</dependency> |
| 147 | +---- |
| 148 | +
|
| 149 | +RESTEasy Reactive: |
| 150 | +
|
| 151 | +[source ,xml] |
| 152 | +---- |
| 153 | +<dependency> |
| 154 | + <groupId>io.quarkus</groupId> |
| 155 | + <artifactId>quarkus-oidc-client-reactive-filter</artifactId> |
| 156 | +</dependency> |
| 157 | +---- |
| 158 | +
|
| 159 | +If authentication support doesn't suit your needs you can decide to disable it with `enable-security-generation=false`. In such case CompositeAuthenticationProvider and AuthenticationPropagationHeadersFactory wont be generated and used with your api. |
| 160 | +The option can be set globally with `quarkus.openapi-generator.codegen.enable-security-generation` or per api `quarkus.openapi-generator.codegen.spec.my_spec_yml.enable-security-generation` |
| 161 | +Custom authentication provider can be used with `additional-api-type-annotations` |
| 162 | +
|
| 163 | +See the module https://github.com/quarkiverse/quarkus-openapi-generator/tree/main/integration-tests/generation-tests[generation-tests] for an example of how to use this feature. |
0 commit comments