From 8c9e7f8ae9f08fe0e29e4adfc5d65e70ae821e30 Mon Sep 17 00:00:00 2001 From: Enrique Fernandez Date: Sat, 19 Apr 2025 15:58:09 +0200 Subject: [PATCH] feat: support bearer authentication --- docs/generators/elixir.md | 2 +- .../codegen/languages/ElixirClientCodegen.java | 3 ++- .../src/main/resources/elixir/connection.ex.mustache | 11 +++++++++++ .../elixir/lib/openapi_petstore/connection.ex | 5 +++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/generators/elixir.md b/docs/generators/elixir.md index bc1a7a9beb88..0548f29f10de 100644 --- a/docs/generators/elixir.md +++ b/docs/generators/elixir.md @@ -189,7 +189,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 diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java index 99be37036d74..c5299218e21e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java @@ -71,7 +71,8 @@ public ElixirClientCodegen() { .includeDocumentationFeatures(DocumentationFeature.Readme) .securityFeatures(EnumSet.of( SecurityFeature.OAuth2_Implicit, - SecurityFeature.BasicAuth)) + SecurityFeature.BasicAuth, + SecurityFeature.BearerToken)) .excludeGlobalFeatures( GlobalFeature.XMLStructureDefinitions, GlobalFeature.Callbacks, diff --git a/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache b/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache index 18c795e23906..57c8b6fc5ece 100644 --- a/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache @@ -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 diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex b/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex index ec43955eaf29..72f0e64abe55 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex @@ -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)