feat: support bearer authentication (#21110)

This commit is contained in:
Enrique Fernández 2025-04-25 10:17:32 +02:00 committed by GitHub
parent 3fa806006b
commit be77442bff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 2 deletions

View File

@ -188,7 +188,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|BasicAuth|✓|OAS2,OAS3
|ApiKey|✗|OAS2,OAS3
|OpenIDConnect|✗|OAS3
|BearerToken||OAS3
|BearerToken||OAS3
|OAuth2_Implicit|✓|OAS2,OAS3
|OAuth2_Password|✗|OAS2,OAS3
|OAuth2_ClientCredentials|✗|OAS2,OAS3

View File

@ -71,7 +71,8 @@ public class ElixirClientCodegen extends DefaultCodegen {
.includeDocumentationFeatures(DocumentationFeature.Readme)
.securityFeatures(EnumSet.of(
SecurityFeature.OAuth2_Implicit,
SecurityFeature.BasicAuth))
SecurityFeature.BasicAuth,
SecurityFeature.BearerToken))
.excludeGlobalFeatures(
GlobalFeature.XMLStructureDefinitions,
GlobalFeature.Callbacks,

View File

@ -52,6 +52,9 @@ defmodule {{moduleName}}.Connection do
- `username`: A username for basic authentication.
- `password`: A password for basic authentication.
{{/hasHttpBasicMethods}}
{{#hasHttpBearerMethods}}
- `bearer_token`: A bearer token for bearer authentication.
{{/hasHttpBearerMethods}}
"""
@type options :: [
{:base_url, String.t()},
@ -64,6 +67,9 @@ defmodule {{moduleName}}.Connection do
{:username, String.t() | nil},
{:password, String.t() | nil},
{{/hasHttpBasicMethods}}
{{#hasHttpBearerMethods}}
{:bearer_token, String.t() | nil},
{{/hasHttpBearerMethods}}
]
@doc "Forward requests to Tesla."
@ -240,6 +246,11 @@ defmodule {{moduleName}}.Connection do
end
{{/hasHttpBasicMethods}}
{{#hasHttpBearerMethods}}
bearer_token = Keyword.get(options, :bearer_token)
middleware = [{Tesla.Middleware.BearerAuth, token: bearer_token} | middleware]
{{/hasHttpBearerMethods}}
{{#hasOAuthMethods}}
middleware =
if token = Keyword.get(options, :token) do

View File

@ -46,6 +46,7 @@ defmodule OpenapiPetstore.Connection do
fetcher function.
- `username`: A username for basic authentication.
- `password`: A password for basic authentication.
- `bearer_token`: A bearer token for bearer authentication.
"""
@type options :: [
{:base_url, String.t()},
@ -54,6 +55,7 @@ defmodule OpenapiPetstore.Connection do
{:token_scopes, list(String.t())},
{:username, String.t() | nil},
{:password, String.t() | nil},
{:bearer_token, String.t() | nil},
]
@doc "Forward requests to Tesla."
@ -175,6 +177,9 @@ defmodule OpenapiPetstore.Connection do
middleware
end
bearer_token = Keyword.get(options, :bearer_token)
middleware = [{Tesla.Middleware.BearerAuth, token: bearer_token} | middleware]
middleware =
if token = Keyword.get(options, :token) do
scopes = Keyword.get(options, :token_scopes, @default_scopes)